Please help with compressing multiple Private Subs

heyitslujan

New Member
Joined
Jul 29, 2015
Messages
7
Hi All,

Thanks for checking out my question. I've been asked to create a button that only appears based on 3 of 4 values available from a dropdown box(K6). The values are "REMOVE", "ADD ON" and "REPLACE EXISTING". Here's what I have so far. I can get it to work for one value like, "REMOVE", but do not know how to write this to work for all 3 approved values.

Private Sub Worksheet_Change(ByVal Target As Range)
'ActiveX button
If Range("K6").Value = "ADD ON" Then
ActiveSheet.CommandButton1.Visible = True
Else
ActiveSheet.CommandButton1.Visible = False
End If
End Sub
 
You can change your If statement as

Code:
If Range("K6").Value = "ADD ON" Or Range("K6").Value = "REMOVE" Or Range("K6").Value = "REPLACE EXISTING" Then
 ' Do work
End If
 
Upvote 0
Thank you sir! Worked great. Though, I did have to remove the "'Do work" statement for some reason. Here's what ended up working.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)'ActiveX button
If Range("K6").Value = "ADD ON" Or Range("K6").Value = "REMOVE" Or Range("K6").Value = "REPLACE EXISTING" Then
 ActiveSheet.CommandButton1.Visible = True
Else
    ActiveSheet.CommandButton1.Visible = False
End If
End Sub
You can change your If statement as

Code:
If Range("K6").Value = "ADD ON" Or Range("K6").Value = "REMOVE" Or Range("K6").Value = "REPLACE EXISTING" Then
 ' Do work
End If
 
Upvote 0
My bad. That was just a VBScript comment - a comment in VBScript is preceded by a single quote mark. I should've mentioned that.
 
Upvote 0
Wait, that's like some cool cue to proceed with what I orginally had? Aw man! Did I blow that one. One day I'll be one of the cool kids, one day
:nervous:
My bad. That was just a VBScript comment - a comment in VBScript is preceded by a single quote mark. I should've mentioned that.
 
Upvote 0
Well a comment is basically a non-executable line in your code. You can document your code statements using comments. That comes handy when you come back to a complicated code after a long time or if you hand over the code to someone else.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)'ActiveX button
[COLOR=#008000]    ' Code to show or hide the button
    ' Approved values to show button are "REMOVE", "ADD ON" and "REPLACE EXISTING"
[/COLOR]
    If Range("K6").Value = "ADD ON" Or Range("K6").Value = "REMOVE" Or Range("K6").Value = "REPLACE EXISTING" Then
[COLOR=#008000]        ' Approved keyword in the cell; Show the button [/COLOR]
        ActiveSheet.CommandButton1.Visible = True
    Else
[COLOR=#008000]        ' The cell does not have approved keyword; Hide the button [/COLOR]
        ActiveSheet.CommandButton1.Visible = False
    End If
End Sub
 
Upvote 0

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