Data Sorting by String and Integer Values

rigneys

New Member
Joined
Jan 7, 2015
Messages
1
I wrote a code that allows me to sort a long worksheet of data that consists of lists of objects that contain other objects onto individual worksheets based on the main objects name. The code works well but I am trying to simplify it. It is dependent on me knowing exactly what the object name is and how many objects total there are and I will not always know that. The object name will always be text followed by a number increasing in value ex. vessel 1, vessel 2, vessel 3...... After each vessel # will be rows of other data that needs to be copied to a new worksheet until the next vessel row is reached. Here is an example of some of my data and code for more understanding. I'm trying to write a more generic version that loops through my data and copies the list under each numbered vessel and pastes it to a new worksheet without me having to write the exact name.
For example:
Dim V As String
Dim i As Integer
lastrow = (Cells(Rows.Count, "A").End(xlUp).Row)
ThisValue = Cells(i, "A").Value
V = "Vessel"
For i=1 To lastrow
If ThisValue = V & i Then .........

But I'm having no luck so far

Below is an example of my current code I'm trying to simplify:

Sub DataSortingByVessel()
Dim rng1 As Range, rng2 As Range, Rng3 As Range
Dim lastrow As Long
Dim i As Long


lastrow = (Cells(Rows.Count, "A").End(xlUp).Row)


For i = 2 To lastrow
ThisValue = Cells(i, "A").Value
'Vessel 1
If ThisValue = "Vessel 1" Then
Set rng1 = Range(Cells(i, "A"), (Cells(i, "A").End(xlToRight)))


ElseIf ThisValue = "Blood Vessel 2" Then
Set rng2 = Range(Cells(i, "A"), (Cells(i, "A").End(xlToRight)))
Set Rng3 = Range(rng1, rng2.Offset(-1))
Rng3.Copy
ActiveSheet.Name = "Imported Data"
Worksheets.Add After:=Worksheets(Sheets.Count)
With ActiveSheet
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
.Paste
.Name = "BV1"

End With
Sheets("Imported Data").Select
'Vessel 2
ElseIf ThisValue = "Vessel 3" Then
Set rng1 = Range(Cells(i, "A"), (Cells(i, "A").End(xlToRight)))
Set Rng3 = Range(rng2, rng1.Offset(-1))
Rng3.Copy
Worksheets.Add After:=Worksheets(Sheets.Count)

With ActiveSheet
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
.Paste
.Name = "BV2"

End With
Sheets("Imported Data").Select
'Vessel 3
ElseIf ThisValue = "Vessel 4" Then
Set rng2 = Range(Cells(i, "A"), (Cells(i, "A").End(xlToRight)))
Set Rng3 = Range(rng1, rng2.Offset(-1))
Rng3.Copy
Worksheets.Add After:=Worksheets(Sheets.Count)
With ActiveSheet
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
.Paste
.Name = "BV3"


End With
Sheets("Imported Data").Select
'Last Vessel
Set rng1 = Range(rng2, Range("A1").End(xlDown))
rng1.Copy
Worksheets.Add After:=Worksheets(Sheets.Count)
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Cells(NextRow, 1).Select
ActiveSheet.Paste
ActiveSheet.Name = "BV16"

Sheets("Imported Data").Select

End If
Next i
'Copy Title

Dim ws As Worksheet

Sheets("Imported Data").Range(Cells(1, "A"), (Cells(1, "A").End(xlToRight))).Copy
For Each ws In Worksheets
If ws.Name <> "Imported Data" Then
With ws
.Activate
.Range("A1").Select
With ActiveSheet
.Paste
.Columns("A:Z").AutoFit

End With
End With
End If
Next
End Sub
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.

Forum statistics

Threads
1,215,219
Messages
6,123,692
Members
449,117
Latest member
Aaagu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top