2017-02-21 43 views
0

因此,我试图完成一个宏,该宏选择具有相似名称的所有工作表,并在工作簿中的特定工作表之前移动它们。用户可以使用这些名称添加尽可能多的页面,所以我不能只使用数组函数来移动它们。这是我到目前为止有:在工作簿中移动具有相似名称的页面

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 
+2

你如何定义“相似的名字”? “发现”类似于“视觉发现”?也许最好说明你想在所有其他表之后的某个页面(“最终结果”?)*? – BruceWayne

+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") 
+0

我计算出来去另一个方向: –

+0

昏暗numtimes作为整数 昏暗X作为整数 昏暗ý作为字符串 X =的InputBox(“输入的附加功能的数量”) 对于numtimes = 1至x ActiveWorkbook.Sheets(阵列(“数据收集”,“ILI与NDE”,“屏幕截图”))复制_ Before:= ActiveWorkbook.Sheets(“MPI”) If numtimes> 1 Then y =“Data Collection(”&numtimes & “)” 否则:Y = “数据收集” 结束如果 工作表( “数据收集(” &numtimes + 1 “)”)移动_ 之后:工作表=(y)的 –

相关问题