Running VBA set on formula cell - excel

I have some VBA code (below) to hide and show rows based on a cell value, the VBA works when the cell is clicked in and then out however or done manually however there is a formula in the cell i need the VBA to be based on and I have tried a couple of ways but I am not getting any success and cannot get it to refresh and run the VBA. I don't really want or need to have the end user to click in any unnecessary fields - any advice would be greatly appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
Application.ScreenUpdating = False
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "0":
Rows("27:64").EntireRow.Hidden = True
End Select
End If
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "1":
Rows("27:29").EntireRow.Hidden = False
Rows("31:42").EntireRow.Hidden = False
Rows("52:64").EntireRow.Hidden = False
Rows("43:45").EntireRow.Hidden = True
Rows("46:51").EntireRow.Hidden = True
Rows("30:30").EntireRow.Hidden = True
End Select
If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "2":
Rows("27:29").EntireRow.Hidden = False
Rows("31:45").EntireRow.Hidden = False
Rows("52:64").EntireRow.Hidden = False
Rows("46:51").EntireRow.Hidden = True
Rows("30:30").EntireRow.Hidden = True
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "3":
Rows("27:31").EntireRow.Hidden = False
Rows("31:42").EntireRow.Hidden = False
Rows("46:51").EntireRow.Hidden = False
Rows("43:45").EntireRow.Hidden = True
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "4":
Rows("27:31").EntireRow.Hidden = False
Rows("32:45").EntireRow.Hidden = True
Rows("52:64").EntireRow.Hidden = True
Rows("46:51").EntireRow.Hidden = False
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "5":
Rows("27:64").EntireRow.Hidden = False
End Select
End If
Application.ScreenUpdating = True
End Sub

I'm gonna assume you are checking the cell G20in order to hide/unhide rows. So we are going to do this:
First change the code in your worksheet object for this:
Option Explicit
Private Sub Worksheet_Calculate()
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
HideRows Me.Range("G20"), Me
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
This will call another procedure which will be contained in a module like this:
Option Explicit
Sub HideRows(CellValue As Long, ws As Worksheet)
With ws
Select Case CellValue
Case 0
.Rows("27:64").EntireRow.Hidden = True
Case 1
.Rows("27:29").EntireRow.Hidden = False
.Rows("31:42").EntireRow.Hidden = False
.Rows("52:64").EntireRow.Hidden = False
.Rows("43:45").EntireRow.Hidden = True
.Rows("46:51").EntireRow.Hidden = True
.Rows("30:30").EntireRow.Hidden = True
Case 2
.Rows("27:29").EntireRow.Hidden = False
.Rows("31:45").EntireRow.Hidden = False
.Rows("52:64").EntireRow.Hidden = False
.Rows("46:51").EntireRow.Hidden = True
.Rows("30:30").EntireRow.Hidden = True
Case 3
.Rows("27:31").EntireRow.Hidden = False
.Rows("31:42").EntireRow.Hidden = False
.Rows("46:51").EntireRow.Hidden = False
.Rows("43:45").EntireRow.Hidden = True
Case 4
.Rows("27:31").EntireRow.Hidden = False
.Rows("32:45").EntireRow.Hidden = True
.Rows("52:64").EntireRow.Hidden = True
.Rows("46:51").EntireRow.Hidden = False
Case 5
.Rows("27:64").EntireRow.Hidden = False
End Select
End With
End Sub
This way you can use the code for multiple worksheets if they are build the same way, you will only need to copy the code from the worksheet object to other worksheets and it will cal the HideRows which will then hide/unhide rows depending on the value of the cell G20 you can change the target on every sheet.
And as for the use of Select Case you can see that you only need one with all your cases for the value.

Related

Merging separate Double Click VBA events in a single worksheet

