populating table w/vlookup using VBA

Hlatigo

Well-known Member
Joined
Jun 3, 2002
Messages
677
Hello all, i have a table i want to populate using to values for vlooup. problem i am having is keeping these values stationary when they need to and dynamic when they need to. this is the code i was trying to use.

as the for each loops down the header should stay stantionary but the lookup value on column C should go down in rows. once that column is done, the column lookup should stay parallal and start at top but the header lookup should have moved over with the for each. I hope this makes sense :(

Code:
Sub greeet()
For Each cell In Range("D10:J27")
        If IsEmpty(cell.Value) Then
            cell.Value = WorksheetFunction.VLookup(cell.Offset(, -4), _
            Sheets("sheet1").Range("L6:N29"), 3, False)
        End If

    Next cell
End Sub
Book2.xls
CDEFGHIJKLMN
62004012004022004032004042004052004062004072004011/1/20081
71/1/20082004011/2/20082
81/2/20082004011/3/20083
91/3/20082004011/4/20084
101/4/20082004011/5/20085
111/5/20082004011/6/20086
121/6/20082004011/7/20087
131/7/20082004011/8/20088
141/8/20082004011/9/20089
151/9/20082004011/10/200810
161/10/20082004011/11/200811
171/11/20082004011/12/200812
181/12/20082004011/13/200813
191/13/20082004011/14/200814
201/14/20082004011/15/200815
211/15/20082004011/16/200816
221/16/20082004011/17/200817
231/17/20082004011/18/200818
241/18/2008200402
25200403
26200404
27200405
28200406
29200407
Sheet1
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
i found this piece of code but I am having issues seeing if this will really help me and i think it can but not sure.

Code:
Set findrange = datasheet.Range("a:b") 
 
rangend = Cells(lets + 1,  nums + 1).Address 
Set plotrange =  Worksheets("scorecard").Range("b2:" & rangend) 
 
For Each mycell In plotrange 
    findstring = Cells(mycell.Row, 1) & Cells(1, mycell.Column) 
    mycell.Formula = "=vlookup(" & findstring & "," & findrange & ",2)" 
Next mycell
 
Upvote 0
ok, so this is the code that will work and do what i need it to do but i was really hoping to see how to use vlookup or indexmatch in VBA since I am under the impression that it would work faster and/or have less lines of code.

Code:
Dim wbModel As Workbook
Dim rngData As Range, rngTable As Range
Dim dblDataCounter As Double
Dim dblRowCounter As Double, dblColumnCounter As Double
Dim vntRowData As Variant, vntColumnData As Variant

'set the workbook and ranges
Set wbModel = Application.ThisWorkbook
Set rngData = wbModel.Sheets(1).Range("N6")
Set rngTable = wbModel.Sheets(1).Range("C9")

dblRowCounter = 1

'loop through all the rows
Do Until IsEmpty(rngTable.Offset(dblRowCounter, 0))
    dblColumnCounter = 1
    'loop through all the columns
    Do Until IsEmpty(rngTable.Offset(0, dblColumnCounter))
        'set the variable
        vntColumnData = rngTable.Offset(0, dblColumnCounter)
        vntRowData = rngTable.Offset(dblRowCounter, 0)
        dblDataCounter = 0
        'search the data for the variables above
        Do Until IsEmpty(rngData.Offset(dblDataCounter, 0)) Or _
        (rngData.Offset(dblDataCounter, 0) = vntColumnData And _
        rngData.Offset(dblDataCounter, 1) = vntRowData)
            dblDataCounter = dblDataCounter + 1
        Loop
        'check if found the data
        If IsEmpty(rngData.Offset(dblDataCounter, 0)) Then
        'do nothing
        Else
            rngTable.Offset(dblRowCounter, dblColumnCounter) = rngData.Offset(dblDataCounter, 2)
        End If
        'next column
        dblColumnCounter = dblColumnCounter + 1
    Loop
    'next row
    dblRowCounter = dblRowCounter + 1
Loop
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,717
Members
448,985
Latest member
chocbudda

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top