尝试循环工作表时出现运行时错误

问题描述 投票:0回答:0

遇到运行时错误 1004,但无法找出问题所在。

当我在 XD1111X 工作表上单击运行时它可以工作,但不会循环到另一个工作表或在另一个工作表中单击运行时工作。将不胜感激任何建议..


    Sub LoopWorksheetsAndRunVBA()
    Dim ws As Worksheet

    Loop through specified worksheets
    For Each ws In ThisWorkbook.Worksheets(Array("XD1111X", "XD2222X", "XD3333X"))
        
        Activate a cell in each worksheet
        ws.Range("A1").Activate
        
        Call EachVeh(ws)
        
    Next ws

    End Sub

    Sub EachVeh(ws As Worksheet)

    wsName = ActiveSheet.Name

    Dim varResponse As Variant
    varResponse = MsgBox("Clear data?", vbYesNo, "CAUTION")
    If varResponse \<\> vbYes Then Exit Sub
    ThisWorkbook.Sheets(wsName).Range("A2:F1000").Select
    Selection.ClearContents

    Dim rng As Range, destRow As Long
    Dim shtSrc As Worksheet, shtDest As Worksheet
    Dim c As Range '-- this is used to store the single cell in the For Each loop

    Set shtSrc = Sheets("CALCULATE")
    Set shtDest = Sheets(wsName)

    destRow = 2 'Start copying to this row on destination sheet

' Set range to search for v
    Set rng = Application.Intersect(shtSrc.Range("A1:A5000"), shtSrc.UsedRange)

    For Each c In rng.Cells
    If (c.Value = wsName) Then
    c.Offset(0, 0).Resize(1, 7).Copy \_
    shtDest.Cells(destRow, 1) 'Copy a 7 cell wide block to the other sheet, paste into Column on row    destRow
    destRow = destRow + 1
    End If
    Next

    End Sub`
excel vba loops worksheet
© www.soinside.com 2019 - 2024. All rights reserved.