如何调用针对 VBA 中第一个工作表以外的每个工作表的函数

How to call a function that targets every worksheet except the first one in VBA

我是 VBA 的新手,遇到了一些问题。 我基本上在一个 Excel 文件中有 4 个不同的工作表,第一个基本上是空的,除了一个按钮,我想 link 到一个宏,该宏调用一个针对所有其他工作表的函数,除了第一个(上面有按钮的那个)所以我想知道我该怎么做。

到目前为止我有这个,但它似乎没有用。在我调用的函数中,我还想只针对除第一个工作表之外的每个工作表,然后有一长串我想影响三个工作表的东西,所以我是否必须重复相同的代码?在函数调用和标题为 ProcessData 的函数中,我需要什么代码才能定位除第一个工作表之外的所有工作表,其中包含一长串用于转换这些工作表上的数据的内容?

Sub DumpOutput()
    Dim WS As Worksheet
For Each WS In ThisWorkbook.Worksheets
    If WS.Name <> "Main" Then
        ProcessData WS
    End If
Next WS
End Sub

Sub ProcessData(ByRef w As Worksheet)
With w
    Range("f1:f3") = Application.Transpose(Array("1", "2", "3"))
    Debug.Print .Name
End With
End Sub
Sub FunctionCaller()
    Dim WS As Worksheet
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> ThisWorkbook.Worksheets(1).Name Then
            Call ProcessData
        End If
    Next WS
End Sub

您将要将每个工作表对象传递到处理子过程中。

Sub FunctionCaller()
    Dim WS As Worksheet
    For Each WS In ThisWorkbook.Worksheets
        If WS.Name <> "Main" Then
            ProcessData ws
        End If
    Next WS
End Sub

sub ProcessData(byref w as worksheet)
    with w
        'process the worksheet here.
        'any worksheet object members (e.g. Range, Cells, Name, etc) need to
        'be prefixed with a period (.) to be associated with the parent
        'worksheet defined in the With ... End With and passed into the
        'procedure as the parameter (e.g. .Range, .Cells, .Name, etc) 
        .Range("f1:f3") = Application.Transpose(Array(1, 2, 3))
        debug.print .name
    end with
end sub