我正在尝试在 VBA 中编写从另一个工作簿获取数据并将其粘贴到正确位置的代码。例如:
在一个工作簿中:
123 x
321 xx
159 xxx
235 xxxx
x 列应该被复制并放置在正确的位置,最终如下所示:
235 xxxx
321 xx
123 x
159 xxx
我已经设法获取数据,但问题是当我尝试将其放入正确的单元格时;块中的If
语句For...Next
不能正常工作。这是我的代码:
Sub copyToWorkbook()
Dim arr(32) As String
Dim rr(32) As String
Dim temp(32) As String
For k = 1 To 32
temp(k) = Cells(k, 1).Value
Next
With ActiveSheet
.Range("B200:B300").FormulaArray = "='" & "g:" & "\[" & "PPM_et_top_fournisseur.xls" & "]" _
& "PPM officiels" & "'!" & "B12:B43"
End With
Dim q As Integer
For f = 1 To 32
q = 199 + f
arr(f) = Cells(q, 2).Value
Cells(f, 8) = arr(f)
Next
With ActiveSheet.Range("C200:C300")
.FormulaArray = "='" & "g:" & "\[" & "PPM_et_top_fournisseur.xls" & "]" _
& "PPM officiels" & "'!" & "J12:J43"
.Value = .Value
End With
Dim w As Integer
For o = 1 To 32
w = 199 + o
rr(o) = Cells(w, 3).Value
Cells(o, 9) = rr(o)
Next
For t = 1 To 32
For n = 1 To 32
If Cells(20, 1).Value <> arr(n) Then
Cells(n, 7) = arr(n)
Else: Cells(t, 10) = "nop"
End If
Next
Next
End Sub