VBA code to enter result of formula for all rows in 3 columns

Hans_

Board Regular
Joined
Feb 7, 2009
Messages
71
Hi all,

I have 3 columns in a sheet that contain formulas but i want to replace this with a value from a vba code as my sheet contains more than 10.000 lines. The formulas start at row 2. Row 1 is a header row.

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]timePeriod (column A)[/TD]
[TD]timeTable (column B)[/TD]
[TD]timeCount (column C)[/TD]
[/TR]
[TR]
[TD]=VLOOKUP("W"&D2;tblTime!$A:$N;4;FALSE)[/TD]
[TD]=COUNTIF(tblTime!D:D;tblWeekData!A2)[/TD]
[TD]=IF(SUMPRODUCT(($G$1:$G2=G2)*($A$1:$A2=A2)*($D$1:$D2=D2))>1;0;1)[/TD]
[/TR]
</tbody>[/TABLE]

Usualy i would copy down the formula to the last line but i want the entire process automated and replaced by a hard value instead of the formula.

At the moment i have the following code that isn't working:

Code:
With ThisWorkbook.Sheets("tblWeekData")
    lastRow = .Cells(.Rows.Count, "D").End(xlUp).Row
End With


With ThisWorkbook.Sheets("tblWeekData").Range("A2:A" & lastRow)
    .Offset(0, 0).Formula = "=VLOOKUP(""W""&D2;tblTime!$A:$N;" & i & ";FALSE)"
    .Offset(0, 0).Value = .Value
    .Offset(0, 1).Formula = "=IF(SUMPRODUCT(($G$1:$G2=G2)*($A$1:$A2=A2)*($D$1:$D2=D2))>1;0;1)"
    .Offset(0, 1).Value = .Value
    .Offset(0, 2).Formula = "=COUNTIF(OFFSET(tblTime!A:A;0;" & i - 1 & ";tblWeekData!A2)"
    .Offset(0, 2).Value = .Value
End With

Any help would be appreciated.

Kind regards,
Hans
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
What isn't working? See if this helps (have made a few changes to your formula to make them calculate faster):
Code:
Dim x   As Long

With Application
    .ScreenUpdating = False
    .Calculation = xlManual
End With

With Sheets("tblWeekData")
    x = Sheets("tblTime").Cells(Rows.Count, 1).End(xlUp).row
    .Cells(2, 2).Formula = "=--NOT(SUMPRODUCT(($G$1:$G2=G2)*($A$1:$A2=A2)*($D$1:$D2=D2))>1)"
    .Cells(2, 3).Formula = "=COUNTIF(OFFSET(tblTime!$A$1:$A$" & x & ";0;" & i - 1 & ";tblWeekData!A2)"
    x = .Cells(.Rows.Count, 4).End(xlUp).row
    With .Cells(2, 1)
        .Formula = "=VLOOKUP(""W""&D2;tblTime!$A$1:$N$" & x & ";" & i & ";0)"
        .Resize(x - 1, 3).FillDown
        Parent.Calculate
        .Resize(x - 1, 3).Value = .Resize(x - 1, 3).Value
    End With
End With

With Application
    .ScreenUpdating = True
    .Calculation = xlAutomatic
End With
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,830
Messages
6,181,227
Members
453,025
Latest member
Hannah_Pham93

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