Change in macro

Roman12

Board Regular
Joined
Jun 10, 2008
Messages
117
Hi,
I have received the attached macro that does exactly what I asked for, but it would actually help if somebody could make a minor change in it.

The following macro copy pastes the data into different sheets. That means: in my sheet called Data, the macro copies the data in the whole row if the word xxx is written in column C and pastes it into the sheet with the same name.

Would it be possible to copy paste the data instead of the sheet into a new Excel file? This would mean, that the data of xxx would instead of in a sheet be placed into a new Excel file with the same name.

Thank you very much for your help.


Sub ExtractData()
Dim lr As Long
Dim i As Long

mysheet = Array("www", "xxx", "yyy")
lr = Sheets("Data").Range("C" & Rows.Count).End(xlUp).Row
Application.EnableEvents = False
Application.ScreenUpdating = False
For i = 0 To UBound(mysheet)
Sheets(mysheet(i)).UsedRange.ClearContents
With Sheets("Data").Range("A1:L" & lr)
.AutoFilter Field:=3, Criteria1:=mysheet(i)
.Copy Destination:=Sheets(mysheet(i)).Range("A1")
.AutoFilter
End With
Next i
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
 
At the moment I get a runtime error which says, subscript out of range.

Where exactly do you get this error? Indicate on which line of code.

At the beginning, the workbook has been created, but no data has been copied in.

which workbook? Is this the workbook that contains the data that the macro will use autofilter on?
 
Upvote 0

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
I think the relevant line near the top might need changing to:

wb.SaveAs ele & ".xls"
 
Upvote 0
Now I have taken the suggestion that Richard has sent me at the beginning. This macro does create a new sheet, but only one, and there is no data pasted from my data sheet.
I try to put in Peters line into Richards macro now and see what happens.
 
Upvote 0
Also, just before the line...
Next i

... suggest adding this line:

Workbooks(mysheet(i) & ".xls").Save
 
Upvote 0
That was a great change Peter. The macro opens all the workbooks needed with the exact name on it. Unfortunately, the workbooks do not contain any data required.
 
Upvote 0
That was a great change Peter. The macro opens all the workbooks needed with the exact name on it. Unfortunately, the workbooks do not contain any data required.
It worked for me. This was my original sheet:

Excel Workbook
ABCDEFGHIJKL
1Expired2 weeks noticeGroupidaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2xxx0sdfg
3xxx0gsds
4xxx0gfsddg
5xxx0fgss
6yyy1dfgdfg
7yyy1sdgfs
8yyy1sfg
9yyy1gfsdsdf
10yyy1dfgg
11yyy1ssdf
12yyy1gdfg
13yyy1ss
14
Data



..and this was the final code I used:

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> ExtractData()<br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</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> wb, ele<br>    <br>    mysheet = Array("www", "xxx", "yyy")<br>    <SPAN style="color:#00007F">For</SPAN> <SPAN style="color:#00007F">Each</SPAN> ele <SPAN style="color:#00007F">In</SPAN> mysheet<br>        <SPAN style="color:#00007F">Set</SPAN> wb = Workbooks.Add(xlWBATWorksheet)<br>        wb.SaveAs ele & ".xls"<br>    <SPAN style="color:#00007F">Next</SPAN><br>    ThisWorkbook.Activate<br>    lr = Sheets("Data").Range("C" & Rows.Count).End(xlUp).Row<br>    Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>    <SPAN style="color:#00007F">For</SPAN> i = 0 <SPAN style="color:#00007F">To</SPAN> <SPAN style="color:#00007F">UBound</SPAN>(mysheet)<br>        <SPAN style="color:#00007F">With</SPAN> Sheets("Data").Range("A1:L" & lr)<br>            .AutoFilter Field:=3, Criteria1:=mysheet(i)<br>            .Copy Destination:=Workbooks(mysheet(i) & ".xls").Sheets(1).Range("A1")<br>            .AutoFilter<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        Workbooks(mysheet(i) & ".xls").Save<br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>    Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br><br><br></FONT>
 
Upvote 0
Great it worked for me as well. Is it possible to give the sheet the same name as well? You made that yesterday. The workbook with the name xxx should also have the sheet called xxx.
 
Upvote 0
Great it worked for me as well. Is it possible to give the sheet the same name as well? You made that yesterday. The workbook with the name xxx should also have the sheet called xxx.
Add the middle line below between the other two

<font face=Courier New>        <SPAN style="color:#00007F">Set</SPAN> wb = Workbooks.Add(xlWBATWorksheet)<br>        ActiveSheet.Name = ele<br>        wb.SaveAs ele & ".xls"</FONT>
 
Upvote 0
Strange. That does not work. It opens the workbooks without data again and without the right name in the sheet.

Do you know why? I only put the line into the middle as you have told me.
 
Upvote 0
If I add the suggested line, the runtime error subscript out of range appears. The rest is still working, I started again.
 
Upvote 0

Forum statistics

Threads
1,216,731
Messages
6,132,391
Members
449,725
Latest member
Enero1

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