很抱歉,如果我搞砸了一些格式,但这是我的第一个问题。我正在尝试使用excel宏将单元格的值设置为VLookup函数。它接受来自userForm的输入以确定范围,Vlookup工作表,输出列和查找值。尝试使用它时,我遇到了很多错误。任何帮助将不胜感激!
Sub Go_Click()
Dim First As Boolean
Dim Start As Integer
First = True
Start = LookupName.Value
Sett Start, First
End Sub
Sub Sett(Start As Integer, First As Boolean)
'EXIT WHEN NO VALUE IS FOUND WHERE NAME IS SUPPOSED TO BE
If IsEmpty(Cells(Start, 1)) Then
If First = False Then
Exit Sub
End If
End If
Dim val As String
val = Cells(Start, 1).Value
Dim Hincrement As Integer
Hincrement = 1 'start at col 2 (increments in loop)
Dim sheetname1 As String
Dim start_range As String
Dim end_range As String
sheetname1 = name_of_sheet.Value
start_range = RangeStart.Value
end_range = RangeEnding.Value 'all values saving correctly
Dim myRange As Range
Dim ending As Integer
ending = (Start) + Range(start_range, end_range).Columns.Count
Cells(1, 17) = ending
Dim out As Integer
out = Output.Value
Dim ptr As Integer 'stays at original start point
For ptr = Start + 1 To ending
Hincrement = Hincrement + 1
Worksheets("Sheet1").Range(ptr, out).Formula = "=VLookup(val, Worksheets(sheetname1).Range(start_range, end_range), Hincrement, False)"
Next ptr
End Sub
答案 0 :(得分:0)
作为评论的后续内容,这个有效:
Worksheets("Sheet1").Cells(ptr, out).Formula = "=VLookup(""" & val & """,'" & sheetname1 & "'!" & Worksheets(sheetname1).Range(start_range, end_range).Address & "," & Hincrement & ", False)"