Hi there!
(I am using Excel 2010) I have a workbook containing several worksheets. Many of the worksheets in the workbook have similar names: for example, "125005A," "125005B," "125005C," "120320A", "120320B," "130211A," "123456A," "123456B," "123456C," "123456D," and "123456E" are all in the *same* file. I want to create a macro that will prompt the user for the numeric portion of the spreadsheet name, and then copy all the spreadsheets containing that same numeric portion of the name as an *array* to a new workbook. In this example, my desired end result is:
125005A, 125005B, 125005C - Copied as an array of sheets into a new workbook
120320A, 120320B - Copied as an array of sheets into a SEPARATE new workbook
130211A - Copied into a SEPARATE new workbook
123456A, 123456B, 123456C, 123456D, and 123456E - Copied as an array of sheets into a SEPARATE new workbook
So in this example, the 1 original file containing 11 spreadsheets is parsed into 4 new workbooks. Here's the code I have so far:
Sub TestIt()
Dim arrShts()
Dim strWSName As String
Dim s As Worksheet
Dim I As Long
Set s = ActiveSheet
Do
On Error Resume Next
Application.DisplayAlerts = False
strWSName = Application.InputBox(Prompt:= _
"Please enter a dept id number ONLY. Do NOT include the 'DP'", _
Title:="Enter Dept ID", Type:=2)
On Error GoTo 0
Application.DisplayAlerts = True
If InStr(s.Name, strWSName) > 0 Then
ReDim Preserve arrShts(I)
arrShts(I) = s.Name
I = I + 1
End If
Sheets(arrShts).Copy '<--------- I get a "Type Mismatch" error at this step. Then the macro fails.
Loop Until MsgBox("Would you like copy another id?", vbYesNo) <> vbYes
(I am using Excel 2010) I have a workbook containing several worksheets. Many of the worksheets in the workbook have similar names: for example, "125005A," "125005B," "125005C," "120320A", "120320B," "130211A," "123456A," "123456B," "123456C," "123456D," and "123456E" are all in the *same* file. I want to create a macro that will prompt the user for the numeric portion of the spreadsheet name, and then copy all the spreadsheets containing that same numeric portion of the name as an *array* to a new workbook. In this example, my desired end result is:
125005A, 125005B, 125005C - Copied as an array of sheets into a new workbook
120320A, 120320B - Copied as an array of sheets into a SEPARATE new workbook
130211A - Copied into a SEPARATE new workbook
123456A, 123456B, 123456C, 123456D, and 123456E - Copied as an array of sheets into a SEPARATE new workbook
So in this example, the 1 original file containing 11 spreadsheets is parsed into 4 new workbooks. Here's the code I have so far:
Sub TestIt()
Dim arrShts()
Dim strWSName As String
Dim s As Worksheet
Dim I As Long
Set s = ActiveSheet
Do
On Error Resume Next
Application.DisplayAlerts = False
strWSName = Application.InputBox(Prompt:= _
"Please enter a dept id number ONLY. Do NOT include the 'DP'", _
Title:="Enter Dept ID", Type:=2)
On Error GoTo 0
Application.DisplayAlerts = True
If InStr(s.Name, strWSName) > 0 Then
ReDim Preserve arrShts(I)
arrShts(I) = s.Name
I = I + 1
End If
Sheets(arrShts).Copy '<--------- I get a "Type Mismatch" error at this step. Then the macro fails.
Loop Until MsgBox("Would you like copy another id?", vbYesNo) <> vbYes