Select case with formulated cell NOT WORKING - excel

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

Related

Trying to Have specific cell values trigger multiple macros in VBA code

I am creating an inputs page on an excel document.
Cell B3- can have three values, each value triggers a different macro
Cell B4- can have two values, each value triggers a different macro
I wrote the following code:
Sub worksheet_change(ByVal target As Excel.Range)
If target.Cells.Count > 1 Then Exit Sub
If IsNumeric(target) And target.Address = "$B$3" Then
Select Case target.Value
Case Is = 2: Class2
Case Is = 3: Class3
End Select
End If
End Sub
Sub worksheet_change(ByVal target As Range)
Set target = Range("$B$4")
If target.Value = "yes" Then
Call RetireeLife
End If
End Sub
However, I found out the worksheet_change can only be used once in a worksheet but I am unsure on how to combine the codes. Any help would be greatly appreciated.
Depending on what you want to use, ElseIf or Cases, the following demonstrates both used in your worksheet_change:
Sub worksheet_change(ByVal target As Excel.Range)
If target.Cells.Count > 1 Then Exit Sub
If IsNumeric(target) And target.Address = "$B$3" Then
Select Case target.Value
Case Is = 2: Class2
Case Is = 3: Class3
Case Is = 4: YourOtherSub 'You did say 3 options :p
Case Else
MsgBox("Not a correct value in B3")
Exit Sub
End Select
ElseIf target.Address = "$B$4" Then
If target.Value = "yes" Then RetireeLife
ElseIf target.Value = "no" Then KeepLiving 'or whatever your other macro is called
Else
MsgBox("Not a correct value in B4")
Exit Sub
End If
End If
End Sub
Do note that the Exit Sub (apart from the first one) isn't necessary since you'll get out of the If's and exit the sub regardless when you get to those lines in the code. Just in case you'll append extra code, it's there already.
If you want to use Select for the cell-choices, you can use Select for the target.Address and then check within the "$B$3" case if it's Numeric.
Hope this helps!
Like this for example:
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case "$B$3"
Select Case Target.Value
Case 2: Class2
Case 3: Class3
End Select
Case "$B$4"
If IsNumeric(Target.Value) Then RetireeLife
End Select
End Sub

Hide Rows in Excel based on cell value

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

MsgBox does not pop when given multiple arguments in If / Then statement

When i select cell C1, if A1 is empty, i want a message box to pop up. Though nothing actually happens (no error message either). Is my syntax wrong or am i going about it the wrong way?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "C1" And Range("A1") Is Nothing Then
MsgBox "Please fill out previous fields to continue"
End If
End Sub
Try:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$1" And Range("A1").Value = "" Then 'or IsEmpty(Range("A1"))
MsgBox "Please fill out previous fields to continue"
End If
End Sub
Target.Address would return $C$1 - so that's needed there. And Is Nothing isn't used for determining if a cell is empty - that would be IsEmpty() or .Value = "" - Is Nothing is used more for determining if an Object has been Set.
Range("A1") returns a Range object that represents the cell at A1 - this will never be Nothing because A1 will always exist as a cell.
You can And IsEmpty(Range("A1")) to see if it has a value.
Try this
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$1" And Len(Range("A1")) = 0 Then
MsgBox "Please fill out previous fields to continue"
End If
End Sub
This should work for You:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(False, False) = "C1" And IsEmpty(Range("A1")) = True Then
MsgBox "Please fill out previous fields to continue"
End If
End Sub

Worksheet_Change to hide rows in Excel

I have a macro that is supposed to hide a row in excel when a value of a given cell is "ODD" (the word, not an odd number). I've tried two different formats; neither gives any visible error but neither hides the row.
Sub Worksheet_Change(ByVal target As Range)
If target.Address <> "$B$2" Then Exit Sub
ElseIf Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
The other code I had is:
Select Case Range("B2").Value
Case Is = "ODD": Rows("5:5").EntireRow.Hidden = False
Case Else: Rows("5:5").EntireRow.Hidden = True
End Select
It was modified from a more advanced case statement and I just left it that way at first.
The Rows("5:5") would be better as Rows(5). The method you used would be better as Range("5:5").
Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
Rows(5).EntireRow.Hidden = (UCase(Target.Value) = "ODD")
End If
End Sub
Since comparing B2 to ODD already produces a True or False, you can dispense with the If/Else/End If. Text comparisons in VBA are usually case sensitive, hence the need for UCase to force case insensitivity.
You are missing a key code line If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then Try the following
Private Sub Worksheet_Change(ByVal Target As Range)
Dim cell As Range
Set cell = Range("B2")
If Not Application.Intersect(cell, Range(Target.Address)) Is Nothing Then
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = False
Else
Rows("5:5").EntireRow.Hidden = True
End If
End If
End Sub
First make sure your Change Sub is stored in the Worksheet module of the Sheet you want this to perform on. Then you have a slight syntax error with your If Statements:
Private Sub Worksheet_Change(ByVal target As Range)
If target is Nothing Then Exit Sub
If target.Address <> "$B$2" Then Exit Sub
If Range("B2").Value = "ODD" Then
Rows("5:5").EntireRow.Hidden = True
Else
Rows("5:5").EntireRow.Hidden = False
End If
End Sub
When you put the If...Then... on one line, it actually closes the If (no End If needed) Also, I flipped your True and False statements to match your requirement in your question.

Bind macro to excel cell onclick?

In excel 2000, is it possible to bind a vba function to be executed when a cell is clicked with the mouse?
Found a solution:
Make a named range for the cells you want to capture clickevent
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
Application.ActiveSheet.Cells(1, 1).Value = Target.Address
If Not Intersect(Target, Range("MyNamedRange")) Is Nothing Then
' do your stuff
Range("A1").Select
Endif
Application.EnableEvents = True
End Sub
You can bind to a cell double click.
Open VBA, goto the worksheet you want to wire up the event
Select WorkSheet in the dropdown on the top left and BeforeDoubleClick in the top right
The check the Target.Address is equal to the address of the cell you care about and call the function you wish.
Something like this:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address(True, True, xlA1) = "$A$1" Then
Call MyDemo
End If
End Sub
Private Sub MyDemo()
MsgBox "Hello"
End Sub

Resources