When attempting to clear a cell range in a hidden worksheet I am receiving a "Select method of range class failed error" on line .Range("A1:EC168").select
Below is a copy of my code, thanks for any advice.
Private Sub ClearAll_Click()
Dim Sheet As Worksheet
Dim CompareTool As Workbook
Dim Sheetname As String
Set CompareTool = ThisWorkbook
With Application
.DisplayAlerts = False
.ScreenUpdating = True
End With
For Each Sheet In CompareTool.Worksheets
If Left(Sheet.Name, 8) = "Scenario" Then
Sheetname = Sheet.Name
With CompareTool.Sheets(Sheetname)
.Visible = True
.Range("A1:EC168").Select
.Visible = False
End With
End If
Next Sheet
Unload Me
End Sub
You will not be able to Select anything on an inactive sheet, so the solution would be to Activate it prior to the Select statement, though since the sheet is hidden, I'm not sure what the benefit of making the selection is...
With Sheet
.Visible = True
.Activate
.Range("A1:EC168").Select
.Visible = False
End With
You don't need to select the range to delete it, just do .Range("A1:EC168").Delete. This way, you don't even need to activate or make it visible:
With Sheet
.Range("A1:EC168").Delete
End With
Related
I want see if there is a way to track changes in a newly created tab only. Below are two sets of code that I've come up with in my research. For reference, "Overview" is where I put all the inputs that the newly created tab will have and "Variance Template" is the template I copy for the newly created tab. Now, when I create a new tab using the macro, I also need the tracking code to be copied within that new tab. Does anyone know if this is possible?
Create a new tab code:
Sub AddNewTab()
'Assign Overview tab to be read as "ws"
Dim ws As Worksheet
Set ws = Worksheets("Overview")
'Assign newly created tab as "newWs" and rename it to cell D6 in Overview tab
Dim newWs As Worksheet
Set newWs = Sheets.Add(After:=ActiveWorkbook.Worksheets(ActiveWorkbook.Worksheets.Count))
newWs.Name = ws.Range("D6").Value
'Copy data from other, already created, comparison tab into new tab
Worksheets("Variance Template").Range("A1:A59").EntireRow.Copy
newWs.Range("A1").PasteSpecial
Worksheets("Variance Template").Range("A1:BJ59").Copy
newWs.Range("A1").PasteSpecial xlPasteColumnWidths
'Copy comparison name/month/year, last closed month/year, and service type
ws.Range("D6").Copy
newWs.Range("F2").PasteSpecial Paste:=xlPasteValues
ws.Range("Q8").Copy
newWs.Range("L2").PasteSpecial Paste:=xlPasteValues
ws.Range("Q12").Copy
newWs.Range("P2").PasteSpecial Paste:=xlPasteValues
'Hide gridlines in newly created tab
newWs.Activate
ActiveWindow.DisplayGridlines = False
'First disables any freezed panes, then freezes panes to row 9 & column 7
ActiveWindow.FreezePanes = False
Cells(9, 7).Select
ActiveWindow.FreezePanes = True
'Disables "ignore errors" notices
Application.ErrorCheckingOptions.BackgroundChecking = False
'Change zoom to 80%
ActiveWindow.Zoom = 80
'Change tab color to green
ActiveSheet.Tab.Color = RGB(146, 208, 80)
End Sub
Tracking Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:BJ200")) Is Nothing Then
Application.EnableEvents = False
With newWs
.Range("F3").Value = "Last Updated: " & Environ("username") & " " & Format(Now, "MM/dd/yyyy h:mm:ss_ AM/PM")
End With
End If
Application.EnableEvents = True
End Sub
I need this code to copy over every time I create a new tab using the first set of code. I'm relatively new to VBA so let me know if you need any other type of information.
Thank you.
This worked for me:
Sub AddNewTab()
Dim ws As Worksheet, wb As Workbook
Dim newWs As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("Overview")
' Variance Template is a hidden sheet (first tab position)
' zoom, freeze panes etc are already set up on this sheet
wb.Worksheets("Variance Template").Copy after:=wb.Worksheets(wb.Worksheets.Count)
Set newWs = wb.Worksheets(wb.Worksheets.Count) 'get the copy
With newWs
.Visible = True
.Name = ws.Range("D6").Value
.Tab.Color = RGB(146, 208, 80)
.Range("F2").Value = ws.Range("D6").Value
.Range("L2").Value = ws.Range("Q8").Value
.Range("P2").Value = ws.Range("Q12").Value
.Activate
End With
Application.ErrorCheckingOptions.BackgroundChecking = False
End Sub
I built a code into a command button that identified a single sheet, copies the values and the formulas, and creates a separate detached workbook that we use for ordering material. The original formula worked great and was simple when there was only 1 target sheet. I have added 4 additional sheets to be captured, the core of the formula still works and it copies/detaches a separate workbook for ordering, but now it is also creating a copy of the original workbook as well for no reason.
How can I get it to only copy and detach the sheets in the formula and not the entire workbook?
Private Sub CommandButton1_Click()
' Plain_Copy Macro '
Sheets("PROCUREMENT").Visible = True
Sheets("Request").Visible = True
Sheets("LISTS").Visible = True
Sheets("Copy").Visible = True
Dim TheActiveWindow As Window
Dim TempWindow As Window
Dim ws As Worksheet
With ActiveWorkbook
Set TheActiveWindow = ActiveWindow
Set TempWindow = .NewWindow
.Sheets(Array("REQUESTOR", "PROCUREMENT", "Request", "LISTS", "Copy")).Copy
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
With ws.UsedRange
.Copy
.PasteSpecial xlPasteFormulasAndNumberFormats
End With
TempWindow.Close
Next ws
Application.CutCopyMode = False
Application.ScreenUpdating = True
End With
I am trying to write a script which will cycle through the worksheets in my workbook and delete the worksheet if the cells directly under the strings "detected", "not detected" and "other" are empty. If there is something entered under any of the three strings the worksheet shouldn't be deleted.
I have some code (below) which will delete the worksheet if a specific cell is empty, but I need to integrate a piece to FIND any of the three strings (if they are there, they will be in column A), and to offset this to check whether the cell below is empty.
Sub DeleteEmptyWorksheets()
Dim MySheets As Worksheet
Application.DisplayAlerts = False
For Each MySheets In ActiveWorkbook.Worksheets
If MySheets.Range(“A1”) = “” Then
MySheets.Delete
End If
Next
Application.DisplayAlerts = True
End Sub
The script will be used in processing COVID19 test results, so if you can help it will be extra karma points!!
Thankyou.
Here's a code that should assist you.
Sub DeleteEmptyWorksheets()
Dim MySheets As Worksheet
Dim rngTest As Range
Dim arTest
Dim blNBFound As Boolean
arTest = Array("detected", "not detected", "other")
Application.DisplayAlerts = False
For Each MySheets In ActiveWorkbook.Worksheets
blNBFound = False
For i = LBound(arTest) To UBound(arTest)
Set rngTest = MySheets.Range("A:A").Find(arTest(i))
If Not rngTest Is Nothing Then
If Len(rngTest.Offset(1, 0)) > 0 Then
blNBFound = True
Exit For
End If
End If
Next i
If blNBFound = False Then MySheets.Delete
Next
Application.DisplayAlerts = True
End Sub
I am trying to create a macro that deletes the active sheet without displaying the prompt. Which is what the code below does...This works great until the last sheet. I get the prompt no matter what. I do not want to delete the last sheet and at the same time, I don't want the error '1004' message to come up. Is there a way to change the code above to not delete my last sheet and not display the error message at the same time?
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
If the idea is to delete the ActiveSheet and only it, this is something that will work, until there is only 1 sheet in the workbook:
Sub DeleteActiveSheet()
If ThisWorkbook.Worksheets.Count = 1 Then
Exit Sub
Else
Application.DisplayAlerts = False
ThisWorkbook.ActiveSheet.Delete
Application.DisplayAlerts = True
End If
End Sub
If the idea is to delete all worksheets, but the last, then follow this sequence:
assign a worksheet variable wksToStay from the type Worksheet and set it to the last worksheet in the workbook;
loop through all the Worksheets in the Workbook.Worksheets collection, starting from the last one;
always perform a check, whether the worksheet to be deleted wksToDelete has the same name as the wksToStay;
delete, if the name is not the same;
it will delete all the worksheets, including the hidden and the very hidden ones;
Sub DeleteAllButLast()
Dim wksToStay As Worksheet
Dim wksToDelete As Worksheet
Dim i As Long
Set wksToStay = ThisWorkbook.Worksheets(Worksheets.Count)
For i = Worksheets.Count To 1 Step -1
Set wksToDelete = ThisWorkbook.Worksheets(i)
If wksToDelete.Name <> wksToStay.Name Then
Application.DisplayAlerts = False
wksToDelete.Delete
Application.DisplayAlerts = True
End If
Next
End Sub
Test the next code, please:
Sub deleteExceptTheLastSh()
If ActiveWorkbook.Sheets.count > 1 Then
Application.DisplayAlerts = False
ActiveSheet.Delete
Application.DisplayAlerts = True
Else
MsgBox "This is the last sheet and it cannot be deleted!"
End If
End Sub
This happens because you cannot delete the last worksheet in a workbook. Is the macro you have executed with a button? If you do not like the 1004 message, one possible solution may be to create a custom error message:
Sub deleteActiveSheet()
Application.DisplayAlerts = False
On Error GoTo Error
ActiveSheet.Delete
Application.DisplayAlerts = True
Exit Sub
Error:
MsgBox "you cannot delete the last worksheet in the workbook!"
End Sub
If I understand correctly, you don't want to delete the last worksheet, and you want to avoid the error message.
You could try this:
Sub deleteallbutlast()
Application.DisplayAlerts = False
If Worksheets.Count > 1 Then
ActiveSheet.Delete
Else
End
End If
Application.DisplayAlerts = True
End Sub
Attempting to set the print area for all visible sheets in my workbook however I am getting an "object required" error on line PageSetup.PrintArea = "$B$2:$K$55
Any help on how to fix this error is appreciated.
Here is a copy of my full code.
Private Sub YesPrint_Click()
Dim Sheet As Worksheet
Dim CompareTool As Workbook
Dim Sheetname As String
Set CompareTool = ThisWorkbook
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
For Each Sheet In CompareTool.Worksheets
If Sheet.Visible = True Then
Sheet.Activate
With ActiveSheet.Name
Range("B2:K55").Select
PageSetup.PrintArea = "$B$2:$K$55"
End With
End If
Next Sheet
End Sub
It looks like there are two issues with the code provided.
First, There is a syntax error in the with statement. Each property accessed inside the with statement should have a dot before it.
Second, the ActiveSheet.Name is not the object that includes the Range and PageSetup Method/object. It's the ActiveSheet object.
The corrected code should look like:
With ActiveSheet
.Range("B2:K55").Select
.PageSetup.PrintArea = "$B$2:$K$55"
End With
The MSDN article for PageSetup provides an example as well:
https://msdn.microsoft.com/en-us/library/office/ff196103.aspx