Help Changing Formula in VBA

markjdm

New Member
Joined
Nov 26, 2013
Messages
8
Hi Guys,

I tried searching for this but am not what function/feature it may be referred to.

I'm trying to do something similar to below but I can't seem to find the correct syntax to get it working. Very new to VBA so still finding my way around it.

What I'm tyring to do is change the format with the below VBA script but want to loop changing my formula in the condition similar to this when we enter data

"=Calculations!$I$2 on first cell then Calculations!$I$3 and so forth"



Sub CondFormat()'


Dim col As Integer
col = 6


For i = 13 To 50
Cells(i, col).Select
Selection.FormatConditions.add Type:=xlExpression, Formula1:= "=Calculations!$I$2)"
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.ThemeColor = xlThemeColorAccent2
.TintAndShade = 0.799981688894314
End With
Selection.FormatConditions(1).StopIfTrue = False

Next i

End Sub

Thanks! :)
 
.
.

Try this code instead:

Code:
Sub SetFormatCondition()
    
    Dim FCon As FormatCondition
    
    Set FCon = Range("F13:F50").FormatConditions.Add( _
        Type:=xlExpression, _
        Formula1:="=F13=Calculations!I2")
        
    With FCon
        .SetFirstPriority
        
        With .Interior
            .ThemeColor = xlThemeColorAccent2
            .TintAndShade = 0.8
        End With
        
        .StopIfTrue = False
    End With
    
End Sub
 
Upvote 0
Thanks mate, this works with formatting everything based on this formula. "=F13=Calculations!I2"

I was hoping to make something that for each cell it will change the formula too. Sorry for not being clear, about it.

I was hoping to do a conditional format similar to if I did it manually.

Cell F3 = format will be based on formula =Calculations!I2
Cell F4 = format will be based on formula = Calculations!I3
Cell F5 = format will be based on formula = Calculations!I4

Basicall as each cell goes down to be formatted, it will also change the conditional format formula with a new cell location?

Hope that makes sense.Ü

Cheers,
Mark
 
Upvote 0
Thanks mate, this works with formatting everything based on this formula. "=F13=Calculations!I2"

I was hoping to make something that for each cell it will change the formula too. Sorry for not being clear, about it.

I was hoping to do a conditional format similar to if I did it manually.

Cell F3 = format will be based on formula =Calculations!I2
Cell F4 = format will be based on formula = Calculations!I3
Cell F5 = format will be based on formula = Calculations!I4

Basicall as each cell goes down to be formatted, it will also change the conditional format formula with a new cell location?

Hope that makes sense.Ü

Cheers,
Mark


What you are asking for is achieved by the code I originally posted...
 
Upvote 0

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