Question

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
Was it helpful?

Solution

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)"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top