I have an Excel 2003 workbook with a sheet that has multiple data entries entered as groups of 8 rows and multiple columns. Each data entry corresponds with a real world location (Houston, St. Louis, or Spring House) and is denoted in one of the 8 rows by one of the following strings: HOU, STL, SPH.
I want to separate the data for each location into it's own sheet. The 3 letter string is not always in the same row, and one 8 row data entries aren't always consecutive. However, the first row of each data entry always contains the string "Project Management".
My basic strategy is to search for "Project Management", get the row that contains that string, then search the following 8 rows for one of the 3 strings above. Then, copy the data to the corresponding sheet depending on which string was found.
Here is the function I am using to return the row containing "Project Management":
Here is the code I'm using to search within the 8 row range and determine if it has "HOU", "STL" or "SPH":
Both of these functions work. Now I'm trying to write the code to transfer the group of 8 rows in the data entry to a new sheet with the location name. Here is the code I'm using for that:
I get a "Subscript out of range" error on the following line:
If Not Worksheets(sText).Name <> "" Then
Also, I will need to run this code in a loop to go through a worksheet of data entries, any suggestions on how I might do this efficiently?
Thanks.
I want to separate the data for each location into it's own sheet. The 3 letter string is not always in the same row, and one 8 row data entries aren't always consecutive. However, the first row of each data entry always contains the string "Project Management".
My basic strategy is to search for "Project Management", get the row that contains that string, then search the following 8 rows for one of the 3 strings above. Then, copy the data to the corresponding sheet depending on which string was found.
Here is the function I am using to return the row containing "Project Management":
Code:
Private Function pFindDataEntry(sText As Variant) As Variant
Dim lResult As Long, rRowPos As Range
Set rRowPos = Cells.Find(What:=sText, After:=ActiveCell, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rRowPos Is Nothing Then lResult = rRowPos.Row
pFindDataEntry = lResult
Set rRowPos = Nothing
End Function
Code:
Private Function pFindLocation(sText As Variant) As Variant
Dim lRowStart As Long, rDataEntry As Range, rLocation As Range, sResult As Variant
lRowStart = pFindDataEntry("project management")
Set rDataEntry = Rows(lRowStart & ":" & lRowStart + 7)
Set rLocation = rDataEntry.Find(What:=sText, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False)
If Not rLocation Is Nothing Then sResult = rLocation.Value Else sResult = "Nothing"
pFindLocation = sResult
Set rLocation = Nothing
'this activates the location cell so it will find the next one in the sheet next time it is run
If Not sResult = "Nothing" Then _
rDataEntry.Find(What:=sText, LookIn:=xlValues, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False, SearchFormat:=False).Activate
On Error Resume Next
End Function
Code:
Public Sub CopyEntryNewSheet(sText As Variant)
Dim lRowStart As Long, rDataEntry As Range, sLocation As Variant
lRowStart = pFindDataEntry("project management")
'range that contains 8 row data entry
Set rDataEntry = Rows(lRowStart & ":" & lRowStart + 7)
With Worksheets("Project Hours")
'check if sheet exists for this location
If Not Worksheets(sText).Name <> "" Then
'No worksheet for this location - create one and copy rows
Worksheets.Add After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Name = sText
rDataEntry.EntireRow.Copy Destination:=Worksheets(sText).Range("A1")
Else
'worksheet exists
'copy rows to end of used range
rDataEntry.EntireRow.Copy Destination:=Worksheets(sText).Range("A1") _
.Offset(Worksheets(sText).UsedRange.Rows.Count, 0)
End If
End With
End Sub
If Not Worksheets(sText).Name <> "" Then
Also, I will need to run this code in a loop to go through a worksheet of data entries, any suggestions on how I might do this efficiently?
Thanks.