我确定我做得不对,但我要做的是插入带有for循环功能的vlookup公式。
Dim cnt As Integer
Dim row As Long
Dim col As Long
cnt = [A:A].Cells.SpecialCells(xlCellTypeConstants).Count
For row = 1 To cnt
col = col + 1
[A2].Offset(row - 1, 1).Value = "Vlookup(A" & col + 1 & ",'Sheet1'!,A$2:A$" & cnt & ",1,0)"
Next row
VLOOKUP
公式lookup_value
应与col + 1
相同,col_index_num
等于cnt
我尝试了.Value
选项,但它不接受它,因为它在字符串上有"="
。我尝试使用.Formula
功能,但它没有给我一个在col + 1
上插入cnt.
的选项以及 return "translate(`enter code here`" + arc.centroid(d) + ")";
答案 0 :(得分:0)
不确定table_array
应该是什么。但是这样的事情应该有效:
[a2].Offset(row - 1, 1).Value = "=Vlookup(A" & col + 1 & ",'Sheet1'!A$2," & cnt & ",0)"
答案 1 :(得分:0)
此行中有一个流氓逗号
[A2].Offset(row - 1, 1).Value = "Vlookup(A" & col + 1 & ",'Sheet1'!,A$2:A$" & cnt & ",1,0)"
'Sheet1'!
之后不应该有逗号。删除它。
编辑:您需要使用=
符号启动公式,即使在VBA中输入该公式也是如此。
This code runs fine in my tests:
Sub test()
Dim cnt As Integer
Dim row As Long
Dim col As Long
cnt = [A:A].Cells.SpecialCells(xlCellTypeConstants).Count
For row = 1 To cnt
col = col + 1
[A2].Offset(row - 1, 1).Value = "=Vlookup(A" & col + 1 & ",'Sheet1'!A$2:A$" & cnt & ",1,0)"
Next row
End Sub
答案 2 :(得分:0)
如果我正确地抓住了你的目标,你可以避免循环并使用" .FormulaR1C1" "范围"对象利用相对范围引用,如下所示:
Sub main()
Dim cnt As Long
With Worksheets("Sheet1") '<--| this is the sheet where to lookup
cnt = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
End With
With Worksheets("MySheet") '<--| change "MySheet" with your actual "working" sheet name
.Range("B2", .Cells(.Rows.Count, 2).End(xlUp)).FormulaR1C1 = "=Vlookup(RC[-1], 'Sheet1'!A$2:A$" & cnt & ",1,0)"
End With
End Sub