Copy row from one worksheet to worksheet named using cell reference

scarlett3

Board Regular
Joined
Jan 21, 2005
Messages
73
Office Version
  1. 365
Platform
  1. Windows
I gather data from an external source into one worksheet, entitled 'Paste'. It will look like the following:

Worksheet Date text1 text2
alpha 05/08/2019 text text
gamma 05/08/2019 text text

I then copy the row with 'alpha' in column A and paste to the bottom of the current rows of text in the 'alpha' worksheet, copy the row with 'gamma' and paste to the bottom of the current rows of text in the 'gamma' worksheet and so on.

It is rather tiresome when there are about 100 rows to copy over to the relevant worksheet.

Can someone help me with a macro that will read each row, copy that row to the worksheet name in column A, and repeat this for all non-empty rows in the 'Paste' worksheet?

Thanks
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
How about
Code:
Sub scarlett3()
   Dim Cl As Range
   Dim Ws As Worksheet
   Dim Ky As Variant
   
   Set Ws = Sheets("Paste")
   With CreateObject("scripting.dictionary")
      For Each Cl In Ws.Range("A2", Ws.Range("A" & Rows.Count).End(xlUp))
         If Cl.Value <> "" Then .Item(Cl.Value) = Empty
      Next Cl
      For Each Ky In .keys
         If Evaluate("isref('" & Ky & "'!a1)") Then
            Ws.Range("A1").AutoFilter 1, Ky
            Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp).Row
         End If
      Next Ky
      Ws.AutoFilterMode = False
   End With
End Sub
 
Upvote 0
Thanks Fluff!

I've tried the script and it generates the following error: 'Run-time error '1004': Copy method of Range class failed'

When it paused, a filter in column A has been activated on the value for cell A2 and the debugging highlighted the line 'Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp).Row'
 
Upvote 0
Do you have a header in row 1 with data starting in A2?
Also do you have any merged cells?
 
Upvote 0
Yes, header in row 1 and data starting in A2.

There are no merged cells.
 
Upvote 0
Oops
Remove the word in red
Code:
            Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp)[COLOR=#ff0000].Row[/COLOR]
 
Upvote 0
Hi Fluff

Thanks for the edit. The macro now completes.

Just one issue...

When it copies to the row to the relevant worksheet, it overwrite the final row in the destination worksheet. Could you amend it so that it pastes below the current text in the destination worksheet?
 
Upvote 0
I'll get right eventually (honest) :)
It should be
Code:
            Ws.AutoFilter.Range.Offset(1).EntireRow.Copy Sheets(Ky).Range("A" & Rows.Count).End(xlUp)[COLOR=#ff0000].Offset(1)[/COLOR]
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

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