So basically I probably have a lame question. I've used VBA for about two hours now and I'm just trying to make a macro that would would create a new speadsheet and copy data from existing one.
I've dealt with compilation errors there were, but now when I try to compile the project I get a "Run-time Error 9: Subscript out of range". In the code below the error comes up in the line where Name variable gets assigned its value.
I've had a look at other threads with the same issue but with my limited knowledge of VBA I couldn't figure out what is wrong or whether this code would work at all. Any help will be greatly appreciated! Thanks everyone!
Option Explicit
Sub PointsCopy()
'Declaring variables for correct naming
Dim Pit As String
Dim RL As Integer
Dim Pattern As Integer
Dim Name As String
Dim DataBook As String
Dim DataSheet As String
Dim oBook As Workbook
Dim oSheet As Worksheet
Dim NewBook As Workbook
Dim NewSheet As Worksheet
Dim Rows As Integer
Dim Pts As String
'Figuring out active workbook and worksheet
Set oBook = ActiveWorkbook
Set oSheet = ActiveSheet
DataBook = ThisWorkbook.Name
DataSheet = ActiveSheet.Name
'Getting pit, RL and pattern name from cell A2 and assigning to variable
Error 9 comes in this line Name = Workbooks(DataBook).Sheets(DataSheet).Range("A2").Text
Name = Workbooks(DataBook).Sheets(DataSheet).Range("A2").Text
Pit = Mid(Name, 4, 2)
RL = Mid(Name, 7, 4)
Pattern = Right(Name, 4)
Pts = "" & Pit & "_" & RL & "_" & Pattern & "_pts.csv"
'Adding new workbook with a proper name
Set NewBook = Workbooks.Add
With NewBook
.SaveAs Filename:="" & Pts & ""
Set NewSheet = Workbooks(NewBook).Sheets("Sheet1")
'Activating new worksheet
NewSheet.Activate
'Adding column names to the new workbook
Range("A1").Value = "MQ2_PIT_CODE"
Range("B1").Value = "BLOCK_TOE"
Range("C1").Value = "PATTERN_NUMBER"
Range("D1").Value = "BLOCK_NAME"
Range("E1").Value = "EASTING"
Range("F1").Value = "NORTHING"
Range("G1").Value = "RL"
Range("H1").Value = "POINT_NO"
'Activate original data sheet
Workbooks(oBook).Sheets(oSheet).Activate
'Count number of data rows in the original spreadsheet
Rows = Application.Count(Range("A2:A"))
'Activate the new spreadsheet and enter pit code, block toe and pattern number
NewSheet.Activate
Range("A2:A" & Rows) = "" & Pit & ""
Range("B2:B" & Rows) = "" & RL & ""
Range("C2:C" & Rows) = "" & Pattern & ""
'Copying data for easting, northing, rl and point number from original spreadsheet to the new one
Workbooks(oBook).Sheets(oSheet).Activate
Range("C2:C" & Rows).Select
Selection.Copy
NewSheet.Activate
Range("D2").PasteSpecial Paste:=xlPasteValues
Workbooks(oBook).Sheets(oSheet).Activate
Range("E2:E" & Rows).Select
Selection.Copy
NewSheet.Activate
Range("H2").PasteSpecial Paste:=xlPasteValues
Workbooks(oBook).Sheets(oSheet).Activate
Range("G2:I" & Rows).Select
Selection.Copy
NewSheet.Activate
Range("E2").PasteSpecial Paste:=xlPasteValues
Workbooks(NewBook).Sheets(NewSheet).Save
End With
End Sub
UPDATE
I've figured out why I was having this error - I was referring to a workbook and worksheet with String type of variable, so I've changed the erroring line the following way:
Name = ActiveSheet.Range("A2").Text
No I don't get Error 9 but I'm getting Error 13: Type mismatch if the following line:
Set NewSheet = Workbooks(NewBook).Sheets("Sheet1")
Any clues on what is wrong here? Thanks again!