Forum Discussion
NateFromFun
Sep 29, 2020Copper Contributor
Macro for Moving Rows to New Tab
Hey there- Hope you're having a great day so far! I have an excel file that is several thousand lines long. My columns are: Item title price image url website category What I need help wit...
- Sep 29, 2020
Here is a macro. It uses column D as category column. If you are using another column for this, change the constant at the beginning of the code accordingly.
The code assumes that row 1 contains column headings.
Sub SplitData() Const strCol = "D" ' the column with categories Dim wshS As Worksheet Dim wshT As Worksheet Dim r As Long Dim r0 As Long Dim m As Long Dim ID Application.ScreenUpdating = False Set wshS = ActiveSheet m = wshS.Range(strCol & Rows.Count).End(xlUp).Row wshS.Range("1:" & m).Sort Key1:=wshS.Range(strCol & "1"), Header:=xlYes r = 2 Do If wshS.Range(strCol & r).Value <> wshS.Range(strCol & r - 1).Value Then Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count)) wshT.Name = wshS.Range(strCol & r).Value wshT.Range("1:1").Value = wshS.Range("1:1").Value ID = wshS.Range(strCol & r).Value r0 = r Do While wshS.Range(strCol & r + 1).Value = ID r = r + 1 Loop wshT.Range("2:" & r - r0 + 2).Value = wshS.Range(r0 & ":" & r).Value End If r = r + 1 Loop Until r > m Application.ScreenUpdating = True End Sub
HansVogelaar
Sep 29, 2020MVP
Here is a macro. It uses column D as category column. If you are using another column for this, change the constant at the beginning of the code accordingly.
The code assumes that row 1 contains column headings.
Sub SplitData()
Const strCol = "D" ' the column with categories
Dim wshS As Worksheet
Dim wshT As Worksheet
Dim r As Long
Dim r0 As Long
Dim m As Long
Dim ID
Application.ScreenUpdating = False
Set wshS = ActiveSheet
m = wshS.Range(strCol & Rows.Count).End(xlUp).Row
wshS.Range("1:" & m).Sort Key1:=wshS.Range(strCol & "1"), Header:=xlYes
r = 2
Do
If wshS.Range(strCol & r).Value <> wshS.Range(strCol & r - 1).Value Then
Set wshT = Worksheets.Add(After:=Worksheets(Worksheets.Count))
wshT.Name = wshS.Range(strCol & r).Value
wshT.Range("1:1").Value = wshS.Range("1:1").Value
ID = wshS.Range(strCol & r).Value
r0 = r
Do While wshS.Range(strCol & r + 1).Value = ID
r = r + 1
Loop
wshT.Range("2:" & r - r0 + 2).Value = wshS.Range(r0 & ":" & r).Value
End If
r = r + 1
Loop Until r > m
Application.ScreenUpdating = True
End Sub
- NateFromFunSep 29, 2020Copper ContributorI can't even say thank you enough!! This worked perfectly!