I want to print all the sheets in the workbook I am working in except for Three specific sheets called "Front Page", "Data" and "Logs". I've tried with an "and"- and an "or"-statement and wrapped parenthesis around and nothing worked.
Here's the code:
Sub printsheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Front Page" And _
ws.Name <> "Data" And _
ws.Name <> "Logs" Then
ws.PrintOut
End If
Next ws
End Sub
It appears that you have hidden worksheets in your workbook.
Before printing them, you need to unhide them, or check if the worksheet is hidden. Either manually, or (especially if your sheets were hidden programmatically with xlSheetVeryHidden):
ws.Visible = xlSheetVisible ' ADD THIS LINE TO UNHIDE A SHEET
ws.PrintOut
Or
If ws.Visible = xlSheetVisible Then
ws.PrintOut
End if
Try something like this if you do not want to print out hidden worksheets:
Sub printsheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Visible = True Then
If ws.Name <> "Front Page" And _
ws.Name <> "Data" And _
ws.Name <> "Logs" Then
With ws.PageSetup
.PrintArea = "b2:g26" ' USE YOUR PRINTAREA
.Zoom = False
.FitToPagesTall = 1
.FitToPagesWide = 1
End With
ws.PrintOut
End If
End If
Next ws
End Sub
You can also use one list with the sheets that you don't want to print
Sub printsheets()
Dim dontPrint As Object
Dim ws As Worksheet
Set dontPrint = CreateObject("Scripting.Dictionary")
dontPrint.Add "Front Page", 1
dontPrint.Add "Data", 2
dontPrint.Add "Logs", 3
For Each ws In ActiveWorkbook.Worksheets
If dontPrint.Exists(ws.Name) Then
Else
ws.PrintOut
End If
Next ws
End Sub
Related
I intend to rename all the sheets with the cell "G2" value except the two sheets called "Main" and "Fixed".
The code keeps renaming the two sheets.
Sub RenameSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
'With ActiveWorkbook.ActiveSheet
If ActiveWorkbook.ActiveSheet.Name <> "Main" Or _
ActiveWorkbook.ActiveSheet.Name <> "Fixed" Then
ws.Activate
If Range("G2").Value = "" Then
ActiveSheet.Name = Range("G2").End(xlDown).Value
Else: ActiveSheet.Name = Range("G2").Value
End If
Else:
GoTo Nextsv
End If
Nextsv: Next ws
Sheets("Main").Activate
ActiveSheet.Cells(1, 1).Select
End Sub
Your code had 3 mistakes
You were using Activate and Active which produced the second error. Read this on how to avoid these.
You were checking the name of the ActiveSheet before the ws.Activate so it would always check the previous sheet.
Your conditions were with Or. Main <> Fixed so it would change their name anyways because Main is not Fixed and mets the second part of your Or and viceversa. Use And to force the code check that both conditions are met.
This is the code without using Activate or Select:
Option Explicit
Sub RenameSheets()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Main" And ws.Name <> "Fixed" Then
With ws
If .Range("G2") = vbNullString Then
.Name = .Range("G2").End(xlDown)
Else
.Name = .Range("G2")
End If
End With
End If
Next ws
End Sub
I have an issue with below code.
I want to delete all visible sheets but a certain sheet, when a user closes the workbook.
This is the code:
Private Sub workbook_BeforeClose(cancel As Boolean)
Dim ws As Worksheet
For Each ws In Workbook
If ws.Visible = xlSheetVisible Then
If ws.Name <> "Choose BU" Then ws.Delete
End If
Next ws
End Sub
It says "Object required", however I thought the worksheet per default is an object in VBA?
You could try:
Option Explicit
Sub test()
Dim ws As Worksheet
Application.DisplayAlerts = False
For Each ws In ThisWorkbook.Worksheets
With ws
If .Visible = True And .Name <> "Choose BU" Then
.Delete
End If
End With
Next ws
Application.DisplayAlerts = True
End Sub
I have a workbook and the macro doesn't work, basically just copying from cell A3 and down with values in each worksheet, then pasting each of that into a new summary worksheet in succession.
When I literally create a new workbook and copy and paste all my sheets into the new workbook everything works fine. But If I stay in the old work book it gives the error
Select Method of Worksheet class failed
It didn't fail in the other 2 workbooks I created with the exact same sheets that I copied over...why this one particular workbook?
I close all other workbooks to avoid error with ActiveWorkBook - perhaps not the best way of doing things, but it shouldn't be affecting this.
Option Explicit
Public Sub SelectItemsEstimate()
Dim ws As Worksheet
Dim LastRow As Long
For Each ws In ActiveWorkbook.Worksheets
If ws.Name <> "Business Unit Key" _
And ws.Name <> "dv" And ws.Name <> "cc" And ws.Name <> "wer" And ws.Name <> "dafd" _
And ws.Name <> "Master Sheet Summary Data" _
And ws.Name <> "Query for Macro" _
And ws.Name <> "Query for Macro 2 with Format" _
And ws.Name <> "Paste all values" _
And ws.Name <> "Summary" Then
Worksheets(ws.Name).Select
Range("A3", Range("A3").SpecialCells(xlCellTypeLastCell)).Select
Selection.Copy
With ActiveWorkbook.Worksheets("Summary")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' get last row with data in column "A"
' paste
.Range("A" & LastRow + 1).PasteSpecial Paste:=xlPasteValues
End With
End If
Next
End Sub
Select Method of Worksheet class failed
This may be caused by worksheet visibility - excel cannot select hidden or very hidden worksheet. Try this code to check whether some sheets are not visible
Sub CheckSheetVisibility()
Dim ws As Worksheet
Dim wb As Workbook
Set wb = ThisWorkbook
For Each ws In wb.Sheets
If Not ws.Visible = xlSheetVisible Then ws.Visible = xlSheetVisible
Next
End Sub
And here how you can refactor your code to avoid issues with ActiveWorkbook/ActiveSheet statements, and without using .Select/.Activate methods:
Sub SelectItemsEstimate()
Dim ws As Worksheet
Dim wb As Workbook
Dim wsToCopyTo As Worksheet
Dim LastRow As Long
Set wb = ThisWorkbook
Set wsToCopyTo = wb.Sheets("Summary")
For Each ws In wb.Sheets
If ws.Name <> "Business Unit Key" _
And ws.Name <> "dv" And ws.Name <> "cc" And ws.Name <> "wer" And ws.Name <> "dafd" _
And ws.Name <> "Master Sheet Summary Data" _
And ws.Name <> "Query for Macro" _
And ws.Name <> "Query for Macro 2 with Format" _
And ws.Name <> "Paste all values" _
And ws.Name <> "Summary" Then
With ws
Range(.Cells(3, 1), .Cells(Rows.Count, 1).End(xlUp)).Copy
End With
wsToCopyTo.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlPasteValues
End If
Next
End Sub
I don't know why am getting out of range subscript error. When I click on combobox1 and select an item, MaternityForm combobox is populated with worksheets in my workbook. Then I want to hide other worksheets apart from the one selected in MaternityForm. The active sheet will then receive data from userform but I am getting subscript out of range error..
Private Sub Get_Data_Click()
Dim ws As Worksheet
Dim xWs As Worksheet
For Each xWs In Application.ActiveWorkbook.Worksheets
xWs.Visible = True
Next
Set ws = Worksheets(MaternityForm.Value)
Sheets(MaternityForm.Value).Activate
On Error Resume Next
For Each ws In Application.ActiveWorkbook.Worksheets
if ws.Name <> MaternityForm.Value Then
ws.Visible = xlSheetHidden
End If
Next
With Sheets(MaternityForm.Value)
.Range("B3").Value = Me.NameBox.Text
.Range("f3").Value = Me.PaynoBox.Text
.Range("B6").Value = Me.DTPicker1.Value
.Range("B7").Value = Me.DTPicker2.Value
.Range("B17").Value = Me.FirstPayBox.Value
.Range("B18").Value = Me.SecondPayBox.Value
.Range("B25").Value = Me.MonthlyPayBox.Value
.Range("H7").Value = Me.DTPicker3.Value
End With
End Sub
You are confusing your variables ws and xWs.
ws is referring to a specific sheet while xWs is your variable worksheet.
Therefore, your second loop is invalid (This is like saying For Each Sheet1 in Worksheets).
You need to loop through your variable worksheets and compare them to your specific sheet
For Each xWs In Application.ActiveWorkbook.Worksheets
if xWs.Name <> ws.Name Then
xWs.Visible = xlSheetHidden
End If
Next
With that being said, there is no need to loop twice.
Note that ws.Name = MaterityForm.Value will return either TRUE or FALSE. The result of this determines ws.Visible = TRUE/FALSE
Private Sub Get_Data_Click()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Sheets
ws.Visible = ws.Name = MaternityForm.Value
Next ws
With Sheets(MaternityForm.Value)
.Range("B3").Value = Me.NameBox.Text
.Range("f3").Value = Me.PaynoBox.Text
.Range("B6").Value = Me.DTPicker1.Value
.Range("B7").Value = Me.DTPicker2.Value
.Range("B17").Value = Me.FirstPayBox.Value
.Range("B18").Value = Me.SecondPayBox.Value
.Range("B25").Value = Me.MonthlyPayBox.Value
.Range("H7").Value = Me.DTPicker3.Value
End With
End Sub
I am trying to copy and paste information to certain worksheets. Most of the worksheet names I already know, but there could be one new worksheet added in, which I wouldn't know the name of.
There are 3 worksheets (let's call them WorkSheet1, WorkSheet2, and WorkSheet3) that I don't want to change. I want to copy some values from WorkSheet2 and paste them in all worksheets except 1-3. So far, I have an IF statement with the worksheets I know the names of (WorksheetX,Y, and Z). I am having trouble with the last instance where I wouldn't know the worksheet's name. I have been thinking of using an Or. Here is what I have so far:
Dim ws As Worksheet
Dim og As Worksheet
Set og = Sheets("WorkSheet2")
For Each ws In Worksheets
If ws.Name = "WorkSheetX" Then
og.Range("A1").Copy
Sheets("WorkSheetX").Range("L4").PasteSpecial
ElseIf ws.Name = "WorkSheetY" Then
og.Range("A1").Copy
Sheets("WorkSheetY").Range("L4").PasteSpecial
ElseIf ws.Name = "WorkSheetZ" Then
og.Range("A1").Copy
Sheets("WorkSheetZ").Range("L4").PasteSpecial
'This is where I am having trouble. I won't know the name of the new sheet
'Just that it wont be WorkSheet1,2,3,X,Y, or Z
ElseIf (ws.Name <> "WorkSheet1" Or ws.Name <> "WorkSheet2"
Or ws.Name <> "WorkSheet3" Or ws.Name <> "WorkSheetX" Or
ws.Name <> "WorkSheetY" Or ws.Name <> "WorkSheetZ") Then
og.Range("A1").Copy
ws.Range("L4").PasteSpecial
End If
Next
Here is a simple code to accomplish your task.
Dim ws As Worksheet
'Change the sheet names you don't want to perform you copy_paste
Sheets(Array("Sheet1", "Sheet2", "Sheet4", "Sheet_n")).Visible = False
For Each ws In Application.ThisWorkbook.Worksheets
If ws.Visible = True Then
ws.Range("L4").Value = ws.Range("A1").Value
End If
Next ws
Sheets(Array("Sheet1", "Sheet2", "Sheet4", "Sheet_n")).Visible = True
A Few Worksheets
Option Explicit
Sub AFewWorksheets()
Dim ws As Worksheet
Dim og As Worksheet
Set og = Sheets("WorkSheet2")
For Each ws In Worksheets
Select Case ws.Name
Case "Worksheet1", "Worksheet2", "Worksheet3"
Case Else
og.Range("A1").Copy
ws.Range("L4").PasteSpecial
End Select
Next
End Sub