简体   繁体   中英

Repeat Excel VBA code on Each Sheet in Workbook

I have the VBA code setup to delete rows, format columns, add a heading, etc. Now I need this code to be repeated on each Sheet in the Workbook. Some Workbooks will have 1 sheet, some could have dozens. I've looked at various answers, but can't find something that works.

Here is a snippet of the code I need to have repeated on each sheet:

Sub C_FormattingWTitle_Step3_do_on_each_tab()

'Delete all blank empty rows
Dim FirstBlankCell As Long, rngFound As Range
With ActiveSheet
    Set rngFound = .Columns("G:G").Find("*", After:=.Range("G1"), searchdirection:=xlPrevious, LookIn:=xlValues)
    If Not rngFound Is Nothing Then FirstBlankCell = rngFound.Row
End With

If ActiveCell.SpecialCells(xlLastCell) <> rngFound Then
Selection.SpecialCells(xlCellTypeBlanks).Select
ActiveWindow.SmallScroll Down:=9
Selection.EntireRow.Delete
Else
Range("A1").Select
End If

'Remove all not 260563 or header in SiteID column
Dim LR As Long, i As Long
    LR = Range("G" & Rows.Count).End(xlUp).Row
    For i = LR To 2 Step -1
    If Not (Range("G" & i).Value Like "260563") And Not (Range("G" & i).Value Like "SiteID") Then Rows(i).Delete
Next i

'Remove all False values and header in Sign in Success column
Dim FR As Long, p As Long
    FR = Range("F" & Rows.Count).End(xlUp).Row
    For p = FR To 2 Step -1
    If Not (Range("F" & p).Value Like True) And Not (Range("F" & p).Value Like "SignInSuccess") Then Rows(p).Delete
Next p

'Remove shading and formatting from header row
Rows("1:1").Select
With Selection.Interior
    .Pattern = xlNone
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With

'Format date/time
Columns("A:A").Select
Selection.NumberFormat = "m/d/yyyy hh:mm:ss;@"

After the code is run on every sheet, I want to insert Save As code. Any help would be greatly appreciated.

Use a separate sub to call and execute that subroutine:

Dim wkst As Worksheet

For Each wkst In ActiveWorkbook.Worksheets
    Call C_FormattingWTitle_Step3_do_on_each_tab(wkst)

Next

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM