0
votes

I'm working on dynamic vlookup in single workbook. There are multiple sheets. Firstly I've been using Worksheet function to get out the column number through column name ("header name"). It gives exactly column number while working.

But when I tried to set range in below code it give's run time error 1004.

Set srchRange = OB1.Worksheets(7).Range("wsLCNo3,wsLCNo4").EntireColumn.Select

I want to sent search range in above code where I want to select entire column through range: just for example :
Set srchRange = OB1.Worksheets(7).Range("A:Z")

Below is the code for reference

wsLR = OB1.Worksheets(2).Cells(Rows.Count, 1).End(xlUp).Row

        wsLCNo1 = WorksheetFunction.Match("hs", OB1.Worksheets(2).Range("2:2"), 0) ' gives reference to column no
        wsLCNo2 = WorksheetFunction.Match("Schedulescope", OB1.Worksheets(2).Range("2:2"), 0) ' gives reference to column no
        wsLCNo3 = WorksheetFunction.Match("HW ", OB1.Worksheets(7).Range("2:2"), 0) ' gives reference to column no
        wsLCNo4 = WorksheetFunction.Match("SW scope", OB1.Worksheets(7).Range("2:2"), 0) ' gives reference to column no
        diff = (wsLCNo4 - wsLCNo3) + 1


        OB1.Worksheets(7).Activate



        Set srchRange = OB1.Worksheets(7).Range("wsLCNo3,wsLCNo4").EntireColumn.Select



         OB1.Worksheets(2).Activate
        For x = 3 To wsLR

            Set dynamic_lookup_value1 = Cells(x, wsLCNo1)
            OB1.Worksheets(7).Activate
            Set dynamic_lookup_value2 = Cells(x, wsLCNo3)
            Set dynamic_lookup_value3 = Cells(x, wsLCNo4)
            c1 = dynamic_lookup_value2.Address(0, 0)
            c2 = dynamic_lookup_value3.Address(0, 0)

            OB1.Worksheets(2).Activate


              Cells(x, wsLCNo2).Formula = "=VLOOKUP(" & _
                                        dynamic_lookup_value1.Address(0, 0) & _
                                        "," & _
                                        srchRange.Address(External:=True) & _
                                        "," & diff & ", FALSE)"

             Next x
1
Range(wsLCNo3 & ":" & wsLCNo4)Tim Williams

1 Answers

1
votes

First, make sure the values of the wsLCNoX are what you expected, then, use:

Set srchRange = OB1.Worksheets(7).Columns(wsLCNo3).Resize(,wsLCNo4)