VBA code tweak needed

londa_vba

Board Regular
Joined
May 11, 2023
Messages
61
Office Version
  1. 365
Platform
  1. Windows
Hello
I have the following code that pairs to a drop down on the sheet. The code works when "plate" is selected and when the dropdown is blank but not when "tubes".

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

If [Amp_performed] = "Plate" Then
Sheets("GF Amp Plate Layout").Visible = True
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbBlack
Else
Sheets("GF Amp Plate Layout").Visible = False
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbWhite
End If

If [Amp_performed] = "Tubes" Then
Sheets("GF Amp Plate Layout").Visible = False
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbWhite
Else
Sheets("GF Amp Plate Layout").Visible = True
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbBlack
End If

If [Amp_performed] = "" Then
Sheets("GF Amp Plate Layout").Visible = False
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbWhite
Else
Sheets("GF Amp Plate Layout").Visible = True
Range("a8:a33,a39:a64,a70:a95,a101:a126").Font.Color = vbBlack
End If
End Sub
 
Last edited:

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
I think the issue is that you have multiple IF statements with ELSE clauses.
So in all three cases of your IF...THEN...ELSE, something is running (either the main check or the ELSE).
You do not want that, you only want one thing to run.

I think you would be better off here using a CASE statement. See: Using Select Case statements (VBA)
It would be structured something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case [Amp_performed]
        Case "Plate"
            'Code here that runs when value is "Plate"
        Case "Tubes"
            'Code here that runs when value is "Tubes"
        Case ""
            'Code here that runs when value is empty
        Case Else
            'Code here that runs when value is any other string
    End Select
    
End Sub
Just filling in the appropriate code to run for each criteria.
That ensures only exactly one code section will be run.
 
Upvote 1
Solution
I think the issue is that you have multiple IF statements with ELSE clauses.
So in all three cases of your IF...THEN...ELSE, something is running (either the main check or the ELSE).
You do not want that, you only want one thing to run.

I think you would be better off here using a CASE statement. See: Using Select Case statements (VBA)
It would be structured something like this:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Select Case [Amp_performed]
        Case "Plate"
            'Code here that runs when value is "Plate"
        Case "Tubes"
            'Code here that runs when value is "Tubes"
        Case ""
            'Code here that runs when value is empty
        Case Else
            'Code here that runs when value is any other string
    End Select
   
End Sub
Just filling in the appropriate code to run for each criteria.
That ensures only exactly one code section will be run.
Works perfectly. Thank you for the explanation and the solution!
 
Upvote 0
You are welcome! Glad I was able to help!

The "Case" statement is really a nice feature when you are checking a single item for multiple different options/values.
It sure beats writing a bunch of If...Then...Else statements, and is much shorter!
 
Upvote 0

Forum statistics

Threads
1,215,440
Messages
6,124,882
Members
449,193
Latest member
PurplePlop

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