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
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.
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
Related
I was trying to code in VBA to enable or disable the checkboxes for the mobile plan based on the brand selected as shown in the image.
The table on the left acts as an indicator of the availability of the mobile plans for the respective brands as indicated by "Y" or "N". For example, when the user selected "Apple" from the dropdown list, he is only allowed to tick the Mobile Plan 1.
The attempted solution is based on only one criteria, which is for "Apple" in this case. How do I enhance the coding so that when the user selects "Samsung", the status of the checkboxes will change accordingly?
I planned to declare a variable as an Integer to act as an column indicator (for eg, Apple = 2, Samsung = 3, Nokia = 4) and pass this integer to the each function "CheckBox_Change" and use the VLOOKUP function, but I get an error message:
"procedure declaration does not match description of event or
procedure having the same name"
while I was trying to do so.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$1" Then
CheckBox1.Value = False
CheckBox2.Value = False
CheckBox3.Value = False
Call CheckBox1_Change
Call CheckBox2_Change
Call CheckBox3_Change
End If
End Sub
Private Sub CheckBox1_Change()
If Range("B3").Value = "Y" Then
CheckBox1.Enabled = True
Else
CheckBox1.Enabled = False
CheckBox1.Value = False
End If
End Sub
Private Sub CheckBox1_Click()
If CheckBox1.Value = False Then
Range("H3").Value = ""
End If
End Sub
Private Sub CheckBox2_Change()
If Range("B4").Value = "Y" Then
CheckBox2.Enabled = True
Else
CheckBox2.Enabled = False
CheckBox2.Value = False
End If
End Sub
Private Sub CheckBox3_Change()
If Range("B5").Value = "Y" Then
CheckBox3.Enabled = True
Else
CheckBox3.Enabled = False
CheckBox3.Value = False
End If
End Sub
I'd go this way (explanations in comments):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$1" Then
With Range("B2:D2").Find(Target.Value, , xlValues, xlWhole) ' reference the cell with the proper Brand
CheckBox1.Enabled = .Offset(1).Value = "Y" 'set checkbox visibility to match referenced cell column corresponding value
CheckBox2.Enabled = .Offset(2).Value = "Y" ' same as above
CheckBox3.Enabled = .Offset(3).Value = "Y" ' same as above
End With
End If
End Sub
you could also loop between checkboxes "indexes" and avoid some code duplication:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$G$1" Then
Dim i As Long
With Range("B2:D2").Find(Target.Value, , xlValues, xlWhole) ' reference the cell with the proper Brand
For i = 1 To 3 ' loop from 1 to 3 (number of checkboxes)
Me.OLEObjects("CheckBox" & i).Enabled = .Offset(i).Value = "Y" 'set current checkbox visibility to match referenced cell column corresponding value
Next
End With
End If
End Sub
Basing on #QHarr's answer, you can reduce the code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "G1" Then
Me.CheckBox1.Enabled = (Target = "Apple")
Me.CheckBox2.Enabled = (Target = "Samsung")
Me.CheckBox3.Enabled = (Target = "Samsung")
End If
End Sub
Try the following. It uses Select Case to avoid repeating sections of code and having multiple subs.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "G1" Then
Select Case Target.Value
Case "Apple"
Me.CheckBox1.Enabled = True
Me.CheckBox2.Enabled = False
Me.CheckBox3.Enabled = False
Case "Samsung"
Me.CheckBox1.Enabled = False
Me.CheckBox2.Enabled = True
Me.CheckBox3.Enabled = True
Case Else
Me.CheckBox1.Enabled = False
Me.CheckBox2.Enabled = False
Me.CheckBox3.Enabled = False
End Select
End If
End Sub
I want to lock cell B20 and write "1" on it if I choose a certain option ("T") on the dropdown of B18. If I choose any other option, I want to be able to fill it normally without any limitations. Here is the best code I tried:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim ws As Worksheet
Set ws = ActiveSheet
If Target.Address = "$B$18" Then
ws.Unprotect
Application.EnableEvents = False
Target.Offset(1).Locked = Target.Value = "T"
If Target.Value = "T" Then
Target.Offset(1).Value = 1
Else
If Target.Offset(1).Value = 1 Then Target.Offset.Value = ""
End If
Application.EnableEvents = True
ws.Protect
End If
End Sub
This code is doing exactly what I want but its performing this on the cell B19 instead of B20.
Can somebody help me please?
There is a bunch of code and text all saying different things in your question, so I based this code on your comment and the text of your question.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$18" Then
ws.Unprotect
Application.EnableEvents = False
Target.Offset(2).Locked = Target.Value = "T"
If Target.Value = "T" Then
Target.Offset(2).Value = 1
Else
If Target.Offset(2).Value = 1 Then Target.Offset.Value = ""
End If
Application.EnableEvents = True
ws.Protect
End If
End Sub
I am new to VBA coding and have so far successfully managed to create a scoping sheet in a workbook which hides/unhides tabs based on workbooks users' responses to yes/no questions.
I need to further refine the workbook so that the yes/no responses provided in the scoping tab lead to the auto hiding of columns in other sheets. Using a previous thread on this website I used this code (obviously amended for my own cells refs) on one of the tabs:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$6" Then
Select Case Target.Value
Case Is = "Cast"
Columns("f").EntireColumn.Hidden = False
Columns("d").EntireColumn.Hidden = True
Columns("e").EntireColumn.Hidden = True
Case Is = "LDF"
Columns("f").EntireColumn.Hidden = True
Columns("d").EntireColumn.Hidden = False
Columns("e").EntireColumn.Hidden = False
Case Is = "Select ROV Type"
Columns("f").EntireColumn.Hidden = False
Columns("d").EntireColumn.Hidden = False
Columns("e").EntireColumn.Hidden = False
End Select
In B6, I have a formula (=Name) which pulls through from the scoping tab. While the above code works, it only does so where I manually enter the cell to re-pull through data... any hints on:
- linking through to the original scoping tab in my macro, bypassing the cell reference; and
- automating the column hides?
The easiest thing to do seems to edit your code like this, where needed:
Sheet2.Columns("f").EntireColumn.Hidden = False
Sheet2 is the sheet, where the columns should be hidden.
If I correctly interpreted your needs go like follows
In "ThisWorkbook" code pane place the following code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Application.EnableEvents = False
With Sheets("scoping sheet") '<== here you set which sheet you want to monitor
If .Range("B6") <> .Range("A1") Then '<== check if the "formula" cell changed its previous value, stored in the "echo" cell ("A1")
Select Case .Range("B6").Value
Case Is = "Cast"
.Columns("f").EntireColumn.Hidden = False
.Columns("d").EntireColumn.Hidden = True
.Columns("e").EntireColumn.Hidden = True
Case Is = "LDF"
.Columns("f").EntireColumn.Hidden = True
.Columns("d").EntireColumn.Hidden = False
.Columns("e").EntireColumn.Hidden = False
Case Is = "Select ROV Type"
.Columns("f").EntireColumn.Hidden = False
.Columns("d").EntireColumn.Hidden = False
.Columns("e").EntireColumn.Hidden = False
End Select
.Range("a1") = .Range("b6") '<== update the "echo" cell value for subsequent checking
End If
End With
Application.EnableEvents = True
End Sub
As you see, you must choose an "echo" cell in the "scoping" sheet, which will be used to store the previous value of its "B6" cell.
In my code I chose cell "A1" as the "echo" cell in "scoping" sheet, but you can choose whatever address you need provided it's a "free" cell (i.e.: your code and the user won't use it to write in) and change code accordingly (i.e. the "A1" address in If .Range("B6") <> .Range("A1") Then statement).
I found code online as an example that I have tweaked to show or hide specific rows depending on the selection I choose within a dropdown in my Excel file.
The macro is not working no matter what I try.
My code is as follows (also attached screenshot of rows under question 2 (2a - 2d) that are not showing/hiding)
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$13" Then
If Range("F13").Value = "Yes" Then
Rows("14:17").EntireRow.Hidden = False
End If
If Range("F13").Value = "No" Then
Rows("14:17").EntireRow.Hidden = True
End If
If Range("F13").Value = " " Then
Rows("14:17").EntireRow.Hidden = True
End If
End Sub
This is a good example of properly intending your code helping you identify an issue. You're missing an End IF statement. Try this:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$13" Then
If Range("F13").Value = "Yes" Then
Rows("14:17").EntireRow.Hidden = False
End If
If Range("F13").Value = "No" Then
Rows("14:17").EntireRow.Hidden = True
End If
If Range("F13").Value = " " Then
Rows("14:17").EntireRow.Hidden = True
End If
End If
End Sub
You may also want to use:
If Range("F13").Value = ""
instead of
If Range("F13").Value = " "
There is an End If missing. I assume the value of the target cell (F13) needs to be tested for it's value. If the value is "Yes", it should unhide row 14:17, if it is " " (spacebar) it should hide them and if it is "No" is should hide them as well. Other values will not affect the hiding/unhiding of the rows.
There should be a second End If before End Sub, so that all the if-statements above are wrapped within the Address check.
Also note that this code should be placed in the worksheet itself, since you want to hook into the Worksheet_Change event.
Try this in a worksheet module:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$F$13" Then 'Check if the changed value is indeed in F13
If Target.Value = "Yes" Then
ActiveSheet.Rows("14:17").EntireRow.Hidden = False 'Show the rows if the value is Yes
ElseIf Target.Value = "No" Then
ActiveSheet.Rows("14:17").EntireRow.Hidden = True 'Hide them when it's No
ElseIf Target.Value = " " Then
ActiveSheet.Rows("14:17").EntireRow.Hidden = True 'Or space
End If
End If
End Sub
Other remarks:
Instead of ActiveSheet you can also use Me (Me.Rows...) In this scenario they probably do the same. However, if you change the value on a worksheet from another worksheet (e.g. formula that recalculates), Me will reference the changed worksheet that fires the event, whereas activeworksheet will affect the currently active sheet.
Use Target instead of referencing the Range again. Target is a range object that is already in memory. Hence execution will be faster compared to accessing the worksheet again.
I have a project which requires Excel to hide rows on a separate sheet(within the same workbook) after user selects specific options on the activesheet. The macro is linked to a button, when clicked rows will be hidden on the separate sheet, and the whole process occurs in the background. If the user want to check the table with hidden rows they'd need to navigate to that separate sheet to see the result.
Image explanation:
http://postimg.org/image/ek6981vg1/
Worksheets("Input- Select Pens") --> active sheet where has the button
Worksheets("Input- Pen") --> separate sheet where has the hidden rows
I have tried several methods, but none of them worked:
Method 1:
Sub selectPens()
Dim c As Range
Application.EnableEvents = False
On Error Resume Next
For Each c In Range("E6:E35")
If c.Value = "FALSE" Then
Worksheets("Input- Pen").c.EntireRow.Hidden = True
ElseIf c.Value = "TRUE" Then
Worksheets("Input- Pen").c.EntireRow.Hidden = False
End If
Next c
On Error GoTo 0
Application.EnableEvents = True
End Sub
Method 2:
Sub selectPens()
Dim i As Long
Set wselect = Sheet11
With wselect
For i = 6 To 35
If ActiveSheet.Cells(i, 5).Value = "FALSE" Then
.Range("i:i").EntireRow.Hidden = True
' .Rows(i).EntireRow.Hidden = True
ElseIf ActiveSheet.Cells(i, 5).Value = "TRUE" Then
' .Rows(i).EntireRow.Hidden = False
.Range("i:i").EntireRow.Hidden = False
End If
Next i
End With
End Sub
I would be greatly appreciated for any help.
Many thanks!
Sub selectPens()
Dim i As Long, wsselect
Set wselect = Sheet11
For i = 6 To 35
'EDIT
wselect.Rows(i).Hidden = (ActiveSheet.Cells(i, 5).Value = False)
Next i
End Sub