Guinaba
Board Regular
- Joined
- Sep 19, 2018
- Messages
- 233
- Office Version
- 2016
- Platform
- Windows
Hi guys,
I have the following code to loop across all tabs and delete all rows after the last row with data. I am using an user form to show which tab is being deleted. However, it doesnt seem to be working.
Do I need to active each of the sheets when deleting the rows? If yes, this part is missing in my code.
I have the following code to loop across all tabs and delete all rows after the last row with data. I am using an user form to show which tab is being deleted. However, it doesnt seem to be working.
Do I need to active each of the sheets when deleting the rows? If yes, this part is missing in my code.
VBA Code:
Private Sub UserForm_Activate()
'Variable Declarations
Dim i As Integer
Set mainbook = ThisWorkbook
Dim ws As Worksheet
'Get the sheet name
Dim SheetName As String
Dim SheetIndex As Integer
Dim TotalSheets As Integer
Dim pctCompl As Single
'Turning off screen updating (to reduce screen flicker) and suppressing alerts (if encountered.)
Application.ScreenUpdating = False
Application.DisplayAlerts = False
TotalSheets = ThisWorkbook.Sheets.Count
For Each ws In ActiveWorkbook.Worksheets
SheetName = ws.Name
SheetIndex = ws.Index
Sheets(SheetIndex).Range("A1").End(xlDown).Offset(1).Resize(ActiveSheet.UsedRange.Rows.Count).EntireRow.delete
Call progress(pctCompl, SheetName)
pctCompl = WorksheetFunction.Round((SheetIndex / TotalSheets) * 100, 0)
Next
Unload UserForm1
MsgBox "Empty Roles Deleted", vbInformation
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Sub progress(pctCompl As Single, SheetName As String)
UserForm1.Text = pctCompl & "% Completed"
UserForm1.LabelSheetName = "Tab= " & SheetName
UserForm1.Bar.Width = pctCompl * 2
DoEvents
End Sub