VBA to Populate Table with Dynamic Formulas

GMLee

New Member
Joined
Jul 23, 2012
Messages
21
Excel 2007
Windows XP

Hello

I need to populate a table with two formulas. The numbers of rows and number of column will vary.
I've got my code up to the point where the table is outlined:

Employee NameJob Title7/22/127/23/127/24/12
Employee1
Employee2
Employee3
Employee4
Employee5

<tbody>
</tbody>

The formulas I need to input are: (my apologies for the clunky formulas)
Employee NameJob Title7/22/127/23/127/24/12
Employee1=VLOOKUP(D2,Job_Title!A:B,2,FALSE)=IFERROR(VLOOKUP(CONCATENATE($D2,F$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D2,G$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D2,H$1),$A:$B,2,FALSE),"OFF")
Employee2=VLOOKUP(D3,Job_Title!A:B,2,FALSE)=IFERROR(VLOOKUP(CONCATENATE($D3,F$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D3,G$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D3,H$1),$A:$B,2,FALSE),"OFF")
Employee3=VLOOKUP(D4,Job_Title!A:B,2,FALSE)=IFERROR(VLOOKUP(CONCATENATE($D4,F$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D4,G$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D4,H$1),$A:$B,2,FALSE),"OFF")
Employee4=VLOOKUP(D5,Job_Title!A:B,2,FALSE)=IFERROR(VLOOKUP(CONCATENATE($D5,F$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D5,G$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D5,H$1),$A:$B,2,FALSE),"OFF")
Employee5=VLOOKUP(D6,Job_Title!A:B,2,FALSE)=IFERROR(VLOOKUP(CONCATENATE($D6,F$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D6,G$1),$A:$B,2,FALSE),"OFF")=IFERROR(VLOOKUP(CONCATENATE($D6,H$1),$A:$B,2,FALSE),"OFF")

<tbody>
</tbody>

With a copy paste values at the end the result should look like this:
Employee NameJob Title7/22/127/23/127/24/12
Employee1Admin09:30-20:0009:30-20:0009:30-20:00
Employee2Admin12:30-23:0012:30-23:0012:30-23:00
Employee3Admin08:30-17:4509:15-17:4508:30-17:45
Employee4Admin11:30-20:0014:30-23:0014:30-23:00
Employee5Admin14:30-23:0014:30-23:0014:00-22:30

<tbody>
</tbody>

JoeMo answered a question for me yesterday to copy a formula down one column for a varying number of rows. I've been trying to make modifications to that code to complete this new task. What I have so far is
Code:
Dim lastRow As Long, rd As Long, lastcol As Long, ws As Worksheet, cd As Long

Set ws = ActiveSheet


    lastRow = ws.Range("D" & Rows.Count).End(xlUp).Row
    lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
   
    


    With ws
    
        For rd = 2 To lastRow
            If Len(Trim(.Range("d" & rd).Value)) <> 0 Then _
            .Range("e" & rd).Formula = "=VLOOKUP(RC[-1],Sheet3!C[-4]:C[-3],2,FALSE)"
            .Range("f" & rd).Formula = "=VLOOKUP(CONCATENATE(RC[-2],2C),C[-5]:C[-4],2,FALSE)"
        Next rd
        'do until
    End With

Which really I've only been able to figure out how to count the number of column I have. The column in the table start at D.

Thanks for reading!
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This is what I've used in the past with much success
Code:
Dim source As Range
Dim fill As Range
Dim ws1 As Worksheet

Set ws1 = Worksheets("Sheet1")

Set source = ws1.Range("A1")
            Set fill = ws1.Range("A1:A5")
            ws1.Range("A1").Formula = "=your formula here"
            source.AutoFill Destination:=fill
 
Upvote 0
Thank you bibleguy125

The code you posted is almost what I am looking for except I need the macro to be able to fins and define the range as it will differ each time. I have managed to get the second formula working in column f now I just need to fill to the last column.

Code:
Dim lastRow As Long, rd As Long, lastcol As Long, ws As Worksheet, cd As Long


Set ws = ActiveSheet


    lastRow = ws.Range("D" & Rows.Count).End(xlUp).Row
    lastcol = Cells(1, Columns.Count).End(xlToLeft).Column
   
    


    With ws
    
        For rd = 2 To lastRow
            If Len(Trim(.Range("d" & rd).Value)) <> 0 Then _
            .Range("e" & rd).Formula = "=VLOOKUP(RC[-1],Job_Title!C[-4]:C[-3],2,FALSE)"
            .Range("f" & rd).Formula = "=iferror(VLOOKUP(CONCATENATE(RC4,R1C),C1:C2,2,FALSE),""OFF"")"
        Next rd
        
    End With
 
Upvote 0
Try this.
Code:
Set ws = ActiveSheet

LastRow = ws.Range("D" & Rows.Count).End(xlUp).Row

LastCol = ws.Cells(1, Columns.Count).End(xlToLeft).Column

Set rng = ws.Range("E2", ws.Cells(LastRow, LastCol))

rng.Columns(1).Formula = "=VLOOKUP(D2, Job_Title!A:B, 2, 0)"

rng.Offset(, 1).Resize(, rng.Columns.Count - 1).Formula = "=IFERROR(VLOOKUP(CONCATENATE($D2,F$1),$A:$B,2,FALSE),""OFF"")"
 
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,929
Members
449,274
Latest member
mrcsbenson

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