Move Rows based on Cell Value

VbaHell

Well-known Member
Joined
Jan 30, 2011
Messages
1,220
Hello all

I am hoping someone can help on this

I have a workbook with 13 worksheets, each sheet is named as the 12 months, IE Jan, Feb, Mar Etc and the last sheet is named as data
I am looking for some vba code that when run will look at the cell value in column "S" and move each row to the correct worksheet based on the worksheet name.

IE: S6 = Mar, copy row to the next available row in worksheet "Mar"

Is this possible please
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Try this:
Assuming your sheet is named "data"
The script starts in row(2) and looks in column (S) for sheet name.
Make sure all values in column (S) are exactly the same as sheet name.
Code:
Sub Test()
Application.ScreenUpdating = False
'Modified 3-21-18 5:25 PM EDT
On Error GoTo M
Dim i As Long
Sheets("data").Activate
Dim Lastrow As Long
Lastrow = Sheets("data").Cells(Rows.Count, "S").End(xlUp).Row
    For i = 2 To Lastrow
            Rows(i).Copy Sheets(Cells(i, "S").Value).Rows(Sheets(Cells(i, "S").Value).Cells(Rows.Count, "S").End(xlUp).Row + 1)
    Next
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "That sheet name does not exist or you had some other sort of problem"
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I hope am not being a pain here but is it possible to change the code to work this way please

Instead of copying the whole Row copy a range from "A2:S2" and loop through all Rows

Then copy the range "A2:S2" as before to each worksheet but place "A2:S2" starting in cell "E2"
 
Upvote 0
Try this:
Code:
Sub Test()
Application.ScreenUpdating = False
'Modified 3-22-18 11:55 PM EDT
On Error GoTo M
Dim i As Long
Dim ans As Long
Sheets("data").Activate
Dim Lastrow As Long
Lastrow = Sheets("data").Cells(Rows.Count, "S").End(xlUp).Row
    For i = 2 To Lastrow
        Cells(i, 1).Resize(, 19).Copy Sheets(Cells(i, "S").Value).Cells(Sheets(Cells(i, "S").Value).Cells(Rows.Count, "S").End(xlUp).Row + 1, "E")
     Next
Application.ScreenUpdating = True
Exit Sub
M:
MsgBox "That sheet name does not exist or you had some other sort of problem"
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,200
Members
453,022
Latest member
RobertV1609

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