I have the cell "cModelBoolean" in my worksheet which is calculated by formula into a Boolean.
I'm trying to base a select case function based on this cell. If the cell value = True then display MsgBox.
I have no idea why this isn't working!! See my code below
Option Explicit
Private cModelBoolean As Range
Private Sub DefineRangeVariables()
Set cModelBoolean = Me.Range("_cModelBoolean")
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Call DefineRangeVariables
Select Case Target.Address
Case cModelBoolean.Address
Select Case cModelBoolean
Case Is = True
MsgBox "True"
Case Is = False
MsgBox "False"
End Select
End Select
Exit Sub
End Sub
Thanks
Solution:
Private Sub Worksheet_Change(ByVal Target As Range)
Call DefineRangeVariables
Select Case Target.Address
Case cModelBoolean.Address
MsgBox cModelBoolean
' ..or if you want MsgBox only when True, as you state...
If cModelBoolean Then MsgBox cModelBoolean
' ... all other cases
End Select
End Sub
Im trying to trigger a popup msg for column q if it differs from "Open" Or "Closed" which say Please enter Justification this code works well but it gives me the bellow error message if i click on a different column (other than Q)
how can i get rid of the message?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Open") And Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Closed") Then
MsgBox "Please Enter Justification"
End If
End Sub
One way is to just check the .Column of the Target to make sure it's Q first.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 17 Then
If Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Open") And Not (Application.Intersect(Range("Q2:Q3977"), Target) = "Closed") Then
MsgBox "Please Enter Justification"
End If
End If
End Sub
There are probably much cooler ways.
I have a multiple selection, Option buttons, that change the value of cell D7 from 1 to 5, depending on choice. I want to unhide rows 16 to 26 if value is 1 and hide them if it's different, and so on for every other value.
But I can't even get this to work at all, and I'm not sure what I'm doing wrong.
Update: If I change the cell value, nothing happens, but if I delete all contents and add a value it gives: "Argument not optional", and it highlights this part of the code for me:
Private Sub Worksheet_Change(ByVal Target as Excel.Range)
Thank you
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If IsNumeric(Target) And Target.Address = "$D$7" Then
Select Case Target.Value
Case 0 To 90: Cell_Hider
End Select
End If
End Sub
Sub Cell_Hider(ByVal Target As Range)
If Range("$D$7").Value = "1" Then
Rows("16:26").EntireRow.Hidden = False
Else
Rows("16:26").EntireRow.Hidden = True
End If
End Sub
Your procedure Cell_Hider needs an argument but your code calls it without argument Case 0 To 90: Cell_Hider
You call Cell_Hider if the value is between 0 and 90 then that procedure needs the value to be 1 to show the rows and 0 or 2 to 90 will hide them. If you put 100 in that cell nothing happens at all. Sounds not like what you expect to me.
"1" is text not a number!
Something like the following would work:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If IsNumeric(Target) And Target.Address = "$D$7" Then
Select Case Target.Value
Case 0 To 90: Cell_Hider Target
End Select
End If
End Sub
Sub Cell_Hider(ByVal Target As Range)
If Target.Value = 1 Then
Target.Parent.Rows("16:26").EntireRow.Hidden = False
Else
Target.Parent.Rows("16:26").EntireRow.Hidden = True
End If
End Sub
Even though it doesn't look logic to me and I'm not sure what you are exactly trying to achieve.
Note that you can shorten it to
Sub Cell_Hider(ByVal Target As Range)
Target.Parent.Rows("16:26").EntireRow.Hidden = Not Target.Value = 1
End Sub
Hide/Unhide Rows on Cell Change
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim sCell As Range: Set sCell = Me.Range("D7")
If Intersect(sCell, Target) Is Nothing Then Exit Sub
If IsNumeric(sCell.Value) Then
HideRows sCell
End If
End Sub
Sub HideRows(ByVal SourceCell As Range)
If SourceCell.Value = 1 Then
SourceCell.Worksheet.Rows("16:26").Hidden = False
Else
SourceCell.Worksheet.Rows("16:26").Hidden = True
End If
End Sub
The objective of this code is so every time cell E6:E36 changes from "Yes" or to "Enter Non Final Action Taken Date" I want it to run my macro.
It works only when E6 is marked to match the next value. How do I make it so it is not dependent on the previous cells value?
I'm new with VBA so I'm a bit lost. Any help would be greatly appreciated. See current code below:
Private Sub Worksheet_Change(ByVal Target As range)
Application.EnableEvents = False 'pervent triggering another change event
On Error GoTo ERR_HANDLING
If Not Intersect(Target, range("E6:E36")) Is Nothing Then
Select Case range("E6,E7,E8,E9,E10,E11,E12,E13,E14,E15,E16,E17,E18,E19,E20,E21,E22,E23,E24,E25,E26,E27,E28,E29,E30,E31,E32,E33,E34,E35,E36")
Case "Yes": EnterDate_of_last_Submission
End Select
End If
If Not Intersect(Target, range("E6,E7,E8,E9,E10,E11,E12,E13,E14,E15,E16,E17,E18,E19,E20,E21,E22,E23,E24,E25,E26,E27,E28,E29,E30,E31,E32,E33,E34,E35,E36")) Is Nothing Then
Select Case range("E6,E7,E8,E9,E10,E11,E12,E13,E14,E15,E16,E17,E18,E19,E20,E21,E22,E23,E24,E25,E26,E27,E28,E29,E30,E31,E32,E33,E34,E35,E36")
Case "Enter Non Final Action Taken Date": EnterNonFinal_Date
End Select
End If
On Error GoTo 0
ERR_HANDLING:
Application.EnableEvents = True
If Err.Number <> 0 Then
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContex
End If
End Sub
You need to loop here and compare cell-by-cell, something like the following:
Private Sub Worksheet_Change(ByVal Target As range)
Dim rngToCheck as Range
Set rngToCheck = Intersect(Target, Me.Range("E6:E36"))
If rngToCheck Is Nothing Then Exit Sub
On Error GoTo SafeExit
Application.EnableEvents = False
Dim rng as Range
For Each rng in rngToCheck
Select Case rng.Value
Case "Yes"
EnterDate_of_last_Submission
Case "Enter Non Final Action Taken Date"
EnterNonFinal_Date
End Select
Next
SafeExit:
Application.EnableEvents = True
End Sub
Most likely EnterDate_of_last_Submission and EnterNonFinal_Date should be rewritten to take a Range parameter, namely the cell the date is entered in (which it looks like would correspond to rng.Offset(,1) with your current setup.
I want to my macro to send a message just when the second cell have a value. For now the seconde I put something in the first cell my msgBox pop up!
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("G557") <> Range("G555") Then
MsgBox "It has to be equal"
End If
End Sub
Target in that subroutine parameter is the Range (cell) that had the change that triggered the worksheet_change() event. Just test that target to see if it is the cell you want to track. The most common way to do this is using the INTERSECT() function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("G555")) Is Nothing Then
If Range("G557") <> Range("G555") Then
MsgBox "It has to be equal"
End If
End If
End Sub
Now the rest of your code will only execute if G555 was changed.
Just add additional condition to your If statement:
If Range("G557") <> Range("G555") And Range("G557") <> "" And Range("G555") <> "" Then