HelloLloyd
Technical User
I keep getting "Run-time error '9': Subscript out of range." I am trying to make the code let me copy and paste a series of ranges without having to manually input the ranges and worksheets for each copy and paste operation. To try and clarify, I have 7 different worksheets that I need to get 12 ranges off of each. I want to be able to use the same set of 12 copy lines on all 7 sheets instead of having to code 84 lines of code to copy them. I hope someone can make heads or tails of this. Here is a copy of what I have....
Public SavePath As String
Public File As String
Sub Main()
Call Save_File
Call Open_File
Call Copy_Data
End Sub
Sub Save_File()
SavePath = InputBox("Save File As:"
ActiveWorkbook.SaveAs Filename:=SavePath, FileFormat:=xlWorkbookNormal
End Sub
Sub Open_File()
'Prompts the user for the file name of the file containing the data to input
Path = InputBox("Please input the file and path name of the data file:", "Open File"
'Opens the file the user specified
Application.Workbooks.Open Filename:=Path
Call Activate_File(Path)
End Sub
Sub Activate_File(Path)
File = ExtractFile(Path)
Application.Workbooks(File).Activate
Application.Worksheets("Lot 1"
.Activate
End Sub
Function ExtractFile(Path)
Position = InStrRev(Path, "\"
ExtractFile = Right$(Path, Len(Path) - Position)
End Function
Sub Copy_Data()
Counter = 0
SaveName = ExtractFile(SavePath)
For Each Sheet In Workbooks
If Sheet.Name = ActiveWorkbook.Name Then
Select Case Counter
Case 1
Name = "Lot 1 Data"
MsgBox Name
Case 2
Name = "Lot 2 Data"
MsgBox Name
Case 3
Name = "Lot 3 Data"
MsgBox Name
Case 4
Name = "Lot 5 Data"
MsgBox Name
Case 5
Name = "Lot 6 Data"
MsgBox Name
Case 6
Name = "Lot 7 Data"
MsgBox Name
Case 7
Name = "Lot 8 Data"
MsgBox Name
End Select
ActiveSheet.Range("g14:db14"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c2:az2"
ActiveSheet.Range("g33:db33"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c3:az3"
ActiveSheet.Range("g52:db52"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c4:az4"
ActiveSheet.Range("g71:db71"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c5:az5"
ActiveSheet.Range("g90:db90"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c6:az6"
ActiveSheet.Range("g109:db109"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c7:az7"
ActiveSheet.Range("g128:db128"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c8:az8"
ActiveSheet.Range("g147:db147"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c9:az9"
ActiveSheet.Range("g166:db166"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c10:az10"
ActiveSheet.Range("g185:db185"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c11:az11"
ActiveSheet.Range("g204:db204"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c12:az12"
ActiveSheet.Range("g223:db223"
.Copy Application.Workbooks(SaveName).Worksheets(Name).Range("c13:az13"
Counter = Counter + 1
Else
Application.Workbooks(File).Activate
If Sheet.Name = ActiveWorkbook.Name Then
Counter = Counter + 1
End If
End If
Next Sheet
End Sub
Public SavePath As String
Public File As String
Sub Main()
Call Save_File
Call Open_File
Call Copy_Data
End Sub
Sub Save_File()
SavePath = InputBox("Save File As:"
ActiveWorkbook.SaveAs Filename:=SavePath, FileFormat:=xlWorkbookNormal
End Sub
Sub Open_File()
'Prompts the user for the file name of the file containing the data to input
Path = InputBox("Please input the file and path name of the data file:", "Open File"
'Opens the file the user specified
Application.Workbooks.Open Filename:=Path
Call Activate_File(Path)
End Sub
Sub Activate_File(Path)
File = ExtractFile(Path)
Application.Workbooks(File).Activate
Application.Worksheets("Lot 1"
End Sub
Function ExtractFile(Path)
Position = InStrRev(Path, "\"
ExtractFile = Right$(Path, Len(Path) - Position)
End Function
Sub Copy_Data()
Counter = 0
SaveName = ExtractFile(SavePath)
For Each Sheet In Workbooks
If Sheet.Name = ActiveWorkbook.Name Then
Select Case Counter
Case 1
Name = "Lot 1 Data"
MsgBox Name
Case 2
Name = "Lot 2 Data"
MsgBox Name
Case 3
Name = "Lot 3 Data"
MsgBox Name
Case 4
Name = "Lot 5 Data"
MsgBox Name
Case 5
Name = "Lot 6 Data"
MsgBox Name
Case 6
Name = "Lot 7 Data"
MsgBox Name
Case 7
Name = "Lot 8 Data"
MsgBox Name
End Select
ActiveSheet.Range("g14:db14"
ActiveSheet.Range("g33:db33"
ActiveSheet.Range("g52:db52"
ActiveSheet.Range("g71:db71"
ActiveSheet.Range("g90:db90"
ActiveSheet.Range("g109:db109"
ActiveSheet.Range("g128:db128"
ActiveSheet.Range("g147:db147"
ActiveSheet.Range("g166:db166"
ActiveSheet.Range("g185:db185"
ActiveSheet.Range("g204:db204"
ActiveSheet.Range("g223:db223"
Counter = Counter + 1
Else
Application.Workbooks(File).Activate
If Sheet.Name = ActiveWorkbook.Name Then
Counter = Counter + 1
End If
End If
Next Sheet
End Sub