在工作簿中移动具有相似名称的页面

时间:2017-02-21 22:03:31

标签: excel vba excel-vba

所以我试图完成一个宏,选择所有具有相似名称的工作表,并将它们移动到工作簿中的某个工作表之前。用户可以使用这些名称添加任意数量的页面,因此我无法使用数组函数来移动它们。这就是我到目前为止所做的:

Sub Copier()

Application.DisplayAlerts = False
Application.ScreenUpdating = False


Dim x As Integer
x = InputBox("Enter Number of Additional Features")
For numtimes = 1 To x
ActiveWorkbook.Sheets(Array("Data Collection", "Findings", "Visual Findings")).Copy _
Before:=ActiveWorkbook.Sheets("Final Results")
  'Allows user to create as many pages as necessary

Dim ws As Worksheet, flg As Boolean
For Each ws In Worksheets
    If (ws.Name) Like "*Data Collection*" Then
        ws.Select Not flg
        flg = True
 End If
 Next
'Selects all sheets for "Data Collection"
'Now I need to move all of those selected before a certain sheet at the 
  beginning of the workbook
'I cant seperate the copy functions because some formulas from data collection have to carry 
over to the other copied sheets

'Sheet2.Activate
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

1 个答案:

答案 0 :(得分:0)

您可以使用:

Dim sheetNames As String
Dim ws As Worksheet, flg As Boolean
For Each ws In Worksheets
    If ws.Name Like "*Data Collection*" Then sheetNames = sheetNames & "|"
Next

If sheetNames <>"" Then ActiveWorkbook.Sheets(Split(Left(sheetNames, Len(sheetNames) - 1),"|").Move Before:=ActiveWorkbook.Sheets("Final Results")