我有以下宏,我需要它在工作簿中的所有工作表上执行相同的操作,而不仅仅是活动工作表。我已经尝试了很多不同的东西,但是我没有太多经验,所以我没有太多运气。
这是宏。
Sub Tester()
Dim c As Range, rngMerge As Range
For Each c In ActiveSheet.Range("A1").Resize(1, 100).Cells
Set rngMerge = c.MergeArea
If rngMerge.Cells.Count > 1 Then
c.UnMerge
rngMerge.Value = rngMerge.Cells(1).Value
End If
c.Value = JoinUp(c.Resize(3, 1), "_")
Next c
ActiveSheet.Range("A2:A3").EntireRow.Delete
End Sub
Function JoinUp(rng As Range, Optional Delim As String = "") As String
Dim c As Range, rv As String
For Each c In rng.Cells
If Len(c.Value) > 0 Then
rv = rv & IIf(Len(rv) > 0, Delim, "") & c.Value
End If
Next c
JoinUp = rv
End Function
答案 0 :(得分:4)
Sub ProcessAll()
Dim sht as worksheet
for each sht in ActiveWorkbook.Worksheets
ProcessSheet sht
next sht
End sub
Sub ProcessSheet(sht as worksheet)
Dim c As Range, rngMerge As Range
For Each c In sht.Range("A1").Resize(1, 100).Cells
Set rngMerge = c.MergeArea
If rngMerge.Cells.Count > 1 Then
c.UnMerge
rngMerge.Value = rngMerge.Cells(1).Value
End If
c.Value = JoinUp(c.Resize(3, 1), "_")
Next c
sht.Range("A2:A3").EntireRow.Delete
End Sub
Function JoinUp(rng As Range, Optional Delim As String = "") As String
Dim c As Range, rv As String
For Each c In rng.Cells
If Len(c.Value) > 0 Then
rv = rv & IIf(Len(rv) > 0, Delim, "") & c.Value
End If
Next c
JoinUp = rv
End Function
答案 1 :(得分:2)
或使用
dim sht as worksheet
for each sht in thisworkbook.sheets
debug.print sht.name
next sht
答案 2 :(得分:1)
要循环工作簿中的所有工作表,您可以使用:
dim i as Integer
For i = 1 To ActiveWorkBook.WorkSheets.Count
' do stuff
ActiveWorkbook.Worksheets(i) ....
Next i
答案 3 :(得分:1)
尝试:
Sheets(Array("Sheet2", "Sheet1")).Select
Sheets("Sheet2").Activate
或者如果你想循环:
For Each aSheet In ActiveWorkbook.Sheets
Next aSheet