Copy entire row to matching sheet name (Macro)

ifeelalone

New Member
Joined
Mar 6, 2017
Messages
31
How can you copy an entire row to its matching sheet name based on cell value?

Main Sheet: criteria is in column B where the value and its entire row shall be copied to its matching sheet name
d6e79e9068403578e038de1c3eaf9548.jpg


Here's the sheet names
1.
241e54e612e10997301b40c5f69fdd3b.jpg


2.
642418a9119e451d7d954b30f618eeda.jpg


3.
0102975c0ce51ea85e8f5942540534e5.jpg
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:
You must have a sheet name in column "B" of every row you want copied over

Code:
Sub Copy_Row_To_Sheet_Cell_Value()
Application.ScreenUpdating = False
On Error GoTo M
Dim i As Long
Sheets("Summary").Activate
Dim Lastrow As Long
Lastrow = Sheets("Summary").Cells(Rows.Count, "B").End(xlUp).Row
Dim Lastrowa As Long
Dim ans As String
    For i = 2 To Lastrow
    ans = Cells(i, "B").Value
    Lastrowa = Sheets(ans).Cells(Rows.Count, "B").End(xlUp).Row + 1
    Rows(i).Copy Destination:=Sheets(ans).Rows(Lastrowa)
    Next
Exit Sub
M:
MsgBox "You do not have a sheet named  " & ans
End Sub
 
Last edited:
Upvote 0
It's always better to have your totals at the top of your sheet and not as you have them. Otherwise your constantly needing to adjust your Totals Row depending on how many rows you have each month.
 
Upvote 0
Welcome to the MrExcel board!

Assuming ..
- the numerical values in those Total rows are the result of formulas
- the individual 'Name' sheets exist and can have any existing data on them removed

.. then you could also try this approach which is to cycle through the 3 (or however many) 'Name' sheets and grab all the relevant rows for that sheet at once, rather than cycling through each row on the Summary sheet and moving them individually to the appropriate sheet.

Test in a copy of your workbook.

Rich (BB code):
Sub MoveToSheets()
  Dim ws As Worksheet
  
  For Each ws In Worksheets
    If ws.Name <> "Summary" Then
      ws.UsedRange.Clear
      Sheets("Summary").UsedRange.Copy Destination:=ws.Range("A1")
      With ws.UsedRange
        .AutoFilter Field:=2, Criteria1:="<>" & ws.Name, Operator:=xlAnd, Criteria2:="<>"
        .Offset(1).EntireRow.Delete
        .AutoFilter
      End With
    End If
  Next ws
End Sub


For the future, I'd also suggest that you look at the link in my signature block below for providing sample data. You will get many more potential helpers if they can copy the sample data to their worksheets rather than having to type it out from an image.
 
Last edited:
Upvote 0
Hi Peter_SSs thank you!

That's what I'm looking for. If you look on the sheet names, it was subtotal-ed per month. Do you have work around with this?
 
Upvote 0
Hi Peter_SSsDo you have work around with this?
I'm not sure what you are asking.

I don't have your actual sheet, but my test sheets also has totals at the end of each month and those total rows are copied to each of the individual sheets and the total row formulas automatically adjust to the reduced rows in each month.

Are you saying that yours doesn't do that?

Can you describe exactly how the subtotals were put on the Summary sheet and tell us what the formulas are in cells D10, E10 and F10 of that first image?

To confirm (I didn't bother entereing the fruit column) here is my Summary sheet


Excel 2010 32 bit
ABCDEF
1DateNameFruitqtypriceTotal amount
212-JanLeni5630
313-JanDoi166
413-JanLeni6318
513-JanArvin236
614-JanDoi133
715-JanArvin144
815-JanLeni8432
915-JanDoi248
10January Total2633107
11
121-FebDoi144
131-FebArvin144
1412-FebArvin144
1512-FebLeni5630
1614-FebLeni8324
17February Total162166
Summary


and here are my other sheets after running the code.
Can you explain which aspects of these result sheets are incorrect? They seem to me to match your images pretty well.


Excel 2010 32 bit
ABCDEF
1DateNameFruitqtypriceTotal amount
212-JanLeni5630
313-JanLeni6318
415-JanLeni8432
5January Total191380
6
712-FebLeni5630
814-FebLeni8324
9February Total13954
Leni



Excel 2010 32 bit
ABCDEF
1DateNameFruitqtypriceTotal amount
213-JanArvin236
315-JanArvin144
4January Total3710
5
61-FebArvin144
712-FebArvin144
8February Total288
Arvin



Excel 2010 32 bit
ABCDEF
1DateNameFruitqtypriceTotal amount
213-JanDoi166
314-JanDoi133
415-JanDoi248
5January Total41317
6
71-FebDoi144
8February Total144
Doi
 
Upvote 0
Hey guys,

I'm doing another file with the same goal - Copy entire row to matching sheet name,
but now, the column C is now my reference and matching sheet name. and I'm given an error with this.

aba523b7ae4a078f45ea57cd1ae524d7.jpg


Kindly take note that the column C is a dropdown list (Data Validation-List) type and the whole table was formatted as table.

This is the code that I am using, can you tell me what is wrong?
I am using the code of Mr. Peter.

Sub MoveToSheets()

For Each ws In Worksheets
If ws.Name <> "Summary" Then
ws.UsedRange.Clear
Sheets("Summary").UsedRange.Copy Destination:=ws.Range("A1")
With ws.UsedRange
.AutoFilter Field:=3, Criteria1:="<>" & ws.Name, Operator:=xlAnd, Criteria2:="<>"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
End If
Next ws
End Sub

For your reference, this is the file I am currently working on. https://1drv.ms/x/s!AskvyCa9HdEygxYb0BBo8E2JDjCd
Thank you for your help.
 
Upvote 0

Forum statistics

Threads
1,214,987
Messages
6,122,618
Members
449,092
Latest member
amyap

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