InCorrect Parameter (VBA Script) - excel

Please help - everything seems correct but still getting incorrect parameter.
Sub Display_Tab_Sales_Revenue()
With ActiveSheet
'Visisbility of Tab Buttons
.Shapes("Revenue_Inactive").Visible = False
.Shapes("Revenue_Active").Visible = True
.Shapes("Units_Inactive").Visible = True
.Shapes("Units_Active").Visible = False
'visibility of Tab Contents
.Shapes("Map_Chart_Sales_Revenue").Visible = True
.Shapes("Line_Chart_Sales_Revenue").Visible = True
.Shapes("Map_Chart_Sales_Units").Visible = False
.Shapes("Line_Chart_Sales_Units").Visible = False
End With
End Sub
Sub Display_Tab_Sales_Units()
With ActiveSheet
'Visisbility of Tab Buttons
.Shapes("Revenue_Inactive").Visible = True
.Shapes("Revenue_Active").Visible = False
.Shapes("Units_Inactive").Visible = False
.Shapes("Units_Active").Visible = True
'visibility of Tab Contents
.Shapes("Map_Chart_Sales_Revenue").Visible = False
.Shapes("Line_Chart_Sales_Revenue").Visible = False
.Shapes("Map_Chart_Sales_Units").Visible = True
.Shapes("Line_Chart_Sales_Units").Visible = True
End With
End Sub

Sub Display_Tab_Sales_Units()
With ActiveSheet
.Shapes("Revenue_Inactive").Visible = True
.Shapes("Revenue_Active").Visible = False
.Shapes("Units_Inactive").Visible = False
.Shapes("Units_Active").Visible = True
.Shapes("Map_Chart_Sales_Revenue").Visible = False
.Shapes("Line_Chart_Sales_Revenue").Visible = False
.Shapes("Map_Chart_Sales_Units").Visible = True
.Shapes("Line_Chart_Sales_Units").Visible = True
End With
End Sub

Related

Checkboxes to hide / show tables on another worksheet

On this page you check what sections will be part of the change order and then click the command button. (The checkboxes don't line up, I'll fix it later.)
Once you click the button I want to use VBA to change a template sheet to only show the tables associated with the boxes checked. Then the template sheet is copied and named based on entries.
The problem is that the unwanted tables are not hidden.
When I check just the first box that works, also checking all boxes works.
The tables look like this:
EDIT- After using debug it seems my tables are not being identified. For example the one I have as rng1 "laborhourly" is hiding rows 3-21 instead of 17-22
Also if I change it from
rng1.CurrentRegion.EntireRow.Hidden = False
to
rng1.EntireRow.Hidden = False
it does not hide extra rows, but the table header is still there.
Private Sub CommandButton1_Click()
Dim rng As Range
Dim ars As Areas
Set wb = Sheets("New Change Order Request")
Set ws = Sheets("RFCO")
ws.Range("1:60").EntireRow.Hidden = False
Set rng1 = ws.Range("laborhourly").CurrentRegion
Set rng2 = ws.Range("laborot").CurrentRegion
Set rng3 = ws.Range("laborprem").CurrentRegion
Set rng4 = ws.Range("equip").CurrentRegion
Set rng5 = ws.Range("rental").CurrentRegion
Set rng6 = ws.Range("subs").CurrentRegion
Set rng7 = ws.Range("materials").CurrentRegion
Set rng8 = ws.Range("other").CurrentRegion
If wb.Range("c10") = True Then
ws.Range("1:15").EntireRow.Hidden = False
rng1.CurrentRegion.EntireRow.Hidden = False
ws.CommandButton1.Visible = True
Else
rng1.EntireRow.Hidden = False
rng1.EntireRow.Hidden = True
ws.CommandButton1.Visible = False
End If
If wb.Range("c11") = True Then
ws.Range("1:15").EntireRow.Hidden = False
rng2.EntireRow.Hidden = False
ws.CommandButton2.Visible = True
Else
ws.Range("1:15").EntireRow.Hidden = False
rng2.EntireRow.Hidden = True
ws.CommandButton2.Visible = False
End If
If wb.Range("c12") = True Then
rng3.EntireRow.Hidden = False
ws.CommandButton3.Visible = True
Else
rng3.EntireRow.Hidden = False
ws.CommandButton3.Visible = True
End If
If wb.Range("c13") = True Then
rng4.EntireRow.Hidden = False
ws.CommandButton4.Visible = True
Else
rng4.EntireRow.Hidden = True
ws.CommandButton4.Visible = False
End If
If wb.Range("c14") = True Then
rng5.EntireRow.Hidden = False
Worksheets("RFCO").CommandButton5.Visible = True
Else
rng5.EntireRow.Hidden = True
ws.CommandButton5.Visible = False
End If
If wb.Range("c15") = True Then
ws.Range("1:15").EntireRow.Hidden = False
ws.Range("subs").CurrentRegion.EntireRow.Hidden = False
Worksheets("RFCO").CommandButton6.Visible = True
Else
ws.Range("subs").CurrentRegion.EntireRow.Hidden = True
ws.CommandButton6.Visible = False
End If
If wb.Range("c16") = True Then
rng7.EntireRow.Hidden = False
Worksheets("RFCO").CommandButton7.Visible = True
Else
rng7.EntireRow.Hidden = True
ws.CommandButton7.Visible = False
End If
If wb.Range("c17") = True Then
rng8.EntireRow.Hidden = False
Worksheets("RFCO").CommandButton8.Visible = True
Else
rng8.EntireRow.Hidden = True
ws.CommandButton8.Visible = False
End If
MsgBox ("DONE")
End Sub

