用于替换预先存在的数字的宏

时间:2014-07-20 16:48:24

标签: excel vba excel-vba

我有一个我正在尝试运行的宏的VBA代码。它的作用是从ItemReceipts获取数据,如果它符合标准,它将D列中的总和数放入Demand Planning Prem表的E列。

虽然宏工作的问题是我在需要放置数据的列中有一个公式,用于在每个单元格中运行平均值。当我运行我的宏时,它似乎添加到已存在的数字,但我希望它完全替换该数字和公式。

有人可以帮我一把吗?

Sub subStuff()
Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, lr As Long,     rng As Range, c As Range
Dim fLoc As Range, fAdr As String
Set wb1 = Workbooks("ItemReceipts")
Set wb2 = Workbooks("Demand Planning Prem")
Set sh1 = wb1.Sheets(1) 'Edit sheet name
Set sh2 = wb2.Sheets(1) 'Edit sheet name
lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh1.Range("A2:A" & lr)
For Each c In rng
    Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).Find(c.Offset(0, 2).Value, , xlValues, xlWhole)
        If Not fLoc Is Nothing Then
            fAdr = fLoc.Address
            Do
                If Application.WeekNum(fLoc.Offset(0, -1).Value) = Application.WeekNum(c.Value) Then
                    sh2.Range("E" & fLoc.Row) = sh1.Range("D" & c.Row) + sh2.Range("E" & fLoc.Row)
                    Exit Do
                End If
                Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).FindNext(fLoc)
            Loop While fAdr <> fLoc.Address
        End If
Next
End Sub

ItemReceipts

+ ------------ + --------- + ------- + --------- +
|日期----- |编号 - |项目|数量|
+ ------------ + --------- + ------- + --------- +
| 8/8/2014 | 140981 | AHF-001 | 5 |
+ ------------ + --------- + ------- + --------- +
| 8/3/2014 | 140981 | AHF-001 | 3 |
+ ------------ + --------- + ------- + --------- +

需求计划预算

+ ------------ + --------- + ------- + --------- + ----- ------- +
|日期----- | SKU --- |名称| FG --- ---添加退货|
+ ------------ + --------- + ------- + --------- + -------- -----
| 8/8/2014 | AHF-001 |帐篷| 5744 | 4 |
+ ------------ + --------- + ------- + --------- + -------- ---- +

因此,ItemReceipt会被扫描,如果物品占用SKU并且物品收集的日期属于需求计划预约日期的那一周,那么我希望总数量替换添加退货中的数量。

所以在这种情况下取​​5 + 3 = 8并用8代替4.现在它的作用是4 + 8 = 12这不是我想要的。

2 个答案:

答案 0 :(得分:0)

作为快速解决方法,只需尝试在执行循环之前将sh2.Range(&#34; E&#34;&amp; fLoc.Row).value初始化为0

答案 1 :(得分:0)

我同意@IAmDranged。

将代码编辑为以下内容:

    Sub subStuff()
    Dim wb1 As Workbook, wb2 As Workbook, sh1 As Worksheet, sh2 As Worksheet, lr As Long,     rng As Range, c As Range
    Dim fLoc As Range, fAdr As String
    Set wb1 = Workbooks("ItemReceipts")
    Set wb2 = Workbooks("Demand Planning Prem")
    Set sh1 = wb1.Sheets(1) 'Edit sheet name
    Set sh2 = wb2.Sheets(1) 'Edit sheet name
    lr = sh1.Cells(Rows.Count, 1).End(xlUp).Row
    Set rng = sh1.Range("A2:A" & lr)
    For Each c In rng
        Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).Find(c.Offset(0, 2).Value, , xlValues, xlWhole)
            If Not fLoc Is Nothing Then
                fAdr = fLoc.Address

sh2.Range("E" & fLoc.Row) = "0" ' have added this line.

                Do
                    If Application.WeekNum(fLoc.Offset(0, -1).Value) = Application.WeekNum(c.Value) Then
                        sh2.Range("E" & fLoc.Row) = sh1.Range("D" & c.Row) + sh2.Range("E" & fLoc.Row)
                        Exit Do
                    End If
                    Set fLoc = sh2.Range("B2", sh2.Cells(Rows.Count, 2).End(xlUp)).FindNext(fLoc)
                Loop While fAdr <> fLoc.Address
            End If
    Next
    End Sub

这样,在开始遍历所有可以找到的值的循环之前,找到的项目的“Add Returns”值将为“0”。因此,当您启动脚本时,它将是这样的:

0 + 5 + 3 = 8 ....基本上按要求替换4到8:)

如果这可以解决您的问题,请告诉我。