There are similar posts relating to my question, but I have struggled to adapt them to my problem. I currently have the following code which works just fine:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
Application.ScreenUpdating = True
Set Target = Range("B4")
If Target.Value = "Yes" Then
Call DropDownListOn
Else
Call DropDownListOff
End If
Application.EnableEvents = True
End Sub
and the macros that are being called are:
Option Explicit
Sub DropDownListOn()
Sheet1.Activate
Sheet1.Range("A5").Value = "Plot default probability"
Sheet1.Range("B5").Validation.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Formula1:="Yes, No"
End Sub
and
Option Explicit
Sub DropDownListOff()
Sheet1.Range("A5").Value = ""
Sheet1.Range("B5").Validation.Delete
End Sub
Essentially what it does is as follows: Cell B4 is dropdown list. If "Yes" is chosen, then by calling the macro "DropDownListOn", it generates a new dropdown list in cell B5. This part works just fine; but, say, that once the dropdown list in cell B5 appears, I want to call another macro if B5 is chosen to be "Yes" (e.g., generating another dropdown list in cell B6). Given that only one target can be assigned to each sheet, this does not seem to be very straightforward. There seem to be suggestions for ways around this obstacle on Stackoverflow, but I struggle to adopt these to my own case. Any help and/or sample code will very much be appreciated.
Please, try the next updated event:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "B4" Then
If Target.Value = "Yes" Then
Call DropDownListOn 'it let it creating the drop-down in "B5", TRIGGERING the change event
'but delete the charts ONE BY ONE!
Else
Application.EnableEvents = False
Call DropDownListOff 'the event is not triggered here.
'if you want to also clear "B6", you should place such a code line in the above Sub...
Application.EnableEvents = True
End If
ElseIf Target.Address(0, 0) = "B5" Then
Application.EnableEvents = False
'Place here the sub able to create the drop-down validation in "B6"
'it let it creating something else, but without triggering the event
Application.EnableEvents = Trud
End If
End Sub
Related
I am a newbie in VBA coding.
I am trying to achieve that inside my function
sub Private Sub Worksheet_Change(ByVal Target As Range)
where it checks some specified cells value change to run a given macro. The additional macro that i want to add is that when ever cell a62 is Empty, it will hide rows a56:a61. and consecutively if a82 is empty, it will hide rows a78:a82.
i have the following code but it only hides the rows from the first empty cell until end of sheet.
Sub Test()
Dim i As Long
For i = 4 To 800
If Sheets("Results").Cells(i, 1).Value = "" Then
Rows(i & ":" & Rows.Count).EntireRow.Hidden = True
Rows("1:" & i - 1).EntireRow.Hidden = False
Exit Sub
End If
Next
End Sub
Please, check the next event code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$62" Or Target.Address = "$A$82" Then
Select Case Target.Address
Case "$A$62"
If Target.Value = "" Then
Range("A56:A61").EntireRow.Hidden = True
Else
Range("A56:A61").EntireRow.Hidden = False
End If
Case "$A$82"
If Target.Value = "" Then
Range("A78:A81").EntireRow.Hidden = True
Else
Range("A78:A81").EntireRow.Hidden = False
End If
End Select
End If
End Sub
It will be triggered only if you MANUALLY change the value of one of the two required cells.
If their value is the result of a formula, Worksheet_Calculate event must be used, but in a different way. This event does not have any argument (no Target) and you must check the two cells in discussion and act according to their value, independent if they were changed or not when the Calculate event is triggered. If this is the case, I can post such an event code, too.
Edited:
For the version which does not involve the manual changing of the values, please copy this event code in the sheet code module:
Private Sub Worksheet_Calculate()
If Me.Range("A62").Value = "" Then
Me.Range("A56:A61").EntireRow.Hidden = True
Else
Me.Range("A56:A61").EntireRow.Hidden = False
End If
If Me.Range("A82").Value = "" Then
Me.Range("A78:A82").EntireRow.Hidden = True
Else
Me.Range("A78:A82").EntireRow.Hidden = False
End If
'Edited:
'The part for both analyzed ranges being empty:
If Me.Range("A62").Value = "" And _
Me.Range("A82").Value = "" Then
'Do here what you need...
End If
End Sub
Prototypical post: New to VBA, unable to resolve an issue after having read multiple posts/websites, and now turning to all the fantastic people here who's posts have gotten me this far.
I have a worksheet with data validation in column C (list; forced-choice Yes/No options). If user selects "No" in C7, then C9:C11 need to automatically and immediately populate as "No." I have gotten this to work by the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" And Target.Value = "No" Then
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
End If
End Sub
I also have a text box on the same worksheet (i.e., Sheet5) that when clicked fires a macro which clears the contents of C6:C7. This Reset macro is in a module under General.
Sub C_B_Reset()
Sheet5.Range("C6:C7").ClearContents
End Sub
Individually, these work fine, but when both exist it results in Type 13 error at the Target.Address after the Reset macro is fired. After firing the Reset macro, the "If Target.Address" portion resolves to the range referenced in the Reset macro (i.e., C6:C7). Because "If Target.Address" expects a single, absolute cell reference (e.g., $C$7), it is throwing the mismatch code because it instead is resolving to (C6:C7) when the mouse is hovered over it.
Even if the Reset macro is completely deleted, the same issue happens if the following is used in the Target.Address code:
Range("$C$9:$C$11").Value = "No"
The Target.Address then resolves to "$C$9:$C$11" and throws the Type 13 mismatch error.
It appears that if "Range" is used to refer to a range of cells in any other macro, it automatically gets assigned as Target.Address. However, this doesn't happen if Range only refers to single cells (which is why there are separate lines for C9 to C11 in the Worksheet_Change code).
I'm sure I'm using incorrect terminology, but I hope I explained it well enough, because I sure would appreciate some help.
Thanks for taking a look,
"Excel VBA Target.Address being modified and causing Error 13 type mismatch"
Target.Address isn't the problem here... Target is the cell(s) that were changed, so Target.Address will be $C$6:$C$7 when you clear both C6 and C7.
The main problem is this:
... And Target.Value = "No" ...
This will fail with a Type Mismatch error when Target is a multi-cell range, because then Target.Value is a 2D Variant array, which you can't compare to "No".
Also, the normal approach is to use Intersect instead of considering Target.Address.
If you're only concerned about C7, then perhaps write like this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Me.Range("C7")) Is Nothing Then
If Me.Range("C7").Value = "No" Then
On Error GoTo SafeExit
Application.EnableEvents = False ' Avoid re-triggering the event
Me.Range("C9:C11").Value = "No"
End If
End If
SafeExit:
Application.EnableEvents = True
End Sub
Consider:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" And Target.Value = "No" Then
Application.EnableEvents = False
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
Application.EnableEvents = True
End If
End Sub
and:
Sub C_B_Reset()
Application.EnableEvents = False
Sheet5.Range("C6:C7").ClearContents
Application.EnableEvents = True
End Sub
EDIT#1:
Try this event macro instead:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$C$7" Then
If Target.Value = "No" Then
Application.EnableEvents = False
Range("$C$9").Value = "No"
Range("$C$10").Value = "No"
Range("$C$11").Value = "No"
Application.EnableEvents = True
End If
End If
End Sub
I am trying to creata a VBA that gives me automatic values based on drop down list in a form. The problem is that when I run the macro then it is causing an error and excel stops working. Any help in this case is most welcome.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
First thing first, in your code, no need to put an Exit Sub before the End Sub.
The code will end after that line so this is a redundancy.
The next thing that you need to understand is that the Change Event will keep triggering if you will not disable it explicitly. So it means that when you hide a row on that Sheet, the Change Event will keep on happening since there will be changes that will happen on the Sheet. i.e. (Hiding Rows).
To do that you need to disable the EventsListeners of the application using the Application.EnableEvents = False. So the application can do a single thing based on that first event.
The next thing that you need to keep in mind is to track where the Changes occur and fire your program. Target is a Range Object that will return the Range where the specific change occurs on the Sheet.
In order to do that, you need to validate if you need to trigger the routine based on the target using the Intersect function.
The whole code is as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Intersect(Target, Range("G11")) Is Nothing Then
If Range("$G$11") = "UD Sheet" Then
Rows("20:25").EntireRow.Hidden = False
Else
Rows("21:25").EntireRow.Hidden = True
End If
End If
If Not Intersect(Target, Range("G12")) Is Nothing Then
If Range("G12").Value = "Flex Tape" Then
Range("B20").Value = "None"
Else
Range("B20").Value = ""
End If
End If
Application.EnableEvents = True
End Sub
I'm trying to clear the contents of a merged cell based on the selection from a data validation list (the list is also in merged cells). And yes, "Don't use merged cells!" is great advice (I prefer "center across") but would complicate things in this case. I've tried naming the merged cells, using "MergeArea," etc. but I haven't been successful in finding a solution yet.
Below is the latest iteration I have. Please note I've been trying to make just one selection ("Yes." in this case) work before adding in the second selection (or "blank" in this case).
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Range("Max_Advance").MergeArea Then
If Range.Validation.Type = 2 Then
Range("Days_Needed").MergeArea.ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Can anyone help? I've also attached a picture to visually show what I'm working with:
Does this work for you?
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(0, 0) = "A2" Then
On Error GoTo exitHandler
Application.EnableEvents = False
If LCase(Target.Value) <> "no. please enter." Then
Range("Days_Needed").MergeArea.ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
You can usually clear the contents of a merged cell by treating it as if it was the first cell in the merge.
for example:
Range("A2")=""
This is the first time I'm working with Macros.
I've created a dropdown in B2 with a "Yes" and "No" options.
If User selects "Yes", Row 10 Shows / Row 11 Hides
If User Selects "No", Row 11 Shows / Row 10 Hides
I used this code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
If Range("B2") = Yes Then
ActiveSheet.Rows("10:10").EntireRow.Hidden = False
ActiveSheet.Rows("11:11").EntireRow.Hidden = True
ElseIf Range("B2") = No Then
ActiveSheet.Rows("10:10").EntireRow.Hidden = True
ActiveSheet.Rows("11:11").EntireRow.Hidden = False
End If
End If
End Sub
I Created a new Module in Sheet1, and put it there. I saved the excel as a Macro Enabled Tamplate, however nothing happens when I change the dropdown.
Thanks for your help!
Do yourself a huge favor and get in the habit of writing Option Explicit at the top of every module of VBA code you write.
I have added comments as well explaining your needed revisions.
'this requires you to dimension all variables
'when you used '= yes' VBA thought you were saying
'the same as, = aVariable
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$B$2" Then
If Range("B2").Value = "Yes" Then
'You can reference the row directly on the same sheet
'and do not need ActiveSheet
Rows("10:10").EntireRow.Hidden = False
Rows("11:11").EntireRow.Hidden = True
ElseIf Range("B2").Value = "No" Then
Rows("10:10").EntireRow.Hidden = True
Rows("11:11").EntireRow.Hidden = False
End If
End If
End Sub
Also be aware this is only using "Yes" - using "yes" or "YES" will cause problems. You can use the UCase method as follows if you want to avoid these situations in the future:
If UCase(Range("B2").Value) = "YES" Then
If Range("B2") = "Yes" Then
and similarly with the "No " option