此代码的问题在于,当调整公式以封装行中的所有值时,查找值引用保持不变。也就是说,如果第一个单元格的查找值为50.88,则它下面的下一个单元格保持相同的查找值50.88,但我希望它前进到列表中的下一个数字。
有没有人知道如何在rownum转移到下一个值时加入+1的东西?
Private Sub CommandButton8_Click()
Dim rownum As Long
Dim colnum As Long
Dim x As Long
Dim y As Long
Dim colindexval As Double
Dim resizeval As Double
Dim fVLOOKUP As String
rownum = Sheet1.Cells(28, 21).Value
colnum = Sheet1.Cells(27, 21).Value
x = Sheet1.Cells(20, 21).Value
y = Sheet1.Cells(21, 21).Value
resizeval = Sheet1.Cells(19, 12).Value
colindexval = Sheet1.Cells(29, 12).Value
fVLOOKUP = "=VLOOKUP(@1,'@2'!@3,@4,FALSE)"
fVLOOKUP = Replace(fVLOOKUP, "@1", Sheet9.Cells(rownum, colnum).Value)
fVLOOKUP = Replace(fVLOOKUP, "@2", Sheets("Price Data").Name)
fVLOOKUP = Replace(fVLOOKUP, "@3", Sheets("Price Data").Range("A12").CurrentRegion.Address)
fVLOOKUP = Replace(fVLOOKUP, "@4", colindexval)
Sheet9.Cells(y, x).Resize(resizeval, 1).Formula = fVLOOKUP
End Sub
答案 0 :(得分:0)
修改您的代码,如下所示。使用填充技术,excel应该能够为您完成剩余的工作。
Private Sub CommandButton8_Click()
Dim rownum As Long
Dim colnum As Long
Dim x As Long
Dim y As Long
Dim colindexval As Double
Dim resizeval As Double
Dim fVLOOKUP As String
rownum = Sheet1.Cells(28, 21).Value
colnum = Sheet1.Cells(27, 21).Value
x = Sheet1.Cells(20, 21).Value
y = Sheet1.Cells(21, 21).Value
resizeval = Sheet1.Cells(19, 12).Value
colindexval = Sheet1.Cells(29, 12).Value
fVLOOKUP = "=VLOOKUP(@1,'@2'!@3,@4,FALSE)"
fVLOOKUP = Replace(fVLOOKUP, "@1", Sheet9.Cells(rownum, colnum).Address(false, false))
fVLOOKUP = Replace(fVLOOKUP, "@2", Sheets("Price Data").Name)
fVLOOKUP = Replace(fVLOOKUP, "@3", Sheets("Price Data").Range("A12").CurrentRegion.Address)
fVLOOKUP = Replace(fVLOOKUP, "@4", colindexval)
Sheet9.Cells(y, x).Value = fVLOOKUP
Sheet9.Range(Cells(y, x), Cells.Offset(resizeval, 1)).FillDown
End Sub
注意,在excel VBA中使用.Address时,默认是包含$来锁定单元格地址,通过执行.Address(False,False)删除它们。
答案 1 :(得分:0)
这可能会让你有所思考
Dim i as integer
for each acell in Sheet9.Cells(y, x).Resize(resizeval, 1)
fVLOOKUP = Replace(fVLOOKUP, "@1", Sheet9.Cells(rownum+1, colnum).address)
fVLOOKUP = Replace(fVLOOKUP, "@2", Sheets("Price Data").Name)
fVLOOKUP = Replace(fVLOOKUP, "@3", Sheets("Price Data").Range("A12").CurrentRegion.Address)
fVLOOKUP = Replace(fVLOOKUP, "@4", colindexval)
acell.Formula = fVLOOKUP
i = i + 1
next acell