我正在嘗試在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