简体   繁体   English

在工作簿中的每个工作表上重复Excel VBA代码

[英]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. 我有VBA代码设置,可以删除行,设置格式列,添加标题等。现在,我需要在工作簿的每个图纸上重复此代码。 Some Workbooks will have 1 sheet, some could have dozens. 有些工作簿会有1张纸,有些可能会有几十张纸。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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