我有与该脚本一起使用的伪数据:它只是更新我在工作表2中输入的值,并为工作表1中rIterators
匹配的任何地方使用数组乘数应用它们。对于这个问题,这些都不是必需的,我只需要知道如何更新我的j
变量引用即可。
当前是:
For j = 3 To 5
wsA.Cells(fndRow + i, j).Value = rIterator.Offset(, j - 1).Value * multplr(i)
Next j
,它引用两个工作表中的C-E列,因为这是值对齐的地方。
如果Sheet2的C-E列中的值需要更新,但是这些列存在于Sheet1的AD-AF列中,我该如何更改呢?
其余代码:
Sub UpdateTSRS()
Dim wbk As Workbook
Dim wsA As Worksheet, wsB As Worksheet
Dim rngA As Range, rngB As Range
Dim rIterator As Range
Dim fndRow As Long
Dim multplr As Variant
multplr = Array(1, 1.1, 1.15, 1.2, 1.3)
Set wbk = ThisWorkbook
Set wsA = wbk.Sheets("Sheet1")
Set wsB = wbk.Sheets("Sheet2")
Set rngA = wsA.Range(wsA.Range("A2"), wsA.Range("A2").End(xlDown))
Set rngB = wsB.Range(wsB.Range("A2"), wsB.Range("A2").End(xlDown))
For Each rIterator In rngB
On Error Resume Next
fndRow = Application.Match(rIterator.Value, rngA, 0) + _
rngA.Range("A1").Row - 1
If Err.Number <> 0 Then
Else
For i = 0 To 4
For j = 3 To 5
wsA.Cells(fndRow + i, j).Value = rIterator.Offset(, j - 1).Value * multplr(i)
Next j
Next i
wsA.Range(wsA.Cells(fndRow, 3), wsA.Cells(fndRow, 5)).Resize(5).Interior.Color = RGB(255, 255, 0)
End If
Err.Clear
Next rIterator
End Sub
答案 0 :(得分:1)
根据我的评论显示代码更改:
'Set wsA = wbk.Sheets("Sheet1")
'Set wsB = wbk.Sheets("Sheet2")
Set wsB = wbk.Sheets("Sheet1")
Set wsA = wbk.Sheets("Sheet2")
,您的循环类似于:
For j = 3 To 5
wsA.Cells(fndRow + i, j).Value = rIterator.Offset(, j +27 - 1).Value * multplr(i) 'i think i got that? destination still C->E but source is AD->AF
Next j