JohanGduToit
Board Regular
- Joined
- Nov 12, 2021
- Messages
- 89
- Office Version
- 365
- 2016
- Platform
- Windows
Greetings Experts,
Please assist me with an Error 91 generated in the following code on the 1st occurrence of line "For Each Sheet in ActiveWorkbook.Worksheets"
I am attempting to loop through all worksheets to see if a worksheet titled "Updated Allocation List" exist, and if it does, to delete it. I then want to delete the top 4 rows on all remaining worksheets. (I have commented out the deletion of the 4 top rows in the code below, because I also encounter an error when attempting to do run that code).
Once the abovementioned issue has been resolved I will need to add additional coding to remove all rows containing an array of text strings; but I will post a seperate thread on that issue
Please be so kind as to advise?
Many Thanks!
Please assist me with an Error 91 generated in the following code on the 1st occurrence of line "For Each Sheet in ActiveWorkbook.Worksheets"
I am attempting to loop through all worksheets to see if a worksheet titled "Updated Allocation List" exist, and if it does, to delete it. I then want to delete the top 4 rows on all remaining worksheets. (I have commented out the deletion of the 4 top rows in the code below, because I also encounter an error when attempting to do run that code).
Once the abovementioned issue has been resolved I will need to add additional coding to remove all rows containing an array of text strings; but I will post a seperate thread on that issue
Please be so kind as to advise?
Many Thanks!
VBA Code:
Public Sub FormatMarkham01(sFile As String)
'Delete Worksheet <Updated Allocation List> if sheet exist, Delete Rows 1 to 4 and Delete all Rows containing specified text
'On Error GoTo Err_FormatMarkham01
Dim xlApp As Object
Dim xlSheet As Object
Application.SetOption "Show Status Bar", True
vStatusBar = SysCmd(acSysCmdSetStatus, "Formatting Markham Sales File (Stage 1)... Please wait.")
Set xlApp = CreateObject("Excel.Application")
Set xlSheet = xlApp.Workbooks.Open(sFile).Sheets(1)
With xlApp
[B] For Each Sheet In ActiveWorkbook.Worksheets[/B]
If Sheet.Name = "Updated Allocation List" Then
.Application.DisplayAlerts = False
.Application.Worksheets("Updated Allocation List").Delete
.Application.DisplayAlerts = True
End If
Next Sheet
[B] 'For Each Sheet In ActiveWorkbook.Worksheets
' .Application.Range("1:4").EntireRow.Delete
'Next Sheet[/B]
.Application.Sheets(1).Select
.Application.Range("A1").Select
.Application.ActiveWorkbook.Save
.Application.ActiveWorkbook.Close
.Quit
End With
vStatusBar = SysCmd(acSysCmdClearStatus)
Set xlSheet = Nothing
Set xlApp = Nothing
'Exit_FormatMarkham01:
' Exit Sub
'Err_FormatMarkham01:
' vStatusBar = SysCmd(acSysCmdClearStatus)
' MsgBox Err.Number & " - " & Err.Description
' Set xlSheet = Nothing
' Set xlApp = Nothing
' Resume Exit_FormatMarkham01
End Sub