I have a spreadsheet where I have adapted two pieces of VBA code to perform two different double click event actions.
The 1st piece of code enters a "✓" in a specific range of cells when double clicked and removes it when double clicked again:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
End Sub
The 2nd piece of code enters a date/time stamp in a range of cells when double clicked:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Coded by SunnyKow - 16/09/2016
Application.EnableEvents = False
On Error GoTo ErrorRoutine
'You can change the range here
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Because you cannot have two double click events in single worksheet (as separate VBA code), how do I merge these two pieces of VBA so that it is a single piece of code with two distinct actions based on the cell range selected. Would appreciate any help to resolve this.
It looks like an if statement will do the trick
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
'Update only if cell is empty
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600,M2:V600")) Is Nothing Then
Application.EnableEvents = False
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Application.EnableEvents = False
On Error GoTo ErrorRoutine
If Not Intersect(Target, Range("L2:L600,Y2:Y600")) Is Nothing Then
If Target = "" Then
Target = Now
End If
Cancel = True
ElseIf Not Intersect(Target, Range("M2:V600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
End If
Cancel = True
ElseIf Not Intersect(Target, Range("H2:H600")) Is Nothing Then
If ActiveCell.Value = ChrW(&H2713) Then
ActiveCell.ClearContents
Else
ActiveCell.Value = ChrW(&H2713)
Target.Offset(0, 18) = Now
End If
Cancel = True
End If
Application.EnableEvents = True
Exit Sub
ErrorRoutine:
Application.EnableEvents = True
End Sub

Lock cells dependent on other cells

I'm trying to make an excel where other users can only change a range of cells dependent on an other cell they can change as well (B2). The independent cell B2 is a dropdownlist where they can choose from and can have 4 values. I looked on internet and found examples of independent cells having 2 values. I based myself on this. When I check my code when I didn't protect my worksheet yet it looks like the right cells are locked and unlocked. From the moment I change it to protected sheet I get Run time error 1004:Unable to set locked property of ranged class. I tried to solve it but can't find to fix it. Does someone has an idea where I'm going wrong?
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B2") = "text 1" Then
Range("F4:Q4").Locked = True
Range("B2").Locked = False
ElseIf Range("B2") = "Text 2" Then
Range("F4:I4").Locked = False
Range("B2").Locked = False
Range("J4:Q4").Locked = True
ElseIf Range("B2") = "Text 3" Then
Range("B4:Q4").Locked = False
Range("B2").Locked = False
ElseIf Range("B2") = "text 4" Then
Range("B4:Q4").Locked = False
Range("B2").Locked = False
End If
End Sub
I was able to solve it with following code
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("B2") = "Text 1" Then
Sheet2.Unprotect Password:="Secret"
Range("F4:Q4").Locked = True
Range("B2").Locked = False
Sheet2.Protect Password:="Secret"
ElseIf Range("B2") = "text 2" Then
Sheet2.Unprotect Password:="Secret"
Range("F4:I4").Locked = False
Range("B2").Locked = False
Range("J4:Q4").Locked = True
Sheet2.Protect Password:="Secret"
ElseIf Range("B2") = "text 3" Then
Sheet2.Unprotect Password:="Secret"
Range("B4:Q4").Locked = False
Range("B2").Locked = False
Sheet2.Protect Password:="Secret"
ElseIf Range("B2") = "text 4" Then
Sheet2.Unprotect Password:="Secret"
Range("B4:Q4").Locked = False
Range("B2").Locked = False
Sheet2.Protect Password:="Secret"
End If
Now I'm trying to put back in code that was also embedded in worksheet_change
If Target.Column = 2 Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(0, 1).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
This gives me error that I have protected sheet. I thought solving it like this.
Sheet2.Unprotect Password:="Secret"
If Target.Column = 2 Then
If Target.Validation.Type = 3 Then
Application.EnableEvents = False
Target.Offset(0, 1).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
Sheet2.Protect Password:="Secret"
If I do this it does no longer erase value next to dropdownlist. Any clue why it no longer does this?

How do I make a ListBox activeX (basically a drop checklist) to show hidden rows

Basically I have created a List box with a total of 4 items. You can select multiple items in this sheet and I will like to make it so that each selection would show hidden rows, while being able to make multiple selections.
Option Explicit
Private Sub ListBox1_Click()
End Sub
Private Sub worksheet_change(ByVal target As Range)
Application.EnableEvents = False
Union(Rows("20:30"), Rows("51:56")).EntireRow.Hidden = True
If Not Application.Intersect(Range("ListBox1"), Range(target.Address)) Is Nothing Then
Select Case target.Value
Case "Asset Transfer"
Rows("20:22").EntireRow.Hidden = False
Case "Fund Lineup"
Rows("27:30").EntireRow.Hidden = False
Case "Plan Merge"
Rows("23:26").EntireRow.Hidden = False
Case "Loans"
Rows("51:56").EntireRow.Hidden = False
End Select
End If
Set target = Range("bulk")
If target.Value = "Yes" Then
Range("C60") = InputBox("Please enter the number of labor hours'")
Range("D60") = InputBox("Enter Percentage Increase'")
End If
Set target = Range("AutoEnrollment")
If target.Value = "Yes" Then
Range("C59") = InputBox("Please enter the number of employees'")
End If
Application.EnableEvents = True
End Sub

Worksheet_Calculate to hide command button

I found the code below for Worksheet_Change. But my H29 is formula =SUM.
How do I change this to Worksheet_Calculate so that the below macro will run?
Basically what I want is if H29 is calculated as being equal to 20, show the button, else hide it.
Private Sub Worksheet_Change(ByVal Target As Range)
Application.ScreenUpdating = False
If Target = Range("H29") Then
If Target.Value = "20" Then
Me.CommandButton1.Visible = True
Else
Me.CommandButton1.Visible = False
End If
Application.ScreenUpdating = True
End If
Depending on the button type, the code to instruct the button will be different. Try this.
Private Sub Worksheet_Calculate()
'ActiveX button
If Range("H29").Value = 20 Then
Sheets("Sheet1").CommandButton2.Visible = False
Else
Sheets("Sheet1").CommandButton2.Visible = True
End If
'Forms Button
If Range("H29").Value = 20 Then
Sheets("Sheet1").Shapes("CommandButton1").Visible = msoFalse
Else
Sheets("Sheet1").Shapes("CommandButton1").Visible = msoTrue
End If
End Sub
Sorry asked too soon. Resolved
Private Sub Worksheet_Calculate()
'ActiveX button
Application.ScreenUpdating = False
If Range("H29").Value = 20 Then
Me.CommandButton1.Visible = True
Else
Me.CommandButton1.Visible = False
End If
Application.ScreenUpdating = True
End Sub

Unable to set the hidden property of the range class run time error '1003'

I have code in this module:
Sub HideSalTable()
User = Worksheets("log").Range("R1").Value
If User = ThisWorkbook.Worksheets("SSSSSS").Range("za1").Value Then
Columns("S:AA").EntireColumn.Hidden = True
ElseIf User = ThisWorkbook.Worksheets("SSSSSS").Range("za3").Value Then
Columns("S:AA").EntireColumn.Hidden = False
ElseIf User = ThisWorkbook.Worksheets("SSSSSS").Range("za4").Value Then
Columns("S:AA").EntireColumn.Hidden = False
End If
End Sub
I have a button to redirect me to ThisWorkbook.Worksheets("SSSSSS") with this code:
Private Sub Change_SSSSSS_Button_Click()
Dim pass1 As String
Dim pass2 As String
pass1 = ThisWorkbook.Worksheets("SSSSSS").Range("za3").Value
pass2 = ThisWorkbook.Worksheets("SSSSSS").Range("za4").Value
Dim Inp
Dim lTries As Long
lTries = 1
Do
Inp = InputBoxDK("enter password", "Zmhnk")
If Inp = "" Or Inp = vbCancel Then Exit Sub '* Cancel button pressed or nothing entered
If Inp = (pass1) Or Inp = (pass2) Then
Exit Do
End If
lTries = lTries + 1
If lTries > 4 Then
MsgBox "Error", vbInformation, "Zmhnk"
Exit Sub
Else
If MsgBox("try again", vbYesNo, "error_Zmhnk") = vbNo Then Exit Sub
End If
Loop
Application.ScreenUpdating = False
Sheets("SSSSSS").Visible = True
Sheets("SSSSSS").Activate
Application.ScreenUpdating = True
End Sub
The problem is when the user presses the button with the 2nd code I face an error and I don't know why.
The error:
Unable to set the hidden property of the range class run time error '1003'
Two things
1) You have not fully qualified your range. I understand that you are getting redirected but this is much safer.
Columns("S:AA").EntireColumn.Hidden = True
Change it to
ThisWorkbook.Sheets("SSSSSS").Columns("S:AA").EntireColumn.Hidden = True
2) I believe your worksheet is protected. You have to unprotect it. You can do that as follows
ThisWorkbook.Sheets("SSSSSS").Unprotect "myPassword"
when you have the control from the Form there is no Problem
but if you have it from the worksheet itself then it works actually but with Error:1004
so just use ( On Error Resume Next)
Private Sub ComboBox1_Change()
Dim wsMon As Worksheet
Set wsMon = ThisWorkbook.Worksheets("Montag")
On Error Resume Next
Select Case ComboBox1.ListIndex
Case 0
xHide (False)
wsMon.Rows("12:25").EntireRow.Hidden = True
xHide (True)
Case 1
xHide (False)
wsMon.Rows("12:25").EntireRow.Hidden = False
wsMon.Rows("19:25").EntireRow.Hidden = True
xHide (True)
Case 2
xHide (False)
wsMon.Rows("12:25").EntireRow.Hidden = False
xHide (True)
End Select
End Sub
xHide is a Boolean Function :
true
Application.ScreenUpdating = True
Application.DisplayAlerts = True
or False
Application.ScreenUpdating = False
Application.DisplayAlerts = False
I had a similar issue (only the error code was 1004, but the error message was the same). What solved the issue at my Excel sheet was to remove a comment which was within the range that I tried to hide. It seems like comments are not allowed within the range that should be hidden.

Resources