VBA - formula in filtered mode.

awsumchillicrab

Board Regular
Joined
Jan 30, 2011
Messages
56
Hi,

I'm trying to apply a formula to a range of visible cells after an autofilter, to be done in coding as a macro.

After applying filters
I tried:

With Range("Q2:Q" & LastRow).SpecialCells(xlCellTypeVisible)
.Formula = "=if(L2> H2+21,1,0)"
End With

but this is no good. Because in filtered mode, my first visible row is row6. This macro puts =if(L2> H2+21,1,0) in my cell instead of =if(L6> H6+21,1,0).
As row6 may not always be the first visible row in filtered mode, I don't want to hardcode a =if(L6> H6+21,1,0) into VBA.

I later tried the R1C1 kind of formula:

With Range("Q2:Q" & LastRow).SpecialCells(xlCellTypeVisible)
.FormulaR1C1 = "=if(R[-5]C > R[-9]C+21,1,0"
End With

But this gives me errors.

Need expert advise :)
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try

Code:
With Range("Q2:Q" & LastRow).SpecialCells(xlCellTypeVisible)
    .FormulaR1C1 = "=IF(RC[-5]>RC[-9]+21,1,0)"
End With
 
Upvote 0

Forum statistics

Threads
1,224,520
Messages
6,179,270
Members
452,902
Latest member
Knuddeluff

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