Why does my VBA code throw the Runtime Error 1004 when I am trying to have a button on another worksheet run this macro? Below is the code processed

Below is the code I want to run. I want to be able to click a button on one page and have the other page run this on the pivot table. I will also include the code for the button, which actually my be what is causing the issue.
Private Sub CommandButton1_Click()
Call OHPivot_Store80
End Sub
Sub OHPivot_Store80()
'
' OHPivot_Store80 Macro
'
'
ActiveSheet.PivotTables("PivotTable1").PivotFields("StoreNumber").CurrentPage _
= "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("StoreNumber")
.PivotItems("0").Visible = False
.PivotItems("6").Visible = False
.PivotItems("22").Visible = False
.PivotItems("24").Visible = False
.PivotItems("40").Visible = False
.PivotItems("44").Visible = False
.PivotItems("48").Visible = False
.PivotItems("50").Visible = False
.PivotItems("54").Visible = False
.PivotItems("56").Visible = False
.PivotItems("58").Visible = False
.PivotItems("60").Visible = False
.PivotItems("62").Visible = False
.PivotItems("64").Visible = False
.PivotItems("70").Visible = False
.PivotItems("72").Visible = False
.PivotItems("74").Visible = False
.PivotItems("76").Visible = False
.PivotItems("78").Visible = False
.PivotItems("82").Visible = False
.PivotItems("84").Visible = False
.PivotItems("86").Visible = False
.PivotItems("88").Visible = False
.PivotItems("90").Visible = False
.PivotItems("92").Visible = False
.PivotItems("94").Visible = False
.PivotItems("96").Visible = False
.PivotItems("98").Visible = False
.PivotItems("110").Visible = False
.PivotItems("(blank)").Visible = False
End With
End Sub
Change the code that responds to the button click to active the sheet that has the pivot table as follows:
Private Sub CommandButton1_Click()
Worksheets.Activate("NAME_OF_SHEET_WITH_PIVOT_TABLE")
Call OHPivot_Store80
End Sub

Cannot Hide and Show Particular Sheet on VBA

i have this code assigned to a command button (on sheet 5). My goal is when the command button is clicked, sheet 4 will be shown and other sheets will be hidden. But unfortunately when i run the code, it shows error:
Run time error 1004: application-defined or object-defined error
Any idea how to solve this?
Sub Sample()
Sheet1.Visible = False
Sheet2.Visible = False
Sheet3.Visible = False
Sheet4.Visible = True
Sheet5.Visible = False
Sheet6.Visible = False
Sheet7.Visible = False
Sheet8.Visible = False
Sheet4.Activate
End Sub
Have you tried something like:
Worksheets("Sheet1").Visible = True
would you try?
Sheets(1).visible = false
Sheets(2).visible = false
sheets(3).visible = false
sheets(4).visible = true
Sheets(5).visible = false
Sheets(6).visible = false
sheets(7).visible = false
sheets(8).visible = false
sheets(4).activate

"Method or data member not found" error message

