停留一个步骤即可更新database_ gudang中的库存编号(列“ D”)(database_ gudang中的库存已添加到来自form_penerimaan的收据金额(列“ K”)中)
更新基于商品名称(nama barang),因此,如果form_penerimaan中的商品名称(“ C”列)与以下项目的名称(“ B”列)相同database_ gudang,数据库_ gudang中的库存将更新。
但是有一个问题,该问题仅在第2、9、10行(黄色单元格)中进行了更新。 3,4,5行也应更新。
非常感谢您的帮助。
致谢。
Sub Module1()
s = 10
OT1 = Sheets("Database_Gudang").Cells(Rows.Count, "D").End(xlUp).Row
For j = 2 To OT1
NB1 = Sheets("Database_Gudang").Cells(j, "B").Value
Sheets("Form_Penerimaan").Activate
If Cells(s, "C").Value = NB1 And Cells(s, "C").Value <> "" Then
Sheets("Form_Penerimaan").Cells(s, "Q").Copy
Sheets("Database_Gudang").Activate
Sheets("Database_Gudang").Cells(j, "G").Select
Selection.PasteSpecial Paste:=xlPasteValues
s = s + 1
End If
Next j
End Sub
答案 0 :(得分:0)
嗨,欢迎来到stackoverflow:)
避免使用.Select
和.Activate
。直接使用变量和对象。您可能需要查看How to avoid using Select in Excel VBA
您正面临该问题,因为您没有在第二张工作表的单元格中循环。
这是您要尝试的吗? (未测试)
我已注释了该代码,因此您可能对理解它没有任何问题。如果您愿意,请分享确切的错误消息,我们将从那里获取。
Sub Sample()
Dim wsThis As Worksheet, wsThat As Worksheet
Dim i As Long, j As Long
Dim wsThisLRow As Long, wsThatLRow As Long
'~~> Set your worksheets
Set wsThis = ThisWorkbook.Sheets("Database_Gudang")
Set wsThat = ThisWorkbook.Sheets("Form_Penerimaan")
'~~> Find relevant last row in both sheets
wsThisLRow = wsThis.Range("D" & wsThis.Rows.Count).End(xlUp).Row
wsThatLRow = wsThat.Range("C" & wsThat.Rows.Count).End(xlUp).Row
With wsThis
'~~> Loop through cell in Database_Gudang
For i = 2 To wsThisLRow
'~~> Loop through cell in Form_Penerimaan
For j = 10 To wsThatLRow
'~~> Compare values and get values across if applicable
If .Range("B" & i).Value = wsThat.Range("C" & j).Value Then
.Range("G" & i).Value = wsThat.Range("Q" & j).Value
Exit For
End If
Next j
Next i
End With
End Sub