2017-02-21 72 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)的 –

相關問題