So at the moment I have this piece of code and when I press the button that runs it the error "Method or data member not found" and it highlights the line "Question13.hide"
I have reused the same piece of code for all of my user forms and it only gives me the error on screen 13, why is this happening?
Private Sub CommandButton1_Click()
'Option 1
If CheckBox1.Value = True Then Worksheets("Results").Range("D54").Value = True
If CheckBox1.Value = True Then Worksheets("Results").Range("D54").Interior.ColorIndex = 10
If CheckBox1.Value = True Then Worksheets("Results").Range("D54").Font.Bold = True
If CheckBox1.Value = False Then Worksheets("Results").Range("D54").Value = False
If CheckBox1.Value = False Then Worksheets("Results").Range("D54").Interior.ColorIndex = 3
If CheckBox1.Value = False Then Worksheets("Results").Range("D54").Font.Bold = True
'Option 2
If CheckBox2.Value = True Then Worksheets("Results").Range("E54").Value = True
If CheckBox2.Value = True Then Worksheets("Results").Range("E54").Interior.ColorIndex = 10
If CheckBox2.Value = True Then Worksheets("Results").Range("E54").Font.Bold = True
If CheckBox2.Value = False Then Worksheets("Results").Range("E54").Value = False
If CheckBox2.Value = False Then Worksheets("Results").Range("E54").Interior.ColorIndex = 3
If CheckBox2.Value = False Then Worksheets("Results").Range("E54").Font.Bold = True
'Option 3
If CheckBox3.Value = True Then Worksheets("Results").Range("F54").Value = True
If CheckBox3.Value = True Then Worksheets("Results").Range("F54").Interior.ColorIndex = 10
If CheckBox3.Value = True Then Worksheets("Results").Range("F54").Font.Bold = True
If CheckBox3.Value = False Then Worksheets("Results").Range("F54").Value = False
If CheckBox3.Value = False Then Worksheets("Results").Range("F54").Interior.ColorIndex = 3
If CheckBox3.Value = False Then Worksheets("Results").Range("F54").Font.Bold = True
'Option 4
If CheckBox4.Value = True Then Worksheets("Results").Range("G54").Value = True
If CheckBox4.Value = True Then Worksheets("Results").Range("G54").Interior.ColorIndex = 10
If CheckBox4.Value = True Then Worksheets("Results").Range("G54").Font.Bold = True
If CheckBox4.Value = False Then Worksheets("Results").Range("G54").Value = False
If CheckBox4.Value = False Then Worksheets("Results").Range("G54").Interior.ColorIndex = 3
If CheckBox4.Value = False Then Worksheets("Results").Range("G54").Font.Bold = True
Question13.Hide
Question14.Show
End Sub
Rather than copying the code onto each form put this into module
Sub UpdateSheet(ByRef frm As UserForm, ByRef rng As Range)
Dim i As Integer, chk As Object
For i = 1 To 4
Set chk = frm.Controls.Item("CheckBox" & i)
rng.Value = chk.Value
rng.Font.Bold = True
If chk.Value = True Then
rng.Interior.ColorIndex = 10
Else
rng.Interior.ColorIndex = 3
End If
Set rng = rng.Offset(0, 1)
Next
End Sub
and then put this code on each form changing the "D54" and Userform2 to suit each question
Private Sub CommandButton1_Click()
' form Question 1
Dim rng As Range
Set rng = ThisWorkbook.Sheets("Results").Range("D54")
Module1.UpdateSheet Me, rng
Me.Hide
UserForm2.Show
End Sub

How to filter pivot table with multiple items?

I am trying to filter data from a pivot table. Why does the first setting work and the 2nd one does not? error is "unable to set the visible property in the PivotItem class"
should I be doing this any differently?
ultimately I want to filter based on a string and apply the correct settings to filter.
Sub Macro11()
'
ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
.PivotItems("BK").Visible = False
.PivotItems("CV").Visible = True
.PivotItems("CV R").Visible = True
.PivotItems("GC").Visible = False
.PivotItems("OS").Visible = False
.PivotItems("CL").Visible = False
.PivotItems("CL R").Visible = False
End With
ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
.PivotItems("Bk").Visible = False
.PivotItems("CV").Visible = False
.PivotItems("CV R").Visible = False
.PivotItems("GC").Visible = False
.PivotItems("OS").Visible = False
.PivotItems("CL").Visible = True
.PivotItems("CL R").Visible = True
End With
End Sub
Looks like this works...
Sub Macro11()
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
.ClearAllFilters
.EnableMultiplePageItems = True
.PivotItems("BK").Visible = False
.PivotItems("CV").Visible = True
.PivotItems("CV R").Visible = True
.PivotItems("GC").Visible = False
.PivotItems("OS").Visible = False
.PivotItems("CL").Visible = False
.PivotItems("CL R").Visible = False
End With
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
.ClearAllFilters
.EnableMultiplePageItems = True
.PivotItems("Bk").Visible = False
.PivotItems("CV").Visible = False
.PivotItems("CV R").Visible = False
.PivotItems("GC").Visible = False
.PivotItems("OS").Visible = False
.PivotItems("CL").Visible = True
.PivotItems("CL R").Visible = True
End With
End Sub
Sub test()
Dim PI As PivotItem
Dim myArray() As Variant
Dim i As Integer
myArray = Array("BK", "GC", "OS", "CL", "CL R")
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
.ClearManualFilter
.EnableMultiplePageItems = True
For i = LBound(myArray) To UBound(myArray)
.PivotItems(myArray(i)).Visible = False
Next i
End With
End Sub
or you can use another approach
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Brand")
For Each PI In .PivotItems
If PI.Name <> "CV" Then PI.Visible = False
Next PI
End With
this is what ultimately worked for me:
For Each PT In ActiveSheet.PivotTables
PTname = PT.Name
Next
With ActiveSheet.PivotTables(PTname).PivotFields("Brand")
.ClearAllFilters
.EnableMultiplePageItems = True
For Each pvtitem In .PivotItems
If pvtitem.Name = "OS" Then
pvtitem.Visible = True
Else
pvtitem.Visible = False
End If
Next
End With

Resources