VBA 腳本在所有工作表中循環,但不會在所有工作表中產生輸出

VBA 腳本在所有工作表中循環,但不會在所有工作表中產生輸出

這個 VBA 腳本我會循環遍歷所有工作表,但我沒有在所有工作表中看到輸出。會是什麼原因呢?

Sub A()
Dim ws As Worksheet
For Each ws In ActiveWorkbook.Worksheets
Dim ct As String
ct = ws.Range("H4").Text
If InStr(1, ct, "CELL") Then
For Each B In ws.Range("B7:B49").Cells
    If IsNumeric(B) And B <> "" Then
        Cells(B.Row, 9) = Trim(Cells(B.Row + 1, 8)) & Trim(Cells(B.Row + 2, 8))
        Cells(B.Row + 1, 8) = ""
        Cells(B.Row + 2, 8) = ""
    If B.Row > 50 Then Exit For
End If
Next B
For Each C In ws.Range("C1:C50").Cells
    If Cells(C.Row, C.Column - 1) = "" Then
        Cells(C.Row, C.Column) = ""
    End If
Next C
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = ""
Next E
For Each F In ws.Range("F1:F50").Cells
    Cells(F.Row, F.Column) = ""
Next F
For Each G In ws.Range("G1:G50").Cells
    Cells(G.Row, G.Column) = ""
Next G
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = Cells(D.Row, D.Column + 4)
    Cells(D.Row, D.Column + 4) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = Cells(E.Row, E.Column + 4)
    Cells(E.Row, E.Column + 4) = ""
Next E
End If


Dim cat As String
cat = ws.Range("I4").Text
If InStr(1, cat, "CELL") Then
For Each B In ws.Range("B7:B49").Cells
    If IsNumeric(B) And B <> "" Then
        Cells(B.Row, 10) = Trim(Cells(B.Row + 1, 9)) & Trim(Cells(B.Row + 2, 9))
        Cells(B.Row + 1, 9) = ""
        Cells(B.Row + 2, 9) = ""
    If B.Row > 50 Then Exit For
End If
Next B
For Each C In ws.Range("C1:C50").Cells
    If Cells(C.Row, C.Column - 1) = "" Then
        Cells(C.Row, C.Column) = ""
    End If
Next C
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = ""
Next E
For Each F In ws.Range("F1:F50").Cells
    Cells(F.Row, F.Column) = ""
Next F
For Each G In ws.Range("G1:G50").Cells
    Cells(G.Row, G.Column) = ""
Next G
For Each M In ws.Range("H1:H50").Cells
    Cells(M.Row, M.Column) = ""
Next M
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = Cells(D.Row, D.Column + 5)
    Cells(D.Row, D.Column + 5) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = Cells(E.Row, E.Column + 5)
    Cells(E.Row, E.Column + 5) = ""
Next E
End If



Dim cate As String
cate = ws.Range("J4").Text
If InStr(1, cate, "CELL") Then
For Each B In ws.Range("B7:B49").Cells
    If IsNumeric(B) And B <> "" Then
        Cells(B.Row, 11) = Trim(Cells(B.Row + 1, 10)) & Trim(Cells(B.Row + 2, 10))
        Cells(B.Row + 1, 10) = ""
        Cells(B.Row + 2, 10) = ""
    If B.Row > 50 Then Exit For
End If
Next B
For Each C In ws.Range("C1:C50").Cells
    If Cells(C.Row, C.Column - 1) = "" Then
        Cells(C.Row, C.Column) = ""
    End If
Next C
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = ""
Next E
For Each F In ws.Range("F1:F50").Cells
    Cells(F.Row, F.Column) = ""
Next F
For Each G In ws.Range("G1:G50").Cells
    Cells(G.Row, G.Column) = ""
Next G
For Each M In ws.Range("H1:H50").Cells
    Cells(M.Row, M.Column) = ""
Next M
For Each I In ws.Range("I1:I50").Cells
    Cells(I.Row, I.Column) = ""
Next I
For Each J In ws.Range("J1:J50").Cells
    Cells(J.Row, J.Column) = ""
Next J
For Each D In ws.Range("D1:D50").Cells
    Cells(D.Row, D.Column) = Cells(D.Row, D.Column + 6)
    Cells(D.Row, D.Column + 6) = ""
Next D
For Each E In ws.Range("E1:E50").Cells
    Cells(E.Row, E.Column) = Cells(E.Row, E.Column + 6)
    Cells(E.Row, E.Column + 6) = ""
Next E
End If
Next ws
End Sub

它循環遍歷工作表但未在 [ages 中產生輸出] 的原因可能是什麼?

答案1

您可以從各個工作表取得內容,但不能更改各個工作表的內容。

在您的腳本中,您引用 ws 作為工作表,但稍後您使用 Cells(..) 來設定其內容。您需要新增 ws.在它前面,所以你的程式碼顯示為 ws.cells(..., ...) = ""

相關內容