Forum Discussion

Rashaud35's avatar
Rashaud35
Copper Contributor
Nov 18, 2022
Solved

VBA to output Product of two cells if checkbox is "Checked" but stay a zero value if "Unchecked"

Hello. I've placed some checkboxes into my Excel spreadsheet and would like to apply a Macro to it that will do the following:

 

If checkbox is CHECKED and linked to cell E7 in format control, then multiply the values of cells B7 and C7 and output that answer in cell E7. But if the checkbox is NOT CHECKED, then E7 should equal 0.

(For example: * if checkbox is placed in cell D7 and is Checked* and the contents of cell B7 is 10 and the contents of cell C7 is 2 - then E7 should display the answer, 20. But if there's NO CHECK in the box, the value should stay Zero. I want to remove the text name on the Checkbox (ie: Check box 18) - I can remove the text, but not sure how to do that and still somehow apply Macros to it.

 

Any idea how I would put this in the Module?

  • mtarler's avatar
    mtarler
    Nov 19, 2022

    Rashaud35  it doesn't matter where the checkbox is located.  you want to link to some 'other' cell and not the one you want the formula in.  so in the attached I changed the 1st row to be 'linked' to D7 so in E7 is the formula =B7*C7*D7

    You can then "hide" the true/false text by changing the font color to be same as fill color (you could also use custom number formatting if needed)

     

  • mtarler's avatar
    mtarler
    Silver Contributor
    A) you don't need a macro for this. in E7 you have =D7*C7*B7 and when the checkbox is cleared it is false and will multiply like 0 and if true it will multiply like 1
    B) the macro call out shouldn't matter if you delete the display name. so if you right click and say edit text and delete all the text and then right click and say assign macro it will still say CheckBox1_Click because the check box is still named that but the label (display) is removed.
    • Rashaud35's avatar
      Rashaud35
      Copper Contributor

      mtarlerThank you so much for helping me, I greatly appreciate it. However, it still does not seem to work. If cell D7 that contains the checkbox is checked, then the "linked" cell (E7) continues to be TRUE or FALSE and does not keep "=D7*C7*B7" inside the cell once checked. I think that's why I was assuming a Macro would work here. I've attached an example spreadsheet to show what's going on. When the box is checked, I'd like "FALSE" in cell E7 to be a Zero, but if "TRUE" I'd like the output to be Product of B7 times C7.

      • mtarler's avatar
        mtarler
        Silver Contributor

        Rashaud35  it doesn't matter where the checkbox is located.  you want to link to some 'other' cell and not the one you want the formula in.  so in the attached I changed the 1st row to be 'linked' to D7 so in E7 is the formula =B7*C7*D7

        You can then "hide" the true/false text by changing the font color to be same as fill color (you could also use custom number formatting if needed)

         

Resources