在下面的代码中,我们可以将 Sheet1-Sheet4 置于选择模式并进行复制。 但是这里的重点是 Sheets的数量各不相同。每次更改文件时,“张数”低或高。我只想复制 Sheet1 .... n ,而不是所有工作表。 (每个具有“ Sheet” 名称的工作表。 如何针对此问题更正此代码?
Worksheets(Array("Sheet1", "Sheet2", "Sheet4")).Copy
答案 0 :(得分:3)
Sub m()
Dim nSht As Long
ReDim shts(1 To Worksheets.Count) As String
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name Like "Sheet*" Then
nSht = nSht + 1
shts(nSht) = sht.Name
End If
Next
If nSht > 0 Then
ReDim Preserve shts(1 To nSht)
Worksheets(shts).Copy
End If
仅作记录,这是第一个破坏解决方案的
Dim sht As Worksheet
For Each sht In Worksheets
If sht.Name Like "Sheet*" Then
If Not ActiveSheet.Name Like "Sheet*" Then sht.Activate
sht.Select False
End If
Next
ThisWorkbook.Windows(1).SelectedSheets.Copy