Split Data from One Sheet to Different Sheets using VBA

Haree

Board Regular
Joined
Sep 22, 2019
Messages
146
Office Version
  1. 2016
Hello,
I have various sheets in excel approximately 30 to 35 sheets, basically one for each SKU i have a main sheet in which i will enter that days sales or purchase for all the SKU'S they should get posted in their respective sheets. I am attaching a sample sheet, Kindly guide me on how to do the same.
Thanks in advance
 
Try this:
VBA Code:
Sub Copy_Rows()
'Modified 3/14/2020 5:39:09 AM EST
Application.ScreenUpdating = False
Sheets("Data Sheet").Activate
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Data Sheet").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(i, 2).Resize(, 40).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Next
Application.ScreenUpdating = True
End Sub

This will copy cells in column B to column 40
If this is not enough change 40 to what you want.
Copying a empty cell is not a problem
So just change 40 to what ever you believe will be the most columns ever needed
Now copying a row that has already been copied needs some thinking.
Like I could have the script put a x in column 45 on each row and if the script sees a x in column 45 it will give you a warning

This x would not be copied over to other sheets.
See if this solves problem 1 and I will get back to you with a solution to second problem
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try this:
When script runs it puts a X in column 45 of sheet named "Data Sheet"
Next Time you run script if there is a X in column 45 that row will not be copied over
VBA Code:
Sub Copy_Rows()
'Modified 3/14/2020 6:07:48 AM EST
Application.ScreenUpdating = False
Sheets("Data Sheet").Activate
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Data Sheet").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
If Cells(i, 45).Value <> "X" Then
Cells(i, 2).Resize(, 40).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Cells(i, 45).Value = "X"
End If
Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
The first problem is solved, so grateful to you, saves a lot of time for me. I will try the above code now, Just asking out of doubt, i will be having a macro to clear all the cells in the data sheet, once the script is run so at that time i should be clearing column 45 as well ??
 
Upvote 0
I can have the row deleted on sheet named Data Sheet after script is run but you never asked for that.
Is that what you want?

So we will be deleting all rows on sheet named Data Sheet correct?
Does this also mean row(1)
 
Upvote 0
Try this will delete all rows on sheet named "Data Sheet" except for row(1)
VBA Code:
Sub Copy_Rows()
'Modified 3/14/2020 6:45:26 AM EST
Application.ScreenUpdating = False
Sheets("Data Sheet").Activate
Dim i As Long
Dim Lastrow As Long
Lastrow = Sheets("Data Sheet").Cells(Rows.Count, "A").End(xlUp).Row
Dim Lastrowa As Long
For i = 2 To Lastrow
Lastrowa = Sheets(Cells(i, 1).Value).Cells(Rows.Count, "A").End(xlUp).Row + 1
Cells(i, 2).Resize(, 40).Copy Sheets(Cells(i, 1).Value).Rows(Lastrowa)
Next
Sheets("Data Sheet").Rows(2).Resize(Lastrow - 1).Delete
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Hello, It worked so well Thank You so much. Really appreciate your help.
 
Upvote 0
Hello
A small Doubt
I am receiving a error
Run Time Error '9"
Subscript out of range
can you help me with this
 
Upvote 0
Hello
A small Doubt
I am receiving a error
Run Time Error '9"
Subscript out of range
can you help me with this
On March 14 you said it worked perfect but now more then a month later you say it's not working. What has changed to make this not work?
 
Upvote 0
No Sir it Worked perfectly alright that's y i said so. Now all of a sudden this popped up that's why asked
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,337
Members
452,637
Latest member
Ezio2866

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