1
votes

Sorry if I screw up some formatting but this is my first question. I am trying to make an excel macro setting a cell's value to a VLookup Function. It accepts input from a userForm to determine the range, sheet for the Vlookup to work off of, output column, and the lookup value. I get a multitude of errors when trying to work with this. Any help would be greatly appreciated!

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
1
I get a multitude of errors - can you elaborate? Also try Worksheets("Sheet1").Range(ptr, out).Formula = "=VLookup(" & val & ",'" & sheetname1 & "'!" & Worksheets(sheetname1).Range(start_range, end_range).Address & "," & Hincrement & ", False)"Dmitry Pavliv
I have been receiving Run-time error "'1004': Application-defined or object-defined error." The error occurs in the VLookup statement. Also tried the syntax you provided, but unfortunately received same errorlefeve111
try to add line Debug.Print "=VLookup(" & Val & ",'" & sheetname1 & "'!" & Worksheets(sheetname1).Range(start_range, end_range).Address & "," & Hincrement & ", False)" . What output it produces (press CTRL+G to see it)?Dmitry Pavliv
=VLookup(Miles Matheson,'Sheet2'!$A$2:$E$7,2, False) That is the output, and it is the correct VLookup information...lefeve111
ok, try Worksheets("Sheet1").Cells(ptr, out).Formula = "=VLookup(""" & val & """,'" & sheetname1 & "'!" & Worksheets(sheetname1).Range(start_range, end_range).Address & "," & Hincrement & ", False)"Dmitry Pavliv

1 Answers

0
votes

As follow up from comments, this one works:

Worksheets("Sheet1").Cells(ptr, out).Formula = "=VLookup(""" & val & """,'" & sheetname1 & "'!" & Worksheets(sheetname1).Range(start_range, end_range).Address & "," & Hincrement & ", False)"