I am trying to reference sheet 1, sheet 2 sheet 3 for my macro. At the moment, I referenced sheet 1 but I don't know how to reference multiple sheets. I hope that in all sheets, cell w6 is checked. Many thanks in advance! :)
Hide F macro is to hide a graph names "F" and show graph "FG" when cell w6 is not empty.
Hide FG macro is to hide a graph named "FG" and show graph "F" when cell w6 is empty.
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End With
Next
End Sub
Sub HideF()
'
' HideF Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG()
'
' HideFG Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
This should do what you need:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG ws
Else
HideF ws
End If
End With
Next
End Sub
Sub HideF(wsht As Worksheet)
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG(wsht As Worksheet)
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Instead of just calling HideFG, the loop now calls it with a reference to the sheet that the loop is testing. So when HideFG is called, it 'knows' which sheet to make the changes to.
Notice that I've changed the lines where you attempt to hide the columns. Instead of setting Visible to False, you should set Hidden to True.
--------------------------------------------------------------------------------
You could also remove the need for two Hide procedures and replace them with one, where the column(s) to hide are included in the reference passed:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideColumns ws.Columns("F:G")
Else
HideColumns ws.Columns("F")
End If
End With
Next
End Sub
Sub HideColumns(rng As Range)
For i = 1 To rng.Parent.Shapes.Count
rng.Parent.Shapes(i).Visible = msoTrue
Next i
rng.Hidden = msoTrue
Application.CommandBars("Selection").Visible = False
End Sub
A final thought - presumably [W6] can change. Currently there is nothing in this code to unhide the columns if it does. You may need to consider this if changes can be made that result in the value of [W6] changing.
Try this:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End With
Next
End Sub
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG
Else
HideF
End If
End With
Next
End Sub
Sub HideF()
'
' HideF Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG()
'
' HideFG Macro
'
'
For i = 1 To ActiveSheet.Shapes.Count
ActiveSheet.Shapes(i).Visible = msoTrue
Next i
ActiveSheet.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Related
I get the run time error when I open the workbook. The open function works great without the close function, but as soon as I add the close function I get the error. Any suggestions?
Private Sub Workbook_Open()
Application.ScreenUpdating = False
For Each cell In Range("A1:Z1")
If cell.Value = "X" Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
ws.Protect "1962"
Next ws
ThisWorkbook.Protect "1962", True
ThisWorkbook.Save
End Sub
The error occurs because you protect the worksheet in the BeforeClose routine. Hence the Workbook_Open doesn't have access to update it the next time it is being opened. Try this:
Private Sub Workbook_Open()
Dim cell As Range
Application.ScreenUpdating = False
ActiveSheet.Unprotect "1962" '<<<<
For Each cell In Range("A1:Z1")
If cell.Value = "X" Then
cell.EntireColumn.Hidden = True
Else
cell.EntireColumn.Hidden = False
End If
Next cell
ActiveSheet.Protect "1962" '<<<<
End Sub
I made the macro code so that it checks cell "w6" in all worksheets when the excel file opens. When "w6" is empty, it should hide graph "FG" and only show graph "F".
When "w6" is not empty, it should hide graph "F" and only show graph "FG". I set the names of each graph as "F" and "FG". But there is an error message in the
wsht.Shapes.Range(Array("FG")).Visible = msoFalse line in the HideFG macro that "the item with the specified name wasn't found." I am sure that the graph name is "FG", but why is this happening? Is there an excel genius who can solve this?
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
With ws
If .Range("W6").Value = 0 Then
HideFG ws
Else
HideF ws
End If
End With
Next
End Sub
Sub HideF(wsht As Worksheet)
'
' HideF Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("F")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Sub HideFG(wsht As Worksheet)
'
' HideFG Macro
'
'
For i = 1 To wsht.Shapes.Count
wsht.Shapes(i).Visible = msoTrue
Next i
wsht.Shapes.Range(Array("FG")).Visible = msoFalse
Application.CommandBars("Selection").Visible = False
End Sub
Instead of calling another macro, you could just simply try the following:
If what you refer to are actually Shapes:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Shapes.Range(Array("F", "FG")).Visible = False
If ws.[W6] = 0 Then
ws.Shapes("F").Visible = True
Else
ws.Shapes("FG").Visible = True
End If
Next ws
End Sub
Or when they actually are ChartObjects then:
Private Sub Workbook_Open()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
If ws.[W6] = 0 Then
ws.ChartObjects("F").Visible = True
ws.ChartObjects("FG").Visible = False
Else
ws.ChartObjects("F").Visible = False
ws.ChartObjects("FG").Visible = True
End If
Next ws
End Sub
As per this question of yours, I feel you might not want to loop all the sheets.
In that case, change:
For Each ws In ThisWorkbook.Sheets
Into:
For Each ws In Sheets(Array("sheet1", "sheet2", "sheet3"))
Let me know if it works for you :)
create combo box that only shows certain sheet instead of all available sheets,plus the ability to click on the sheet even when hidden?
am using forms controls comboBox, In Payment Code
Private Sub cbSheet_Change()
If cbSheet.Value <> "Select a Sheet" Then
Worksheets(cbSheet.Value).Select
End If
cbSheet.Value = "Select a Sheet"
End Sub
Private Sub Worksheet_Activate()
Dim Sh As Worksheet
Me.cbSheet.Clear
For Each Sh In ThisWorkbook.Worksheets
Me.cbSheet.AddItem Sh.Name
Next Sh
End Sub
In ThisWorkBook code
Private Sub Workbook_Open()
If ActiveSheet.Name = "Master Data" Then
Worksheets("Report").Select
Worksheets("Master Data").Select
End If
End Sub
When looping through the sheets, you can make sure not to add the sheet you don't want in the combobox
Private Sub ComboBox1_Change()
Dim sh As Worksheet, s As String
s = Me.ComboBox1
If s = "" Then Exit Sub
Set sh = Sheets(s)
With sh
If .Visible = False Then
.Visible = True
End If
.Select
End With
End Sub
Private Sub Worksheet_Activate()
Dim sh As Worksheet
Application.EnableEvents = False
Me.ComboBox1.Clear
For Each sh In Sheets
If sh.Name <> "Sheet1" Then
Me.ComboBox1.AddItem sh.Name
End If
Next sh
Application.EnableEvents = True
End Sub
The code should add the hidden sheet names
Adding specific sheets the the combbox
Private Sub Worksheet_Activate()
Application.EnableEvents = False
Me.ComboBox1.Clear
With Me.ComboBox1
.AddItem "Sheet2"
.AddItem "Sheet4"
.AddItem "Sheet5"
.AddItem "Sheet6"
End With
Application.EnableEvents = True
End Sub
I have done an extensive amount of research on this topic but no luck so far.
I have moderate experience with programming.
That said my issue is in regards to reading data from a column and deleting the worksheet if certain text is read 47 times.
In Column "L" text ("n/m") is repeated 47x times. Text always begins in row 14 and always goes on until row 70. Within that range there are spaces and
"--------"
If that column has 47 "n/m" then the worksheet can be deleted and it has to be applied/repeated for the whole workbook which contained around 40 to 100 worksheets.
My code:
First try didn't work
Sub DeletingBlankPages()
Dim Ws As Worksheet
Dim nm As Range
Set nm = Ws.Range(Columns("12"))
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
nm.Select
If nm Is "n/m" Then
Application.DisplayAlerts = False
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
Second try still didnt work
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
For Each Ws In ActiveWorkbook.Worksheets
Application.DisplayAlerts = False
If Range(Columns("12")).Value < 47 > "n/m" _
Then _
Ws.Delete
Application.DisplayAlerts = True
End If
Next Ws
End Sub
If any of you with experience know how to solve this please respond.
Thank you
If I understand correctly, try this
Sub DeleteRowBasedOnCriteria()
Dim i As Long
For i = Sheets.Count To 1 Step -1
If WorksheetFunction.CountIf(Sheets(i).Range("L14:L70"), "n/m") >= 47 Then
If Sheets.Count > 1 Then
Application.DisplayAlerts = False
Sheets(i).Delete
Application.DisplayAlerts = True
Else
MsgBox "Only 1 sheet left"
Exit Sub
End If
End If
Next i
End Sub
Try this:
Sub DeleteRowBasedOnCriteria()
Dim Ws As Worksheet
Dim Counter As Integer
Application.DisplayAlerts = False
Application.ScreenUpdating = False
For Each Ws In ActiveWorkbook.Worksheets
Counter = 0
For i = 14 To 70
If Ws.Cells(i, 12) = "n/m" Then
Counter = Counter + 1
End If
Next i
If Counter >= 47 Then
Ws.Delete
End If
Next Ws
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
I have an invoice set up with validation list on a separate worksheet listing all our parts we sell. I put combo boxes on the invoice and linked them to the validation list and included code so that when box is double clicked, it will start auto completing the box using the validation list when typed. I also included code so that when this invoice is closed at end of the day, and then reopened the next day, or when shortcut key is pressed, it will clear the contents and change the invoice number.
Sometimes I need to save a, invoice to add on or change later. So I copy that worksheet and rename it with customer name. This has worked fine for over a year. But last week, when I click on any cell on the copied worksheets, it has a runtime error 1004 Method "OLEObjects" of object"_Worksheet" failed. Then the combo boxes don't work. But it only does it on the copied worksheets. The original worksheet works fine. Any suggestions? Here is the code used:
'==========================
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, _
Cancel As Boolean)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Dim wsList As Worksheet
Set ws = ActiveSheet
Set wsList = Sheets("ValidationLists")
Cancel = True
Set cboTemp = ws.OLEObjects("Parts")
On Error Resume Nex
With cboTemp
'clear and hide the combo box
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
On Error GoTo errHandler
If Target.Validation.Type = 3 Then
'if the cell contains a data validation list
Application.EnableEvents = False
'get the data validation formula
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
With cboTemp
'show the combobox with the list
.Visible = True
.Left = Target.Left
.Top = Target.Top
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = str
.LinkedCell = Target.Address
End With
cboTemp.Activate
End If
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
'=========================================
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim str As String
Dim cboTemp As OLEObject
Dim ws As Worksheet
Set ws = ActiveSheet
Application.EnableEvents = False
Application.ScreenUpdating = True
If Application.CutCopyMode Then
'allow copying and pasting on the worksheet
GoTo errHandler
End If
Set cboTemp = ws.OLEObjects("Parts")
On Error Resume Next
With cboTemp
.Top = 10
.Left = 10
.Width = 0
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
.Value = ""
End With
errHandler:
Application.EnableEvents = True
Exit Sub
End Sub
'====================================
'Optional code to move to next cell if Tab or Enter are pressed
'from code by Ted Lanham
Private Sub Parts_KeyDown(ByVal _
KeyCode As MSForms.ReturnInteger, _
ByVal Shift As Integer)
Select Case KeyCode
Case 9 'Tab
ActiveCell.Offset(0, 1).Activate
Case 13 'Enter
ActiveCell.Offset(1, 0).Activate
Case Else
'do nothing
End Select
End Sub
Set cboTemp = ws.OLEObjects("Parts") is where the problem is. It appears twice and gets flagged on both of them.