User Variable passed to Conditional Formatting with VBA

brainstain

New Member
Joined
Feb 15, 2015
Messages
6
Somewhat newbie here, Hello,

I made a ledger sheet that includes a daily running balance like a checkbook, and wanted to have the
ability to highlight cells if the value falls below a minimum, BUT if I want to change that conditional minimum value on the fly, I want to be able to enter a number in a separate cell, click a button (which I know how to make a button to run a macro) and have that number passed to a variable that goes into the macro of the Conditional Formatting macro I recorded and allow the minimum balance to change based on user input.

This is a 'part' of the Cond. Format. I recorded with the value of 'less than 50' that I want to be able to change by entering another number in a cell, click a button, and it passes then new value to that variable.

Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=50"

So Im guessing the variable would be within the quotes area?
And need to pass the variable some how, (value will only be whole numbers).
I know how to have the macro read the cell of user input, but dont know how to properly assign that value to a proper variable (long, integer...) then pass it to the macro and change it from the original input of 'less than 50'.

Suggestions please?

Thanks

David
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
You will want to use something like this:

Code:
[COLOR=#0000ff]Sub[/COLOR][COLOR=#333333] Test()

[/COLOR][COLOR=#0000ff]     Dim[/COLOR][COLOR=#333333] YourVar [/COLOR][COLOR=#0000ff]As Integer[/COLOR][COLOR=#333333]

[/COLOR][COLOR=#008000]     'The value you want the procedure to take goes in Cell A1[/COLOR][COLOR=#333333]
     YourVar = Range("A1").Value

     Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _[/COLOR]
[COLOR=#333333]     Formula1:="=" & YourVar

[/COLOR][COLOR=#0000ff]End Sub[/COLOR]

Change the variable type for your needs. Link this sub to the button you mentioned.
 
Upvote 0
Thank you very much MrMMickle1, I will try that right now, needed it for a project and will let you know, thanks.
 
Upvote 0
MrMMickle1,

One thing I didnt mention, is that all the cells that I have the Cond. Formula used on,
have formulas, since they are a running balance like a checkbook.
This did seem to work the first time or two, but then when I changed the value in the cell (AW17),
it didnt work as accurate.

Will this work correctly with formulas, or just values in those cells, and
was it meant to be part of the macro for condit. format. or a separate run routine,
and then the user input passed to that condit. format routine?

Below is what Im running from a button, it highlights some, but not all accurate.
Thanks for your valuable input.


Private Sub CommandButton21_Click()
'
' MinBal Macro
'
Dim MinBal As Integer
MinBal = Range("AW17").Value
Range("BB3:BB64").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=" & MinBal
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub

brainstain
 
Upvote 0
One thing I just noticed, that its not clearing the previous condition.
I set the user input very high, then very low, and it kept all the conditions from the high value,
so maybe I need to clear the condition in the macro first, and rerun it with this condition?

UPDATE:

I just added the top 2 lines to clear the previous run, and it seems to be working, will keep you posted,
Thanks sir.

Private Sub CommandButton21_Click()
'
' MinBal Macro

'

Range("BB3:BB64").Select
Selection.FormatConditions.Delete
Dim MinBal As Integer
MinBal = Range("AW17").Value
Range("BB3:BB64").Select
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLess, _
Formula1:="=" & MinBal
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
End Sub
 
Last edited:
Upvote 0
I'm having an issue getting the code to work the way you suggested to use the variable. I keep seeing a Run Time Error - Invalid procedure call or argument.

Sub Macro3()
'
' Macro3 Macro
'
'Dim test1234 As Double
'test1234 = 0.123
Dim test1234 As Integer
test1234 = 1
'
Selection.FormatConditions.Add Type:=xlCellValue, Operator:=xlLesser, _
Formula1:="=" & test1234
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 49407
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False


End Sub

Any Suggestions?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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