Passing variables...not understanding...

BrianExcel

Well-known Member
Joined
Apr 21, 2010
Messages
975
I am using the following code which when run displays an image from a set of three.

Code:
Sub optInkColor_Click()
    ShowImageInk "imgInkColor"
End Sub

Sub optInkBW_Click()
    ShowImageInk "imgInkBW"
End Sub

Sub optInkMix_Click()
    ShowImageInk "imgInkMix"
End Sub

Sub ShowImageInk(whichOneink As String)

    Dim objImage As Image
    Dim cntl As Control

    For Each cntl In Me.frmInk.Controls
        If Left(cntl.Name, 3) = "img" Then
            cntl.Visible = False
        End If
    Next cntl

    Set objImage = Me.Controls(whichOneink)
    objImage.Visible = True

    Set objImage = Nothing
End Sub

I am trying to better understand how to pass variables to simplify code, and so I am trying to re-write the above code using different scenarios. For example: I have three buttons, and one label. Each button is named uniquely such as btn1, btn2, btn3 with different captions. What I want to do is display the caption of the button selected in the label, but I can't figure out what I am doing wrong.

Could someone please help with the requested code above? Or at the very least try to explain (for Dummies) how to pass variables? I know this should be simple but I am struggling with it...
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.

Forum statistics

Threads
1,224,599
Messages
6,179,831
Members
452,946
Latest member
JoseDavid

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