|
一般用vba很少把公式代入工作表,最多是直取application.公式的值,
猜想把公式代入工作表的目的,是給只會公式的人運用看的吧! 看用公式及vba結果是完全一樣
有可能只會公式的人會懷疑VBA執行後的結果值是不是真的對的吧!
Sub test11()
Set A = Sheets("2").[A1].CurrentRegion
Set A = A.Resize(A.Rows.Count - 1, A.Columns.Count).Offset(1)
AA = A.Address
AAA = ",2!" & AA
'---------------------------------------------
r = Sheets("1").[A65000].End(3).Row
Sheets("1").Range("B2:I" & r).ClearContents
'---------------------------------------------
Set B = Sheets("1").Range([A2], [A2].End(4))
For i = 1 To B.Cells.Count
With B.Cells(i)
VA = .Address(0, 0)
V = .Value
.Offset(, 1).Formula = "=VLookup(" & VA & AAA & ",2,0)"
.Offset(, 2).Formula = "=VLookup(" & VA & AAA & ",3,0)"
.Offset(, 3).Formula = "=VLookup(" & VA & AAA & ",4,0)"
.Offset(, 4).Formula = "=VLookup(" & VA & AAA & ",5,0)"
.Offset(, 5) = Application.VLookup(V, Sheets("2").Range(AA), 2, 0)
.Offset(, 6) = Application.VLookup(V, Sheets("2").Range(AA), 3, 0)
.Offset(, 7) = Application.VLookup(V, Sheets("2").Range(AA), 4, 0)
.Offset(, 8) = Application.VLookup(V, Sheets("2").Range(AA), 5, 0)
End With
Next
End Sub |
|