So on excel I have 2 worksheets one called Setup and the other called Summary. The user is asked to fill out questionnaire from the "Setup" worksheet. Information filled out in the setup page copies over to the Summary page. Below are 2 pictures showing the Setup page and Summary page.
Setup Page
Summary page
So the issue I am having is that I am trying to automate the hide/unhide feature using vba and its not working for merged cells. I want the Summary page to hide Rows 33 to 35 if the setup page is left blank and unhide if something is written on the Setup page. Here is the code I have tried below and got this run-time error.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
With Sheets("Setup")
If Range("M29").value = False Then
Sheets("SUMMARY").Range("B33:B35").Hidden = False
Else
Sheets("SUMMARY").Range("B33:B35").Hidden = True
End If
End With
End Sub
Instead of Range, use Rows.
Sheets("SUMMARY").Rows("33:35").Hidden = False 'and similarly for True
Also, note that you're not actually referencing the With. You need a period . before Range.
With Sheets("Setup")
If .Range("M29").value = False Then
You may be looking for IsEmpty to test if that range is blank.
Also, if this code is in the "Setup" sheet code module, then you can use Me instead of Sheets("Setup").
With those edits, you might have something like this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If IsEmpty(Me.Range("M29").Value) Then
Sheets("SUMMARY").Rows("33:35").Hidden = True ' shouldn't this be True, hide if empty?
Else
Sheets("SUMMARY").Rows("33:35").Hidden = False
End If
End Sub
Or as a one-liner:
Sheets("SUMMARY").Rows("33:35").Hidden = IsEmpty(Me.Range("M29").Value)
Related
I have a table where I want to be able to hide individual rows at a mouse click. The (seemingly) easiest solution I've found is to have a column filled with hyperlinks that call a macro to hide the row that they're in.
There are two ways of calling macros from hyperlinks: using Worksheet_FollowHyperlink with manual hyperlinks, and using =HYPERLINK.
The former works fine, except there's no way (that I've found) to have them generate automatically when new rows are added to the table. I would have to either manually copy them down every time, which is unviable, or add them with VBA, which adds a bunch of complexity to an otherwise simple task.
The latter generates fine, being a formula, but it doesn't actually work. It doesn't trigger Worksheet_FollowHyperlink, and when using =HYPERLINK("#MyFunction()") it just doesn't hide rows (or do much other than editing cells contents).
Function MyFunction()
Set MyFunction = Selection
Selection.EntireRow.Hidden = True
End Function
Is there a good solution to this?
Rather than a Hyperlink, you could handle a Double Click event on the table column
Something like
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Dim NameOfTableColumn As String
On Error GoTo EH:
NameOfTableColumn = "DblClickToHide" ' update to suit your table
If Not Application.Intersect(Target, Target.ListObject.ListColumns(NameOfTableColumn).DataBodyRange) Is Nothing Then
Target.EntireRow.Hidden = True
Cancel = True
End If
Exit Sub
EH:
End Sub
Please, copy the next code in the sheet code module where the table to be clicked exists. Clicking on each cell in its first column (dynamic to rows adding/insertions/deletions), the clicked row will be hidden:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim tbl As ListObject
If Target.cells.CountLarge > 1 Then Exit Sub
Set tbl = Me.ListObjects(1) 'you may use here the table name
If Not Intersect(Target, tbl.DataBodyRange.Columns(1)) Is Nothing Then
Application.EnableEvents = False
Target.EntireRow.Hidden = True
Application.EnableEvents = True
End If
End Sub
It would be good to think about a way to unhide the hidden row if/when necessary. If only a row should be hidden at a time, it is easy to unhide all the rest of column cells...
I've created a drop down menu through data validation for workbook navigation. The following is a snippet of code I have for the drop down box to change worksheets in the workbook:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then _
ThisWorkbook.Sheets("Home").Visible = False
ThisWorkbook.Sheets(Target.Value).Activate
ThisWorkbook.Sheets(Target.Value).Visible = True
ThisWorkbook.Sheets("Imported Data").Visible = False
End Sub
The code is meant to hide all other worksheets that are accessible by the drop down list besides the one selected. I have about 10 tabs and this code has worked perfectly to achieve the basic goal of navigation. However, some pages have formulas and when you update data in the cells meant for calculations the workbook jumps to a random worksheet in the workbook that is not at all referenced in this sub.
Is there some way to have my worksheets not try to do anything with this sub unless the dropdown menu itself is changed?
Bonus (less important) Question: is there a way to make the drop box default to (blank) unless the menu itself is accessed?
Then _
The space followed by an underscore _ means that the current statement isn't finished yet but continues on the next line. Right now the last 3 lines will run whenever there is a change in the worksheet. Put the entire code in If-Endif.
Also avoid unnecessary use of On Error Resume Next. Use proper error handling.
You need to make the sheet visible before you activate it and not vice versa.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
If Not (Application.Intersect(Range("J4"), Target) Is Nothing) Then
ThisWorkbook.Sheets("Home").Visible = False
ThisWorkbook.Sheets(Target.Value).Visible = True
ThisWorkbook.Sheets(Target.Value).Activate
ThisWorkbook.Sheets("Imported Data").Visible = False
End If
Letscontinue:
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
is there a way to make the drop box default to (blank) unless the menu itself is accessed?
If you have created it with Data Valdation then insert a blank value in the list.
I have an Excel WorkBook with several Sheets. I would like to be able to select from a drop down list in the "Home" Sheet and after selection is made, automatically switch to the proper Sheet and select a specific Cell.
It appeared to be easy, but I have failed time and again to make it work.
Here is an example of what I would like to do:
The only code that I managed to have a little success with is the following:
Private Sub Worksheet_Activate()
With Sheets("Home")
If Cells(6, 3).Value = "A" Then
Sheets("A").Select
ActiveSheet.Range("B7").Select
End If
End With
End Sub
The problem with it is that it will not check for the value until the user moves to another sheet. Then when it comes back, it will check for it, and will take it to the correct one, but the user will be stuck in a loop without being able to go back to "Home". (I know that it will only work on cell C6, but I just wanted to try if it worked before changing the Range)
You need the worksheet change event, rather than activate. Try this, in the Home sheet module.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$6" Then
Application.Goto Sheets(Target.Text).Range("B7")
End If
End Sub
I want to use VBA code for the following: based on a cell output (either "TRUE" or "FALSE" in cell W20, which changes based on whether a user checks a box or not), I want rows to be hidden/unhidden on another worksheet called "Analysis" worksheet based on the code below. I right clicked the "Summary" tab where cell W20 and the checkbox are located -> view code -> wrote the code below, but it's not working. I am very new to VBA, please help, it would be very appreciated, thanks in advance
Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("$W$20")
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = False
End If
End If
End Sub
Excel doesn't recognize the modification of a cell value caused by the manipulation of form controls. I assume this is because Excel links a form control to a value and applies the changes itself, therefore not considering it a user change in the spreadsheet.
In order to make your code work, you will have to move your code into a module as a click event of your form control. Click the Developer tab and then Visual Basic. Once Visual Basic is open, you will notice on the left pane that you have a list of your worksheets within a VBA Project bearing the name of your workbook and your macro is currently within one of them. Once you found it, delete the code of your macro. Then, right-click on the name of your workbook and then select Insert > Module...
On the coding pane, insert this modified code:
Sub NameOfYourFormControlCheckBox_Click()
Dim MyRange As Range
Set MyRange = Range("$W$20")
If MyRange = False Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
ElseIf MyRange = True Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
End Sub
Make sure that the name of the sub is the name of your form control (checkbox) followed by an underscore and then "click".
Hope this helps!
I would like to hide the current page that the user is looking at when they click a hyperlink within excel that takes them to a different worksheet within the same workbook. I tried using the following code
Private Sub Worksheet_FollowHyperlink(ByVal Target As Hyperlink)
On Error GoTo Cleanup
ActiveSheet.Visible = False
Application.EnableEvents = False
Target.Follow
Cleanup:
Application.EnableEvents = True
End Sub
because I assumed the activesheet would be the sheet that the hyperlink is on and not the target sheet, however, ActiveSheet is the target sheet. Any suggestions on how to hide the partnet sheet?
This is going to sound odd, but you need to replace
ActiveSheet.Visible = False
with
Target.Parent.Parent.Visible = False
Why?
The "Target" is the Cell being linked to.
The Parent of that cell is the cell that is the source of hyperlink
The parent of that cell is the worksheet
The best solution may be to create a list of sheets that can be visible when each sheet is active, then instead of using the FollowHyperlink event, use the Worksheet_Activate event to hide/unhide the necessary sheets.