Cannot Hide and Show Particular Sheet on VBA - excel

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

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

InCorrect Parameter (VBA Script)

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

Running VBA set on formula cell

I have some VBA code (below) to hide and show rows based on a cell value, the VBA works when the cell is clicked in and then out however or done manually however there is a formula in the cell i need the VBA to be based on and I have tried a couple of ways but I am not getting any success and cannot get it to refresh and run the VBA. I don't really want or need to have the end user to click in any unnecessary fields - any advice would be greatly appreciated.
Private Sub Worksheet_Change(ByVal Target As Range)
ActiveSheet.Activate
Application.ScreenUpdating = False
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "0":
Rows("27:64").EntireRow.Hidden = True
End Select
End If
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "1":
Rows("27:29").EntireRow.Hidden = False
Rows("31:42").EntireRow.Hidden = False
Rows("52:64").EntireRow.Hidden = False
Rows("43:45").EntireRow.Hidden = True
Rows("46:51").EntireRow.Hidden = True
Rows("30:30").EntireRow.Hidden = True
End Select
If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "2":
Rows("27:29").EntireRow.Hidden = False
Rows("31:45").EntireRow.Hidden = False
Rows("52:64").EntireRow.Hidden = False
Rows("46:51").EntireRow.Hidden = True
Rows("30:30").EntireRow.Hidden = True
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "3":
Rows("27:31").EntireRow.Hidden = False
Rows("31:42").EntireRow.Hidden = False
Rows("46:51").EntireRow.Hidden = False
Rows("43:45").EntireRow.Hidden = True
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "4":
Rows("27:31").EntireRow.Hidden = False
Rows("32:45").EntireRow.Hidden = True
Rows("52:64").EntireRow.Hidden = True
Rows("46:51").EntireRow.Hidden = False
End Select
End If
On Error Resume Next
If Not Application.Intersect(Range("G20"), Range(Target.Address)) Is Nothing Then
Select Case Target.Value
Case Is = "5":
Rows("27:64").EntireRow.Hidden = False
End Select
End If
Application.ScreenUpdating = True
End Sub
I'm gonna assume you are checking the cell G20in order to hide/unhide rows. So we are going to do this:
First change the code in your worksheet object for this:
Option Explicit
Private Sub Worksheet_Calculate()
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
HideRows Me.Range("G20"), Me
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
This will call another procedure which will be contained in a module like this:
Option Explicit
Sub HideRows(CellValue As Long, ws As Worksheet)
With ws
Select Case CellValue
Case 0
.Rows("27:64").EntireRow.Hidden = True
Case 1
.Rows("27:29").EntireRow.Hidden = False
.Rows("31:42").EntireRow.Hidden = False
.Rows("52:64").EntireRow.Hidden = False
.Rows("43:45").EntireRow.Hidden = True
.Rows("46:51").EntireRow.Hidden = True
.Rows("30:30").EntireRow.Hidden = True
Case 2
.Rows("27:29").EntireRow.Hidden = False
.Rows("31:45").EntireRow.Hidden = False
.Rows("52:64").EntireRow.Hidden = False
.Rows("46:51").EntireRow.Hidden = True
.Rows("30:30").EntireRow.Hidden = True
Case 3
.Rows("27:31").EntireRow.Hidden = False
.Rows("31:42").EntireRow.Hidden = False
.Rows("46:51").EntireRow.Hidden = False
.Rows("43:45").EntireRow.Hidden = True
Case 4
.Rows("27:31").EntireRow.Hidden = False
.Rows("32:45").EntireRow.Hidden = True
.Rows("52:64").EntireRow.Hidden = True
.Rows("46:51").EntireRow.Hidden = False
Case 5
.Rows("27:64").EntireRow.Hidden = False
End Select
End With
End Sub
This way you can use the code for multiple worksheets if they are build the same way, you will only need to copy the code from the worksheet object to other worksheets and it will cal the HideRows which will then hide/unhide rows depending on the value of the cell G20 you can change the target on every sheet.
And as for the use of Select Case you can see that you only need one with all your cases for the value.

"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

Resources