I am trying to implement multiple functions into one worksheet_change. I was able to integrate 3 functions before (all pertaining to hiding/unhiding rows), however, am having trouble adding a function that allows multiples selections within a dropdown.
I have tried to add the new multiple selection code to the previously existing code and it does not give me errors, however it wont run. In a perfect world, it would keep the hiding/unhiding functions, as well as allow for multiple selections in the identified rows.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
If Not Application.Intersect(Range("C10:AA10"), Range(Target.Address))
Is Nothing Then
Select Case Target.Value
Case Is = "Select One": Rows("14:58").EntireRow.Hidden = True
Rows("10").EntireRow.Hidden = False
Case Is = "1": Rows("17:58").EntireRow.Hidden = True
Rows("14:16").EntireRow.Hidden = False
Case Is = "2": Rows("20:58").EntireRow.Hidden = True
Rows("14:19").EntireRow.Hidden = False
Case Is = "3": Rows("23:58").EntireRow.Hidden = True
Rows("14:22").EntireRow.Hidden = False
Case Is = "4": Rows("26:58").EntireRow.Hidden = True
Rows("14:25").EntireRow.Hidden = False
Case Is = "5": Rows("29:58").EntireRow.Hidden = True
Rows("14:28").EntireRow.Hidden = False
Case Is = "6": Rows("32:58").EntireRow.Hidden = True
Rows("14:31").EntireRow.Hidden = False
Case Is = "7": Rows("35:58").EntireRow.Hidden = True
Rows("14:34").EntireRow.Hidden = False
Case Is = "8": Rows("38:58").EntireRow.Hidden = True
Rows("14:37").EntireRow.Hidden = False
Case Is = "9": Rows("41:58").EntireRow.Hidden = True
Rows("14:40").EntireRow.Hidden = False
Case Is = "10": Rows("44:58").EntireRow.Hidden = True
Rows("14:43").EntireRow.Hidden = False
Case Is = "11": Rows("47:58").EntireRow.Hidden = True
Rows("14:46").EntireRow.Hidden = False
Case Is = "12": Rows("50:58").EntireRow.Hidden = True
Rows("14:49").EntireRow.Hidden = False
Case Is = "13": Rows("30:58").EntireRow.Hidden = True
Rows("14:52").EntireRow.Hidden = False
Case Is = "14": Rows("56:58").EntireRow.Hidden = True
Rows("14:55").EntireRow.Hidden = False
Case Is = "15": Rows("14:58").EntireRow.Hidden = False
End Select
End If
If Not Intersect(Range("C66:AA66"), Target) Is Nothing Then
Select Case Target.Value
Case "GBP", "USD", "Yuan", "EUR", "LRD", "Select One"
Rows("67").Hidden = True
Case "Other"
Rows("67").Hidden = False
End Select
End If
If Not Intersect(Range("C11:AA11"), Target) Is Nothing Then
Select Case Target.Value
Case "$"
Rows("13").Hidden = True
Rows("12").Hidden = False
Case "%"
Rows("13").Hidden = False
Rows("12").Hidden = True
Case "Select One"
Rows("13").Hidden = True
Rows("12").Hidden = True
End Select
End If
Dim Oldvalue As String
Dim Newvalue As String
Application.EnableEvents = True
On Error GoTo Exitsub
If Target.Row = "15",”18”,”21” Then
If Target.SpecialCells(xlCellTypeAllValidation) Is Nothing Then
GoTo Exitsub
Else: If Target.Value = "" Then GoTo Exitsub Else
Application.EnableEvents = False
Newvalue = Target.Value
Application.Undo
Oldvalue = Target.Value
If Oldvalue = "" Then
Target.Value = Newvalue
Else
If InStr(1, Oldvalue, Newvalue) = 0 Then
Target.Value = Oldvalue & ", " & Newvalue
Else:
Target.Value = Oldvalue
End If
End If
End If
End If
Application.EnableEvents = True
Exitsub:
Application.EnableEvents = True
End Sub
I want this to be able to continue hiding/unhiding the given rows based upon selections, as well as allow for multi-selects from the drop downs in the rows outlined in the code. The code does not give me errors, but the multi-select does not run
I think I see what you're trying to do and I hope these remarks can help your code. So a few comments...
Always use Option Explicit. No matter what example code you find on the webz, using this habit will be a big help to you in the future.
It's a BIG help to use intermediate variables in your code that makes the code self-documenting. There is no penalty for assigning interim values and objects, so use this to your advantage.
Separate logic blocks into separate subroutines or functions. This makes your code "functionally isolated" -- meaning that each block of code has a specific focus and if you need to change it, you're only changing it in one location. It also makes your code easier to read without scrolling up and down to get a sense of the overall logic.
In the case of your Worksheet_Change event code, I can reduce the logic into a much easier to understand flow:
Option Explicit
Private Sub Worksheet_Change(ByVal target As Range)
Dim groupsRange As Range
Dim currencyRange As Range
Dim valuesRange As Range
Set groupsRange = ActiveSheet.Range("C10:AA10")
Set currencyRange = ActiveSheet.Range("C66:AA66")
Set valuesRange = ActiveSheet.Range("C11:AA11")
If Not Intersect(groupsRange, target) Is Nothing Then
ShowActiveGroups target
ElseIf Not Intersect(currencyRange, target) Is Nothing Then
ShowCurrency target
ElseIf Not Intersect(valuesRange, target) Is Nothing Then
ShowValues target
End If
If target.Count > 1 Then Exit Sub
If (target.Row = 15) Or (target.Row = 18) Or (target.Row = 21) Then
CheckMultiSelect target
End If
End Sub
Clearly, I may not be getting the "point" of your ranges (using "groups", "currency", "values") but you should use descriptive names that make it easier to understand WHAT and WHY the logic is working on certain sections.
The code for the Subs called in the Worksheet_Change event are placed into a separate module and all of them are tagged as Public. Each of them have similar logic and there are a few things working here.
In each of the logic blocks (i.e. in the Sub code in this case) you should go through the steps of establishing exactly which worksheet is being referenced. It's critical to always fully qualify your range references (see #5). The easiest way to do that (without very long, compound statements) is to use intermediate variables.
So in each of the "Show" routines called above I'm setting up a reference to the Worksheet of the target cell (the cell that caused the Worksheet_Change event).
Dim targetWS As Worksheet
Set targetWS = target.Parent
Try to define constants for seemingly "random" numbers or values that have no real meaning outside the context of your worksheet.
In your case, you are referencing many different rows and hiding/unhiding them. I have no idea why. But if you could "name" the rows in your code, it could make more sense. Here are some examples I used:
Const RED_GROUP_1 As String = "14:58"
Const RED_GROUP_2 As String = "10"
Const GREEN_GROUP_1 As String = "17:58"
Const GREEN_GROUP_2 As String = "14:16"
So the first three "Show" routines could look something like this:
Public Sub ShowActiveGroups(ByRef target As Range)
Dim targetWS As Worksheet
Set targetWS = target.Parent
Const RED_GROUP_1 As String = "14:58"
Const RED_GROUP_2 As String = "10"
Const GREEN_GROUP_1 As String = "17:58"
Const GREEN_GROUP_2 As String = "14:16"
With targetWS
Select Case target.Value
Case "Select One"
.Rows(RED_GROUP_1).EntireRow.Hidden = True
.Rows(RED_GROUP_2).EntireRow.Hidden = False
Case 1
.Rows(GREEN_GROUP_1).EntireRow.Hidden = True
.Rows(GREEN_GROUP_2).EntireRow.Hidden = False
Case 2
.Rows("20:58").EntireRow.Hidden = True
.Rows("14:19").EntireRow.Hidden = False
' ...
Case Else
'--- what should we do if it's not a valid value?
End Select
End With
End Sub
Public Sub ShowCurrency(ByRef target As Range)
Dim targetWS As Worksheet
Set targetWS = target.Parent
Const CURRENCY_LINE As String = "67"
With targetWS
Select Case target.Value
Case "GBP", "USD", "Yuan", "EUR", "LRD", "Select One"
.Rows(CURRENCY_LINE).EntireRow.Hidden = True
Case "Other"
.Rows(CURRENCY_LINE).EntireRow.Hidden = False
Case Else
'--- what should we do if it's not a valid value?
End Select
End With
End Sub
Public Sub ShowValues(ByRef target As Range)
Dim targetWS As Worksheet
Set targetWS = target.Parent
Const MONEY_LINE As String = "13"
Const PERCENT_LINE As String = "12"
With targetWS
Select Case target.Value
Case "$"
.Rows(MONEY_LINE).EntireRow.Hidden = True
.Rows(PERCENT_LINE).EntireRow.Hidden = False
Case "%"
.Rows(MONEY_LINE).EntireRow.Hidden = False
.Rows(PERCENT_LINE).EntireRow.Hidden = True
Case "Select One"
.Rows(MONEY_LINE).EntireRow.Hidden = True
.Rows(PERCENT_LINE).EntireRow.Hidden = True
Case Else
'--- what should we do if it's not a valid value?
End Select
End With
End Sub
Finally, I always had trouble with the data-validation/multi-select code that you found on the webz. So I'm tossing in the one I use that has a couple slight mods. This code goes in the regular code module as well.
Public Sub CheckMultiSelect(ByRef target As Range)
Dim targetWS As Worksheet
Set targetWS = target.Parent
On Error Resume Next
Dim dvCheck As Range
Set dvCheck = targetWS.Cells.SpecialCells(xlCellTypeAllValidation)
If dvCheck Is Nothing Then Exit Sub
Application.EnableEvents = False
'--- only allow multi-select if the cell has defined data validation
If Not Intersect(dvCheck, target) Is Nothing Then
Dim currentValue As String
Dim oldValue As String
currentValue = target.Value
Application.Undo
oldValue = target.Value
If oldValue = vbNullString Then
target.Value = currentValue
Else
If InStr(1, oldValue, currentValue) = 0 Then
target.Value = oldValue & "," & currentValue
Else
If currentValue = vbNullString Then
target.Value = vbNullString
Else
target.Value = oldValue
End If
End If
End If
End If
Application.EnableEvents = True
End Sub
Using the code above in both the worksheet module and a regular code module, I sucessfully was able to perform some of the operations in your original question.
Related
I have an excel spreadsheet where I need to insert a data validation from a list, so far not a problem but I need to be able to select multiple entries without overwriting the previous as the normal data validation so the final result would be this:
List
Data Validation Result
Mango
Apple, Mango, Pixel
Iphone
Pixel, Apple
Pixel
Apple
Apple, Mango
Mango
Apple, Mango, Pixel
Iphone
Pixel, Apple
Pixel
I have found online a VBA code to insert in my spreadsheet to obatin the multiple selection without repetion:
Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20180510
Dim I As Integer
Dim xRgVal As Range
Dim xStrNew As String
Dim xStrOld As String
Dim xFlag As Boolean
Dim xArr
On Error Resume Next
Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
Application.EnableEvents = False
xFlag = True
xStrNew = " " & Target.Value & ","
Application.Undo
xStrOld = Target.Value
If InStr(1, xStrOld, xStrNew) = 0 Then
xStrNew = xStrNew & xStrOld & ""
Else
xStrNew = xStrOld
End If
Target.Value = xStrNew
Application.EnableEvents = True
End Sub
It kinda works but I have 2 problems:
I can select multiple choices from my data but the result is this
List
Data Validation Result
Mango
Apple, Mango, Pixel,
with the final comma
I cannot delete or empty the field if I make the wrong selection, I need to use the Erase all function on that cell and then use the dropdown function to re-extend the data validation field from the empty cells not completed so far
I'm not familiar with VBA so any help is appreciated.
I mainly use R and SQL this is a task that I need to do for another person in my office that is going to use this spreadsheet and need to use this function with the lowest difficulty.
Any suggestions?
I have modified the code to add the space and comma only if it actually needs to join 2 strings together. So the first value does not have a comma attached until a second value is also selected.
I have also modified it to allow cells to be cleared. Pressing Delete will now properly allow the user to clear a cell.
Private Sub Worksheet_Change(ByVal Target As Range)
'UpdatebyExtendoffice20180510
Dim I As Integer
Dim xRgVal As Range
Dim xStrNew As String
Dim xStrOld As String
Dim xFlag As Boolean
Dim xArr
On Error Resume Next
Set xRgVal = Cells.SpecialCells(xlCellTypeAllValidation)
If (Target.Count > 1) Or (xRgVal Is Nothing) Then Exit Sub
If Intersect(Target, xRgVal) Is Nothing Then Exit Sub
Application.EnableEvents = False
xFlag = True
xStrNew = Target.Value
Application.Undo
xStrOld = Target.Value
If xStrNew <> "" Then
If InStr(1, xStrOld, xStrNew) = 0 Then
xStrNew = xStrNew & IIf(xStrOld <> "", ", " & xStrOld, "")
Else
xStrNew = xStrOld
End If
End If
Target.Value = xStrNew
Application.EnableEvents = True
End Sub
I left it, in-case it is being used in code that was not copied to this post, but xArr & I are declared but not used. xFlag is declared and set True but not used in any expression.
I am looking for a way to clear a specific slicer before the code below runs without clearing all slicers in the workbook. The target.address D11, D12, etc are in a table of contents (ultimately there will be about 40 of these), but if the selected slicer isn't cleared and someone clicks on a different cell that uses the same slicer, then it returns a debug message. I am also open to other ways around this issue.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Application.EnableEvents = False
If Target.Address = "$D$11" Then
Sheets("Trend In Meals").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields ("Meal Occasion")
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Meal Occasion")
.PivotItems("All Occasions - Main Meals and Between Meals").Visible = True
.PivotItems("Total Main Meals").Visible = False
.PivotItems("Breakfast (Includes Brunch)").Visible = False
.PivotItems("Lunch").Visible = False
.PivotItems("Dinner").Visible = False
.PivotItems("Between Meal Occasions").Visible = False
End With
End If
If Target.Address = "$D$12" Then
Sheets("Trend In Meals").Select
ActiveSheet.PivotTables("PivotTable3").PivotFields ("Meal Occasion")
With ActiveSheet.PivotTables("PivotTable3").PivotFields("Meal Occasion")
.PivotItems("All Occasions - Main Meals and Between Meals").Visible = False
.PivotItems("Total Main Meals").Visible = True
.PivotItems("Breakfast (Includes Brunch)").Visible = False
.PivotItems("Lunch").Visible = False
.PivotItems("Dinner").Visible = False
.PivotItems("Between Meal Occasions").Visible = False
End With
End If
Application.EnableEvents = True
End Sub
You always need one item visible, so set that before hiding the others
(untested so may need tweaking)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim addr As String, arrItems, arrCells, m, lbl, pi As PivotItem
arrCells = Array("D11", "D12", "D13", "D14", "D15", "D16") 'cells to be selected
arrItems = Array("All Occasions - Main Meals and Between Meals", _
"Total Main Meals", "Breakfast (Includes Brunch)", _
"Lunch", "Dinner", "Between Meal Occasions")
If Target.Cells.CountLarge > 1 Then Exit Sub
If Not Application.Intersect(Target, Me.Range("D11:D15")) Is Nothing Then
addr = Target.Address(False, False) 'cell address
m = Application.Match(addr, arrCells, 0) 'position in arrCells (1-based)
lbl = arrItems(m - 1) 'correspondint label (zero-based so adjust by -1)
With Sheets("Trend In Meals").PivotTables("PivotTable3").PivotFields("Meal Occasion")
.PivotItems(lbl).Visible = True 'set the visible one first
For Each e In arrItems
If e <> lbl Then .PivotItems(e) = False 'then hide the others
Next e
End With
Sheets("Trend In Meals").Select
End If
End Sub
Hi guys I’m trying to make a fill-in form where certain rows get hidden when certain options are selected
the first part of the script works perfectly but adding a second optional cell (B31) seems to be problematic
can’t get it to work
can someone please help?
this is the script I found on the internet and adapted to my purpose
Private Sub worksheet_change(ByVal Target As Range)
If Target.Address = ("$B$6") Then
If Target.Text = "Pollution" Then
Rows("21:28").EntireRow.Hidden = False
Rows("29:97").EntireRow.Hidden = True
ElseIf Target.Text = "Select option" Then
Rows("21:97").EntireRow.Hidden = True
ElseIf Target.Text = "Fire" Then
Rows("21:29").EntireRow.Hidden = True
Rows("30:42").EntireRow.Hidden = False
Rows("43:97").EntireRow.Hidden = True
ElseIf Target.Text = "Collision, Grounding & Stranding" Then
Rows("21:43").EntireRow.Hidden = True
Rows("44:57").EntireRow.Hidden = False
Rows("58:97").EntireRow.Hidden = True
ElseIf Target.Text = "Technical Failure" Then
Rows("21:58").EntireRow.Hidden = True
Rows("59:67").EntireRow.Hidden = False
Rows("68:97").EntireRow.Hidden = True
ElseIf Target.Text = "Crew member missing / Man over board" Then
Rows("21:68").EntireRow.Hidden = True
Rows("69:79").EntireRow.Hidden = False
Rows("80:97").EntireRow.Hidden = True
ElseIf Target.Text = "Injury / Fatality" Then
Rows("21:80").EntireRow.Hidden = True
Rows("81:87").EntireRow.Hidden = False
Rows("88:97").EntireRow.Hidden = True
ElseIf Target.Text = "Cargo shift / damage" Then
Rows("21:88").EntireRow.Hidden = True
Rows("89:97").EntireRow.Hidden = False
End If
If Target.Address = ("$B$31") Then
If Target.Text = "Engine room" Then
Rows("40:42").EntireRow.Hidden = True
Rows("30:39").EntireRow.Hidden = False
End If
End If
End If
End Sub
Thanks in advance
Your If blocks are nested wrongly. Try something like this - note it's easier to only unhide the rows you want after first hiding everything.
Private Sub worksheet_change(ByVal Target As Range)
If Target.Address = ("$B$6") Then
Me.Rows("21:97").EntireRow.Hidden = True 'hide everything
'...then unhide only the required rows
Select Case Target.Text
Case "Pollution":
Me.Rows("21:28").EntireRow.Hidden = False
Case "Fire":
Me.Rows("30:42").EntireRow.Hidden = False
Case "Collision, Grounding & Stranding":
Me.Rows("44:57").EntireRow.Hidden = False
Case "Technical Failure":
Me.Rows("59:67").EntireRow.Hidden = False
Case "Crew member missing / Man over board":
Me.Rows("69:79").EntireRow.Hidden = False
Case "Injury / Fatality":
Me.Rows("81:87").EntireRow.Hidden = False
Case "Cargo shift / damage":
Me.Rows("89:97").EntireRow.Hidden = False
End Select
End If
If Target.Address = ("$B$31") Then
If Target.Text = "Engine room" Then
Me.Rows("40:42").EntireRow.Hidden = True
Me.Rows("30:39").EntireRow.Hidden = False
End If
End If
End Sub
You have a predetermined patten for selecting which rows will be shown and which will not. Consequently it is possible to refactor your code so that no 'If/ElseIf/End' are required for the actions of showing/hiding rows.
There is nothing wrong with using 'if', 'if then else' etc so I'm providing the code below as an exercide in how to think differently.
I'm not a user of Excel so my apologies in advance if I've made any mistakes in the Excel object syntax.
The code compiles without errors and generates no unexpected code inspection results with RubberDuck.
Option Explicit
Private Type State
JumpRanges As Scripting.Dictionary
B6JumpTable As Scripting.Dictionary
B31JumpTable As Scripting.Dictionary
End Type
Private s As State
Private Sub worksheet_change(ByVal Target As Range)
If s.JumpRanges Is Nothing Then InitialiseJumpTables
ActiveSheet.Rows.Item("21:97").EntireRow.Hidden = True
If s.JumpRanges.exists(Target.Address) Then
ActiveSheet.Rows(s.JumpRanges.Item(Target.Address).Item(Target.Text)).EntireRow.Hidden = False
Else
Err.Raise _
17, _
"Range Error", _
"The range " & Target.Address & "does not exist in the JumpRanges dictionary"
End If
End Sub
Public Sub InitialiseJumpTables()
Set s.B6JumpTable = New Scripting.Dictionary
With s.B6JumpTable
.Add "Pollution", "21:28"
.Add "Select option", "21:97"
.Add "Fire", "30:42"
.Add "Collision, Grounding & Stranding", "44:57"
.Add "Technical Failure", "59:67"
.Add "Crew member missing / Man over board", "69:79"
.Add "Injury / Fatality", "81:87"
.Add "Cargo shift / damage", "81:87"
End With
Set s.B31JumpTable = New Scripting.Dictionary
With s.B31JumpTable
.Add "Engine room", "30:39"
End With
Set s.JumpRanges = New Scripting.Dictionary
With s.JumpRanges
.Add "$B$6", s.B6JumpTable
.Add "$B$31", s.B31JumpTable
End With
End Sub
This question already has answers here:
Why MS Excel crashes and closes during Worksheet_Change Sub procedure?
(3 answers)
Closed 4 years ago.
When I run this code and select yes in the cell "bulk" I keep receiving "please enter the number of labor hours" over and over.
Basically what my goal is to have a drop down list to show hidden rows. Then if yes is selected in another drop down list, then two additional box inputs show up
Private Sub worksheet_change(ByVal target As Range)
ActiveSheet.Activate
Rows("20:22").EntireRow.Hidden = True
Rows("23:26").EntireRow.Hidden = True
Rows("27:30").EntireRow.Hidden = True
Rows("51:56").EntireRow.Hidden = True
If Not Application.Intersect(Range("Change"), Range(target.Address)) Is Nothing Then
Select Case target.Value
Case Is = "Asset Transfer": Rows("20:22").EntireRow.Hidden = False
Rows("23:26").EntireRow.Hidden = True
Rows("27:30").EntireRow.Hidden = True
Rows("51:56").EntireRow.Hidden = True
Case Is = "Fund Lineup": Rows("27:30").EntireRow.Hidden = False
Rows("20:22").EntireRow.Hidden = True
Rows("23:26").EntireRow.Hidden = True
Rows("51:56").EntireRow.Hidden = True
Case Is = "Plan Merge": Rows("23:26").EntireRow.Hidden = False
Rows("20:22").EntireRow.Hidden = True
Rows("27:30").EntireRow.Hidden = True
Rows("51:56").EntireRow.Hidden = True
Case Is = "Loans": Rows("51:56").EntireRow.Hidden = False
Rows("27:30").EntireRow.Hidden = True
Rows("20:22").EntireRow.Hidden = True
Rows("23:26").EntireRow.Hidden = True
Rows("28:31").EntireRow.Hidden = True
End Select
End If
Set target = Range("bulk")
If target.Value = "Yes" Then
Dim QtyEntry As Integer
Dim Msg As String
Msg = "Please enter the number of labor hours'"
QtyEntry = InputBox(Msg)
ActiveSheet.Range("c60").Value = QtyEntry
Dim Entry As Integer
Dim Msg1 As String
Msg1 = "Enter percentage increase'"
Entry = InputBox(Msg1)
ActiveSheet.Range("d60").Value = Entry
End If
End Sub
Once your cell has been changed, you can disable events right away, and then re-enable them before exiting the sub.
Also, you start the sub by hiding columns so there is no need to hide them again in your Select Case. All you need to do here is un-hide the rows that you want to be visible.
Also #2, are you sure you don't want your 2nd if statement inside your first if statement? As is, any change will prompt your input boxes.
You can reduce your code to this, which makes your logic a little easier to follow. The main take away is to notice that nothing is done outside of the events being disabled.
Option Explicit
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("Change"), 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
'Do you want your second IF statement here?
End If
If Range(“bulk”) = "Yes" Then
Range("C60") = InputBox("Please enter the number of labor hours'")
Range("D60") = InputBox("Enter Percentage Increase'")
End If
Application.EnableEvents = True
End Sub
You will likely need to add some validation/error handling for both of your input boxes. What happens if the user puts "one" for number of labor hours? I recommend looking into Application.InputBox so you can control for the input.
I have this VBA code that worked perfectly fine before yet suddenly not anymore. It returns
application-defined or object defined error
now, when I watched the failed line, it says
expression not defined in context
Basically, what this macro does is to let people choose pivot table filters by several times in a group, rather than one by one. Below is the code, any help will be strongly appreciated.
Sub AdvancedFilter1()
Dim ws
Set ws = Workbooks("Bayer IB.xlsm").Worksheets("Pivots")
ws.Activate
Dim pv3
Dim pv4
Set pv3 = ws.PivotTables("PivotTable3").PivotFields("Shipped Date FY")
Set pv4 = ws.PivotTables("PivotTable4").PivotFields("Shipped Date FY")
Workbooks("Bayer IB.xlsm").Worksheets("Pivots").Activate
If ws.Range("B1").Activate Then
If ActiveCell.Value = "10" Then
With pv3
.PivotItems("2007").Visible = True
.PivotItems("2008").Visible = True
.PivotItems("2009").Visible = True
.PivotItems("2010").Visible = True
.PivotItems("2011").Visible = True
.PivotItems("2012").Visible = True
.PivotItems("2013").Visible = True
.PivotItems("2014").Visible = True
.PivotItems("2015").Visible = True
.PivotItems("2016").Visible = True
End With
ElseIf ActiveCell.Value = "2" Then
With pv3
.PivotItems("2007").Visible = False
.PivotItems("2008").Visible = False
.PivotItems("2009").Visible = False
.PivotItems("2010").Visible = False
.PivotItems("2011").Visible = False
.PivotItems("2012").Visible = False
.PivotItems("2013").Visible = False
.PivotItems("2014").Visible = False
.PivotItems("2015").Visible = True
.PivotItems("2016").Visible = True
End With
End If
End If
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Select Case Target.Address
Case "$B$1"
If Target = "10" Then
Call AdvancedFilter1
ElseIf Target = "2" Then
Call AdvancedFilter1
ElseIf Target = "3" Then
Call AdvancedFilter1
ElseIf Target = "5" Then
Call AdvancedFilter1
ElseIf Target = "7" Then
Call AdvancedFilter1
End If
'TO PREVENT AUTOMATIC REDIRECT TO 'J3'
Range("B1").Select
End Select
End Sub
Just want to add an update to this question... I used the same code in another file, and it worked there. I still don't know why it won't work before, but this code is working out now, though in another workbook...
Thanks anyone who took time reading this and #findwindow's assistant.