OilEconomist
Active Member
- Joined
- Dec 26, 2016
- Messages
- 439
- Office Version
- 2019
- Platform
- Windows
Thanks in advance for any suggestions for which I will give feedback.
Why do I get the following run time error when I don't have any of the exceptions to the the name of my worksheet
"Run-time error '1004': You typed an invalid name for a sheet or chart. Make sure that:
-The name that you typed does not exceed 31 characters.
-The name does not contain of the following characters: : \ / ? * [ or ]
-You did not leave the name blank"
Code with error
and when executed and I want to reference that sheet do I include the quotes when using "ShtName"
Also does the following code lines look okay? If the cell is NOT (1) blank, or (2) Left four characters does not equal "Date", or the cell does not contain a date in the format of "1/1/2019" or "01/01/2019", I would like to execute the code below it, if not just keep checking all of the cells until it loops around and ends on the lastrow of data.
The following is the entire code:
Why do I get the following run time error when I don't have any of the exceptions to the the name of my worksheet
"Run-time error '1004': You typed an invalid name for a sheet or chart. Make sure that:
-The name that you typed does not exceed 31 characters.
-The name does not contain of the following characters: : \ / ? * [ or ]
-You did not leave the name blank"
Code with error
Code:
ActiveSheet.Name = ShtName
and when executed and I want to reference that sheet do I include the quotes when using "ShtName"
Code:
Sheets("ShtName").Select
Worksheets("ShtName").Range("B5").Select
Also does the following code lines look okay? If the cell is NOT (1) blank, or (2) Left four characters does not equal "Date", or the cell does not contain a date in the format of "1/1/2019" or "01/01/2019", I would like to execute the code below it, if not just keep checking all of the cells until it loops around and ends on the lastrow of data.
Code:
If Cells(i, 1) <> "" _
Or Left(Cells(i, 1).Value, 4) <> "Date" _
Or Cells(i, 1) <> "Rem." _
Or Not IsDate(Cells(i, 1)) Then
The following is the entire code:
Code:
Sub ExtractData()
'Dimensioning
Dim LastRow As Long
Dim LRPHDW As Long
Dim StartRow As Long
Dim i As Long
Dim ShtName As String
'Activate the sheet with the data to be copied
Worksheets("Sheet1").Activate
'Find the last row of the entire worksheet for a search stopping point
LastRow = Cells.Find(What:="*", after:=Range("A1"), LookAt:=xlPart, _
LookIn:=xlFormulas, SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, MatchCase:=False).Row
'Find the last row of the data to be copied which will be the same for all data that _
needs to be copied
LRPHDW = Range("A5").End(xlDown).Offset(1).Row
LRPHDW = LRPHDW - 2
'Select the cell to start the data search (i.e. where the first data set _
always starts
Range("A5").Select
'Subroutine to find the desired date and copy and paste into a copy of the template worksheet
For i = 1 To LastRow
Worksheets("Sheet1").Activate
If Cells(i, 1) <> "" _
Or Left(Cells(i, 1).Value, 4) <> "Date" _
Or Cells(i, 1) <> "Rem." _
Or Not IsDate(Cells(i, 1)) Then
'Store the cell as the name of the worksheet that will be duplicated (Template) _
and where the data will pasted
ShtName = Cells(i, 1).Value
'Copy the Template worksheet after "SheetB" for the data to be copied and pasted into
Sheets("Template").Copy after:=Sheets("Start")
ActiveSheet.Name = ShtName
'Copy and paste the data
With Sheets("Sheet1")
.Range(.Cells(i + 3, 2), .Cells(LRPHDW, 14)).Copy
End With
Sheets("Start").Select
Worksheets("ShtName").Range("B5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=True
End If
Next i
End Sub