我正在尝试在VBA中编写从另一个工作簿获取数据并将其粘贴到正确位置的代码。例如:在Excel中将数据从一个表单移动到另一个表单。
在一个工作簿:
123 x
321 xx
159 xxx
235 xxxx
的X列应被复制并放置在正确的地方结束这样的:
235 xxxx
321 xx
123 x
159 xxx
我已经设法得到数据,但问题是当我试图把它放在正确的单元格中时; For...Next
块中的If
声明无法正常工作。这里是我的代码:
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
为什么不使用Vlookup? – 2012-07-12 13:35:50
继我的意见,你可以使用这样的'范围( “B200:B300”)。公式=“= VLOOKUP(B200, 'G:\ [PPM_et_top_fournisseur.xls] PPM officiels' $ B $ 12!$ C $ 43 2,0)“' – 2012-07-12 13:44:03