farmercalgary
New Member
- Joined
- Sep 22, 2020
- Messages
- 1
- Office Version
- 365
- Platform
- Windows
- MacOS
- Mobile
- Web
Help! I am trying to create a VBA Code that is pulls from a list that equals one of the following: "Transport" "Level 1/2" "Level 3" "One to One" or "Youth Work". The information starts on the 2nd row and 8th Column. I need all of the information in the row to sort into their own worksheets and start at Row 2 Column 1, however I am only able to get "Transport" to sort, and then random ones from the other list of names. Here is the code I have tried...
VBA Code:
Sub Diversion()
Dim x As Integer
Dim y As Integer
Dim i As Integer
Dim shSource As Worksheet
Dim shTarget1 As Worksheet
Dim shTarget2 As Worksheet
Dim shTarget3 As Worksheet
Dim shTarget4 As Worksheet
Dim shTarget5 As Worksheet
Set shSource = ThisWorkbook.Sheets("Main")
Set shTarget1 = ThisWorkbook.Sheets("Transport")
Set shTarget2 = ThisWorkbook.Sheets("Level 2")
Set shTarget3 = ThisWorkbook.Sheets("Level 3")
Set shTarget4 = ThisWorkbook.Sheets("One to One")
Set shTarget5 = ThisWorkbook.Sheets("Youth Work")
If shTarget1.Cells(2, 8).Value = "Transport" Then
x = 2
Else
x = shTarget1.Cells(2, 8).CurrentRegion.Rows.Count + 2
End If
If shTarget2.Cells(2, 8).Value = "Level 1/2" Then
y = 2
Else
y = shTarget2.Cells(2, 8).CurrentRegion.Rows.Count + 2
End If
If shTarget3.Cells(2, 8).Value = "Level 3" Then
y = 2
Else
y = shTarget3.Cells(2, 8).CurrentRegion.Rows.Count + 2
End If
If shTarget4.Cells(2, 8).Value = "One to One" Then
y = 2
Else
y = shTarget4.Cells(2, 8).CurrentRegion.Rows.Count + 2
End If
If shTarget5.Cells(2, 8).Value = "Youth Work" Then
y = 2
Else
y = shTarget5.Cells(2, 8).CurrentRegion.Rows.Count + 2
End If
i = 8
Do While i <= 10000
If Cells(i, 8).Value = "Transport" Then
shSource.Rows(i).Copy
shTarget1.Cells(x, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
x = x + 1
GoTo Line1
ElseIf Cells(i, 8).Value = "Level 1/2" Then
shSource.Rows(i).Copy
shTarget2.Cells(y, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
x = x + 1
GoTo Line1
ElseIf Cells(i, 8).Value = "Level 3" Then
shSource.Rows(i).Copy
shTarget3.Cells(y, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
x = x + 1
GoTo Line1
ElseIf Cells(i, 8).Value = "One to One" Then
shSource.Rows(i).Copy
shTarget4.Cells(y, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
x = x + 1
GoTo Line1
ElseIf Cells(i, 8).Value = "Youth Work" Then
shSource.Rows(i).Copy
shTarget5.Cells(y, 1).PasteSpecial Paste:=xlPasteValues
shSource.Rows(i).Delete
x = x + 1
GoTo Line1
End If
i = i + 1
Line1: Loop
End Sub
Last edited by a moderator: