我有一個程式碼可以對頁面中的某些單元格進行特定修改。我想一鍵將此程式碼套用到 Excel 檔案中的所有頁面。我正在使用 VBA,並且是它的新手。這是我的程式碼:
Sub H() Dim B As Range, H As Range, I As Range For Each B In 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 End Sub
我想將上面的巨集套用到 Excel 工作表中的所有頁面。我怎樣才能做到這一點?
答案1
只需循環遍歷工作表(頁面),就像循環遍歷範圍內的單元格一樣:
Sub H()
Dim B As Range
For S in Sheets
For Each B In S.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) = ""
End If
If B.Row > 50 Then Exit For
Next B
Next S
End Sub
PS 讓這兩個If
陳述獨立可能更有意義;即,不要將第二個嵌套在第一個內。