我有一个代码可以对页面中的某些单元格进行特定修改。我想一键将此代码应用于 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
;也就是说,不要将第二个语句嵌套在第一个语句中。