Link to home
Start Free TrialLog in
Avatar of Bright01
Bright01Flag for United States of America

asked on

Formatting code for Excel Forms

I have an  Excel form that I would like to do some conditional formatting on.  Because it is a form, I cannot use the Conditional Formatting tool.  Is there some code I could put into the form code that would identify if the number in the cell is -0- or Blank, the cell would be White and show no content.  If it were positive, it would be green and if negative it would be red.

Thank you in advance,

B1
Avatar of Subodh Tiwari (Neeraj)
Subodh Tiwari (Neeraj)
Flag of India image

What control on UserForm contains the number? Is it a textbox or label or what?
And how does that number populate into that control?
Avatar of Bright01

ASKER

It's a User Form.  It populates a Name Range.


Public Sub Paint_Dashboard()

    fmDashboard.dbcaseGMDollars = Format(caseMarginDollars, "$###,###,###")
    fmDashboard.dbbcGMDollars = Format(bcmarginDollars, "$###,###,###")
    fmDashboard.dbdeltaGMDollars = Format(casedeltamarginDollars, "$###,###,###")
   
    fmDashboard.dbcasePercentProfit = Format(caseProfitMargin, "##.#%")
    fmDashboard.dbbcPercentProfit = Format(bcProfitMargin, "##.#%")
    fmDashboard.dbdeltapercentprofit = Format(casedeltaProfitMargin, "##.#%")
   
    fmDashboard.dbcaseNetIncome = Format(caseNetIncome, "$###,###,###")
    fmDashboard.dbbcNetIncome = Format(bcNetIncome, "$###,###,###")
    fmDashboard.dbdeltaNetIncome = Format(casedeltaNetIncome, "$###,###,###")
   
    If ffNetCashFlow = 0 Then
        fmDashboard.dbdeltaNetCashFlow = "0$"
'        fmDashboard.dbbcNetCashFlow = "0$"
        Else
            fmDashboard.dbdeltaNetCashFlow = Format(ffNetCashFlow, "$###,###,###")
'            fmDashboard.dbbcNetCashFlow = "0$"

    End If
   
    fmDashboard.dbcaseROS = Format(caseROS, "##.#%")
    fmDashboard.dbbcROS = Format(bcROS, "##.#%")
    fmDashboard.dbdeltaROS = Format(casedeltaROS, "##.#%")
   
    fmDashboard.dbcaseROA = Format(caseROA, "##.#%")
    fmDashboard.dbbcROA = Format(bcROA, "##.#%")
    fmDashboard.dbDeltaROA = Format(casedeltaROA, "##.#%")
   
    fmDashboard.dbcaseROE = Format(caseROE, "##.#%")
    fmDashboard.dbbcROE = Format(bcROE, "##.#%")
    fmDashboard.dbdeltaROE = Format(casedeltaROE, "##.#%")
   
    If casedeltaEPS = 0 Then
        fmDashboard.dbcaseEPS = Format(caseEPS, "$##.#")
        fmDashboard.dbbcEPS = Format(caseEPS, "$##.###")
        fmDashboard.dbdeltaEPS = Format(casedeltaEPS, "$##.#")
        Else
            fmDashboard.dbcaseEPS = Format(caseEPS, "$##.##")
            fmDashboard.dbbcEPS = Format(bcEPS, "$##.##")
            fmDashboard.dbdeltaEPS = Format(casedeltaEPS, "$##.##")
    End If
   
End Sub
If the form is populating a cell then apply Conditional Formatting to the cell. An example workbook containing data and the userform might help
I can't use Conditional Formatting.  The object, in the Form is a Commandbutton and when I bring the form up in the editor, I can select it, move it around, look at it's properties, or go to the actual code behind it.  But I cannot use Conditional Formatting because it's not a cell.  Any ideas on how to control the colors based on +, 0, "" or -?

B.
Attach an example workbook. Do you want the button to change colour?
Roy,

Thanks for the help.  I've done my best to make up a mock up to show you what I'm struggling with.  Take a look.  Again, my issue is with formatting a Form Object.

Thank you,

B.
Conditional-format.xlsm
Is this what you mean?
Conditional-format.xlsm
I think this will be better
Conditional-format.xlsm
Roy,

Can't tell anything has changed.  I downloaded your update but can't get any color changes on the form; nor the form to display.

In order to see how this works, you have to link up the Dashboard button to display the Dashboard. Then the formula that is in Profit has to be added (Income before taxes - Income taxes) = Profit.  This number has to then populate the Profit field in the Form.  It is there, in the form where the number should be formatted either green, red or white (if -0-).    

Thanks again for the help here.  Sorry it wasn't clear..... but I had to mock it up to send it to you.

B.
The form changes the back colour of the TextBox based on the Profit showing in the sheet. It is coded in the Initialize event of teh UserForm.
How do I see that?   The User Form won't come up when you click on the Form button?  The Form isn't suppose to change the color on the Sheet.  The Sheet (+,- or 0) should change the color in the Text box on the Form when the Form is displayed.

B.
Open the VB Editor and run the form from there. It's only an example.

You have actually changed the name of your existing button so the code behind it isn't running.
Conditional-format.xlsm
Roy,

I changed the name....big difference.  I see what you did and that's great.  Can you tell me which Macro you changed in order to get the result?

B.
ASKER CERTIFIED SOLUTION
Avatar of Roy Cox
Roy Cox
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Testing now.
Nice Job Roy!  Thank you very much.....always a pleasure working with you.

B.
Pleased to help you.