简体   繁体   English

循环不起作用 - Excel VBA

[英]Loop not working - Excel VBA

For some reason this Array isnt working!出于某种原因,这个数组不起作用! What could be wrong?可能有什么问题? Basically it is supposed to loop through every worksheet and give the same header to each worksheet.基本上它应该遍历每个工作表并为每个工作表提供相同的标题。

WorksheetNames = Array("Sheet1", "Sheet2")
For Each ws In WorksheetNames
    With Worksheets(ws)
        .Range("F1").FormulaR1C1 = "PSTRIK"
        .Range("A1").FormulaR1C1 = "PRECID"
        .Range("C1").FormulaR1C1 = "PEXCH"
        .Range("J1").FormulaR1C1 = "PQTY"
        .Range("G1").FormulaR1C1 = "PCTYM"
        .Range("D1").FormulaR1C1 = "PFC"
        .Range("B1").FormulaR1C1 = "PACCT"
        .Range("K1").FormulaR1C1 = "PPRTCP"
        .Range("E1").FormulaR1C1 = "PSUBTY"
        .Range("H1").FormulaR1C1 = "PSBCUS"
        .Range("I1").FormulaR1C1 = "PBS"
    End With
Next ws

I suspect something like this is what you're looking for:我怀疑这样的事情就是你要找的:

Sub tgr()

    Dim ws As Worksheet
    Dim aHeaders As Variant

    aHeaders = Array("PRECID", "PACCT", "PEXCH", "PFC", "PSUBTY", "PSTRIK", "PCTYM", "PSBCUS", "PBS", "PQTY", "PPRTCP")

    For Each ws In ActiveWorkbook.Sheets
        Select Case ws.Name
            'any worksheet names listed here won't have their headers updated
            Case "NoUpdate", "Leave Alone"
                'Do nothing

            'Update headers for all other sheets
            Case Else
                ws.Range("A1").Resize(, UBound(aHeaders) - LBound(aHeaders) + 1).Value = aHeaders
        End Select
    Next ws

End Sub

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

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