mardi 7 janvier 2020

Add radio buttons and checkboxes to an Excel worksheet with VB

For a friend of mine who thinks of living here in switzerland, I want to make an excel Document with all the specific informations about how much money everything costs, etc.

I chose Excel for this Task, because its easy to use and almost everyone has it. Now for what I want to do with the Excel Document is as following:

  • I have a Main Worksheet which contains all the information in one place where my friend only has to look. This sheet has "Radio Buttons" and "Checkboxes" to change which costs should be used and which not.
  • Several other Worksheets which will then contain the information on specific topics like "Transportation", "subscriptions", etc. Those worksheets are not supposed to be changed later by him.

Now doing those things is easy, I sum up the costs in the seperate worksheets, insert basic Excel sum formulas and voila. Every seperate Worksheet has the same design, where as the Value regarded as "total" will be the value used in the sum forumla, to sum up all the costs necessary for a living.

How the excel sheet with transportation will look like

Now instead of having to go to every Worksheet, look at the possibilities and then change it by hand, I want to add radio buttons and checkboxes in the "Main" Worksheet. There, one can work with them and doesnt have to change Worksheets all the time. Keep in mind, they dont have to be fancy, my idea would be to work with colors where, if one of the boxes becomes green, that value will be chosen and the other boxes will be red (Or green means checked and red unchecked in regards to checkboxes). As seen in the screenshot, the value under the "total" is where the function will set its data, either by choosing one of the other X sums with the radio buttons (like 315 under car or 5 under bicycle) or by summing up multiple chosen with the checkboxes.

Now I tried to do that with the VBA Editor inside of Excel, yet I failed and cant figure out what the problem is or how I can attempt to fix it. Im used to programm with C# (.Net mainly and only since 2 years) but Visual Basic is completly new to me. This is what I came up with until now:

Function SwitchTransportationCosts()
    If Worksheets("Main").range("D5").Interior.Color = RGB(0, 176, 80) Then
        SwitchTransportationCosts = Worksheets("Transportation").range("F4").Value
        Worksheets("Main").range("F5").Interior.Color = RGB(256, 0, 0)
        Worksheets("Main").range("H5").Interior.Color = RGB(256, 0, 0)
    ElseIf Worksheets("Main").range("F5").Interior.Color = RGB(0, 176, 80) Then
        SwitchTransportationCosts = Worksheets("Transportation").range("J4").Value
        Worksheets("Main").range("D5").Interior.Color = RGB(256, 0, 0)
        Worksheets("Main").range("H5").Interior.Color = RGB(256, 0, 0)
    ElseIf Worksheets("Main").range("H5").Interior.Color = RGB(0, 176, 80) Then
        SwitchTransportationCosts = Worksheets("Transportation").range("N4").Value
        Worksheets("Main").range("F5").Interior.Color = RGB(256, 0, 0)
        Worksheets("Main").range("D5").Interior.Color = RGB(256, 0, 0)
    Else
        Worksheets("Main").range("D5").Interior.Color = RGB(0, 176, 80)
        SwitchTransportationCosts = Worksheets("Transportation").range("F4").Value
        Worksheets("Main").range("F5").Interior.Color = RGB(256, 0, 0)
        Worksheets("Main").range("H5").Interior.Color = RGB(256, 0, 0)
    End If
End Function

(I didnt use any variables or parameters yet since I cant exactly get behind them yet)

When I debug that code, it shows me that the value of "SwitchTransportationCosts" gets set by the value of the total costs as seen in the screenshot. Yet still it shows me "#VALUE" whenever it gets calculated. The code was my attempt at the radio button, the function with the chechboxes would work the same, just instead of setting only one value ("SwitchTransportationCosts") I would make a variable and add everything that has been checked to that variable and use that variable.

As said, im completly new to all this and have no idea where to look for in the code for problems and how VB works (even took me 5 min to figure out how the debugger works). Question would now be: ist that the correct approach on how to programm a similar function as radio buttons in Excel and if yes, whats the problem there I seem to be missing?

Thank you for taking your time to read my question .




Aucun commentaire:

Enregistrer un commentaire