S
Steved
Hello from Steved
Below works as it should.
Our IT section made changes, hence I need to ask your help
please. At the moment it looks in Col A and uses the first
numeric say 1, it moves all data associated with 1 to
sheet1 which it created 2 to Sheet2 and so on.
Ok I am not a code specialist so I require your help.
Ok I will present 2 situations
1, Can the below be change to pickup the first 2 Char
ie Sh, Sw which stands for "Shore", "Swanson"
2, I have cities City, Roskill, Papakura, Wiri, Shore,
Orewa, Swanson, Waiheke. Can this be done with the below
please.
Thankyou.
Option Explicit
Public Sub CopyRowsToSheetN()
'copies rows of data from sheet named 'Data' to sheet
'named 'Sheetn' where n is the first character of the
text
'in the first cell if any. Creates Sheetn if necessary.
Application.ScreenUpdating = False
Dim cell As Range
Dim rng As Range, oldSelection As Range
Dim wks As Worksheet, wksT As Worksheet
Set oldSelection = Selection
Set wks = ThisWorkbook.Worksheets("Data")
Set rng = Intersect(wks.Columns("A"), wks.UsedRange)
'copies the row to the new sheet at the current row
For Each cell In rng.Cells
If Len(cell.Text) > 0 Then
Set wksT = GetWorksheet(wks.Parent, "Sheet" & Left
(cell.Text, 1))
cell.EntireRow.Copy wksT.Columns("A").Cells(cell.Row)
End If
Next cell
'compresses each list to the top
On Error Resume Next
For Each wksT In wks.Parent.Worksheets
wksT.Columns("A").SpecialCells
(xlCellTypeBlanks).EntireRow.Delete xlUp
Next
Application.Goto oldSelection
Application.ScreenUpdating = True
End Sub
Private Function GetWorksheet(wkbW As Workbook, _
strName As String) As
Worksheet
'Returns the wkbW worksheet named.
'Adds it, if it doesn't exist.
Dim wks As Worksheet
On Error Resume Next
Set wks = wkbW.Worksheets(strName)
On Error GoTo 0
If (wks Is Nothing) Then
Set wks = wkbW.Worksheets.Add(After:=Worksheets
("Data"))
wks.Name = strName
End If
Set GetWorksheet = wks
Set wks = Nothing
End Function
Below works as it should.
Our IT section made changes, hence I need to ask your help
please. At the moment it looks in Col A and uses the first
numeric say 1, it moves all data associated with 1 to
sheet1 which it created 2 to Sheet2 and so on.
Ok I am not a code specialist so I require your help.
Ok I will present 2 situations
1, Can the below be change to pickup the first 2 Char
ie Sh, Sw which stands for "Shore", "Swanson"
2, I have cities City, Roskill, Papakura, Wiri, Shore,
Orewa, Swanson, Waiheke. Can this be done with the below
please.
Thankyou.
Option Explicit
Public Sub CopyRowsToSheetN()
'copies rows of data from sheet named 'Data' to sheet
'named 'Sheetn' where n is the first character of the
text
'in the first cell if any. Creates Sheetn if necessary.
Application.ScreenUpdating = False
Dim cell As Range
Dim rng As Range, oldSelection As Range
Dim wks As Worksheet, wksT As Worksheet
Set oldSelection = Selection
Set wks = ThisWorkbook.Worksheets("Data")
Set rng = Intersect(wks.Columns("A"), wks.UsedRange)
'copies the row to the new sheet at the current row
For Each cell In rng.Cells
If Len(cell.Text) > 0 Then
Set wksT = GetWorksheet(wks.Parent, "Sheet" & Left
(cell.Text, 1))
cell.EntireRow.Copy wksT.Columns("A").Cells(cell.Row)
End If
Next cell
'compresses each list to the top
On Error Resume Next
For Each wksT In wks.Parent.Worksheets
wksT.Columns("A").SpecialCells
(xlCellTypeBlanks).EntireRow.Delete xlUp
Next
Application.Goto oldSelection
Application.ScreenUpdating = True
End Sub
Private Function GetWorksheet(wkbW As Workbook, _
strName As String) As
Worksheet
'Returns the wkbW worksheet named.
'Adds it, if it doesn't exist.
Dim wks As Worksheet
On Error Resume Next
Set wks = wkbW.Worksheets(strName)
On Error GoTo 0
If (wks Is Nothing) Then
Set wks = wkbW.Worksheets.Add(After:=Worksheets
("Data"))
wks.Name = strName
End If
Set GetWorksheet = wks
Set wks = Nothing
End Function