0

I found the below code which I have modified slightly for my needs. The issue I'm having is it doesn't do exactly what I'd like. Specifically, I have a drop down menu in A1 of each sheet with the names of the three sheets, Shipping, Orders, and Inventory in my workbook. What I'm trying to accomplish is whenever a user selects a drop down menu item regardless of the sheet they are working in, the relevant sheet is shown and the other two are hidden.

The below code works, but only if all three sheets have the same sheet name in the drop down selected, which becomes untenable when two sheets get hidden. I'm not exactly sure how to overcome this, but hopefully someone here who is much better at this than I am will have some advice.

Current VB Code:

Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Value = "Shipping" Then
        Sheets("Shipping").Visible = True
        Sheets("Orders").Visible = False
        Sheets("Inventory").Visible = False

    ElseIf Target.Value = "Orders" Then
        Sheets("Orders").Visible = True
        Sheets("Shipping").Visible = False
        Sheets("Inventory").Visible = False

    ElseIf Target.Value = "Inventory" Then
        Sheets("Inventory").Visible = True
        Sheets("Shipping").Visible = False
        Sheets("Orders").Visible = False

    End If
End Sub

2 Answers 2

1

Here is your code adapted for flexibility. This will hide any sheet that does not equal your target value, and unhide the sheet that DOES equal your target value.

Private Sub Worksheet_Change(ByVal Target As Range)

Dim ws As Worksheet
Dim x As Worksheet
Set x = Excel.ActiveSheet

For Each ws In Excel.ActiveWorkbook.Worksheets
    If Trim(ws.Name) <> Trim(Target.Value) and ws.Name <> x.Name Then
        ws.Visible = xlSheetHidden
        Else
        ws.Visible = xlSheetVisible
    End If
Next ws

End Sub

If you are wondering about the Trim() command, it removes leading and trailing spaces from the string value. Those are sometimes hard to spot in sheet names :)

Edit

I added the ws.Name <> x.Name part of the if statement to make sure the current sheet (aka the sheet on which the drop-down control is located) remains visible.

Sign up to request clarification or add additional context in comments.

Comments

0

Start with all three sheets visible and use this code in all three sheets:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Value = "Shipping" Then
    Sheets("Shipping").Visible = True
    Sheets("Shipping").Select
    Sheets("Orders").Visible = False
    Sheets("Inventory").Visible = False

ElseIf Target.Value = "Orders" Then
    Sheets("Orders").Visible = True
    Sheets("Orders").Select
    Sheets("Shipping").Visible = False
    Sheets("Inventory").Visible = False

ElseIf Target.Value = "Inventory" Then
    Sheets("Inventory").Visible = True
    Sheets("Inventory").Select
    Sheets("Shipping").Visible = False
    Sheets("Orders").Visible = False

End If
End Sub

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.