Run time error when copying rows based on word

jeffreybrown

Well-known Member
Joined
Jul 28, 2004
Messages
5,152
Hi All,

Here is my table which is just a test sample of the larger table, but in the end, it is column 1 I want to base the new ws on.

Before Macro
Excel Workbook
AB
1UnitCount
253
3210
431 LGR SQ10
584
631 LGR SQ7
726
AY


The code below works fine through the first instance of the match and adds a new ws based on the name, but when it gets to the second match the macro tries to add the ws all over again and I get a run time error 1004 which states you can not add a ws and name it the same as one that already exist. I only have one sheet in my wb titled "AY". How can I also have the two column headers transfer to the new ws?
Code:
Public Sub CopyUnit()

Dim N As String
Dim i As Long
Dim ws As Worksheet
Set ws = Sheets("AY")

N = Worksheets("PAS Codes").Range("L14").Value

For i = Range("A65334").End(xlUp).Row To 1 Step -1

With ws
 If Cells(i, 1).Value = N Then
    .Rows(i).Copy
      Sheets.Add.Name = N
    Rows("1:1").Select
    ActiveSheet.Paste
 End If
 
 End With

Next i

Application.CutCopyMode = False
    
End Sub
After Macro
Excel Workbook
AB
1UnitCount
231 LGR SQ7
331 LGR SQ10
31 LGR SQ
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Jeff

I've taken quite a few guesses about what you have and what you are trying to do. Give this a whirl on a copy of your workbook. If it's not what you need , I'll probably have to come back and ask some questions to clarify the situation.

<font face=Courier New><br><SPAN style="color:#00007F">Public</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CopyUnit()<br>    <SPAN style="color:#00007F">Dim</SPAN> N <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> ws <SPAN style="color:#00007F">As</SPAN> Worksheet, wsNew <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    <SPAN style="color:#00007F">Set</SPAN> ws = Sheets("AY")<br>    N = Worksheets("PAS Codes").Range("L14").Value<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">Resume</SPAN> <SPAN style="color:#00007F">Next</SPAN><br>    Sheets.Add.Name = N<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">Set</SPAN> wsNew = Sheets(N)<br>    <SPAN style="color:#00007F">With</SPAN> ws<br>        <SPAN style="color:#00007F">For</SPAN> i = .Range("A65334").End(xlUp).Row <SPAN style="color:#00007F">To</SPAN> 1 <SPAN style="color:#00007F">Step</SPAN> -1<br>            <SPAN style="color:#00007F">If</SPAN> .Cells(i, 1).Value = N <SPAN style="color:#00007F">Then</SPAN><br>               .Rows(i).Copy Destination:= _<br>                   wsNew.Range("A" & Rows.Count).End(xlUp).Offset(1)<br>            <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>        <SPAN style="color:#00007F">Next</SPAN> i<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    Application.CutCopyMode = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> ws = <SPAN style="color:#00007F">Nothing</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsNew = <SPAN style="color:#00007F">Nothing</SPAN><br>End <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Is it Error 438?

Despite the Macro recorder always writing .Paste it doesn't actually work, use .PasteSpecial instead or as Peter_Sss suggested do .Copy Destination:=.... which saves you a few lines of code/screen flicker and the need to set CutCopyMode to false.

HTH


Dave
 
Upvote 0
Peter/Dave,

Thank you so much for the help. That works great Peter. The only thing it did not do is transfer the column headers. As you can see I am pulling the value from sheet "PAS Codes", but I would like to turn this into a DV which will trigger on Selection change. I added this to the "PAS Codes" code, but it doesn't do anything on change.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.CopyUnit
End Sub
Do I need some type of If Target.Address?
 
Upvote 0
Peter/Dave,

Thank you so much for the help. That works great Peter. The only thing it did not do is transfer the column headers. As you can see I am pulling the value from sheet "PAS Codes", but I would like to turn this into a DV which will trigger on Selection change. I added this to the "PAS Codes" code, but it doesn't do anything on change.
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.CopyUnit
End Sub
Do I need some type of If Target.Address?
What do you expect to happen if, say, "31 LGR SQ" is selected in "PAS Codes" cell L14 and then later that same value is selected again?
 
Upvote 0
Peter,

In cell L14 "PAS Codes" I have already created the DV with the list of names. If I select "31 LGR SQ" and it creates a sheet, I would expect if I select that name again and the sheet exist for the macro to exit because it already exists. I get new data once a week so chances are the sheet will not exist, but I guess like you say, "What if?".
 
Upvote 0
I think your original code was correct bar one line: You had the
Sheets.Add.Name = N
within your loop, so it is trying to create the sheet over and over again. If you took this line outside the loop (before it is started) then I think it works OK.
 
Upvote 0
I'm suggesting a number of changes.

1. Change the code to be Worksheet_Change code for the "PAS Codes" sheet. (Post back if you need to know how to do that)

2. Instead of looping through every row and testing for a code match, use AutoFilter to get the lot at once. This has the added advantage of getting the headings at the same time.

3. The code is skipped if the value in cell L14 on PAS Codes is deleted.

4. The filter/copy is skipped and a message box is displayed if a particular code sheet already exists. That is, if the same value is selected more than once in L14.

See how this goes.

<font face=Courier New><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Worksheet_Change(<SPAN style="color:#00007F">ByVal</SPAN> Target <SPAN style="color:#00007F">As</SPAN> Range)<br>    <SPAN style="color:#00007F">Dim</SPAN> N <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> wsAY <SPAN style="color:#00007F">As</SPAN> Worksheet, wsN <SPAN style="color:#00007F">As</SPAN> Worksheet<br>    <br>    N = Range("L14").Value<br>    <SPAN style="color:#00007F">If</SPAN> N = "" <SPAN style="color:#00007F">Then</SPAN> <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsAY = Sheets("AY")<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> PreExit<br>    Sheets.Add.Name = N<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    <SPAN style="color:#00007F">Set</SPAN> wsN = Sheets(N)<br>    <SPAN style="color:#00007F">With</SPAN> wsAY.UsedRange<br>        .AutoFilter Field:=1, Criteria1:=N<br>        .Copy Destination:=wsN.Range("A1")<br>        .AutoFilter<br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>    <SPAN style="color:#00007F">Set</SPAN> wsAY = Nothing: <SPAN style="color:#00007F">Set</SPAN> wsN = Nothing<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">Exit</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br>    <br>PreExit:<br>    <SPAN style="color:#00007F">On</SPAN> <SPAN style="color:#00007F">Error</SPAN> <SPAN style="color:#00007F">GoTo</SPAN> 0<br>    Application.DisplayAlerts = <SPAN style="color:#00007F">False</SPAN><br>    ActiveSheet.Delete<br>    Application.DisplayAlerts = <SPAN style="color:#00007F">True</SPAN><br>    MsgBox "Sheet " & N & " already exists. Copy skipped."<br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Peter,

This works extremely nice, I can't thank you enough. I sure appreciate your expertise along with all the other experts. Have a good day or maybe it is night for you?
 
Upvote 0
Have a good day or maybe it is night for you?
Definitely night and nearly time to sign off. Getting on towards 11pm (Saturday) here.

Glad to have been able to help. :)
 
Upvote 0

Forum statistics

Threads
1,215,020
Messages
6,122,709
Members
449,093
Latest member
Mnur

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