Loop and Copy/Paste all Rows equaling a variable into a new Excel File

BibiC

New Member
Joined
May 30, 2018
Messages
4
Hi everyone,

I'm new to vba so will really appreciate any help ;).
After a long unfruitful search I can't find why my code won't work.

To explain the problem, I have an Excel File with a bunch of data sorted by an index called Prod.Type in column A

Prod.TypeNamePriceNb.Stock
2Fries4100
2Roasted Potato250
3Computer70030
4Paper1500
4Binder2200
7Chair407
9Bag5020
9Panier5020

<tbody>
</tbody>

And my code is supposed to :

in a loop to check all Rows value of Prod.Type and
if it equals the variable prodNum then it copy the row into a new excel file.
else it close the file and increment prodNum and loop again until the whole column has been checked.

The result should be like this for prodNum = 2

Prod.TypeName
PriceNb.Stack
2Fries4100
2Roasted Potato250

<tbody>
</tbody>

But the result I get is only the headers row getting paste

Here's the code I have made
Code:
Sub test()


    Dim wbtarget As Excel.Workbook
    Dim consh As Worksheet
    Dim prodNum As Long
    Dim i As Long
    Dim shnum As Long
    
    Set consh = ThisWorkbook.Sheets("Sheet1")
    
    For counter = 1 To 20
    
    Set wbtarget = Workbooks.Add
    consh.Rows(1).Copy wbtarget.Sheets(1).Range("A1")
        For i = 1 To ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
            If Range("A" & i).Value = prodNum Then
                consh.Rows("A" & i).Copy wbtarget.Sheets(1).Range("A2")
                    Else
                    wbtarget.SaveAs "C:\Users\Anon\Desktop\Project\" & shnum & ".xlsx" 'path to save file
                    prodNum = prodNum + 1
                    shnum = shnum + 1
            End If
        Next
    Next counter
End Sub

The "For counter = 1 To 20" is for testing purpose, I have more than 6000 rows of data to copy paste.
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi & welcome to MrExcel
Untested but try
Code:
Sub test()
   Dim wbtarget As Workbook
   Dim Consh As Worksheet
   Dim Cl As Range
   
   Set Consh = ThisWorkbook.Sheets("Sheet1")
   With CreateObject("scripting.dictionary")
      For Each Cl In Consh.Range("A2", Consh.Range("A" & Rows.Count).End(xlUp))
         If Not exists(Cl.Value) Then
            Set wbtarget = Workbooks.Add
            .Add Cl.Value, Nothing
            Consh.AutoFilter 1, Cl.Value
            Consh.AutoFilter.Range.Copy wbtarget.Sheets(1).Range("A1")
            wbtarget.SaveAs "C:\Users\Anon\Desktop\Project\" & Cl.Value & ".xlsx" 'path to save file
         End If
      Next Cl
   End With
End Sub
 
Upvote 0
Hi Fluff, thx for your reply

I tested your code, and It give a "Sub or Function not defined" when I run it.
 
Upvote 0
I'm guessing that it was this line that gave the error
Code:
If Not exists(Cl.Value) Then
there should be a . in front of Exists. However try this instead.
Code:
Sub test()
   Dim wbtarget As Workbook
   Dim Consh As Worksheet
   Dim Cl As Range
   
   Set Consh = ThisWorkbook.Sheets("Sheet1")
   If Consh.AutoFilterMode Then Consh.AutoFilterMode = False
   With CreateObject("scripting.dictionary")
      For Each Cl In Consh.Range("A2", Consh.Range("A" & Rows.Count).End(xlUp))
         If Not .Exists(Cl.Value) Then
            Set wbtarget = Workbooks.Add
            .Add Cl.Value, Nothing
            Consh.Range("A1").AutoFilter 1, Cl.Value
            Consh.AutoFilter.Range.Copy wbtarget.Sheets(1).Range("A1")
            wbtarget.SaveAs "C:\Users\Anon\Desktop\Project\" & Cl.Value & ".xlsx" 'path to save file
         End If
      Next Cl
   End With
   Consh.AutoFilterMode = False
End Sub
There were a couple of other mistakes.
 
Upvote 0
Thx a lot! it works! Thank you so much Fluff!

ah.... there's still one problem..... each newly created excel file don't close after getting all the data, im with 300 opened excel file, and the computer also crashed :ROFLMAO:.
 
Upvote 0
Ok, add this line
Code:
            wbTarget.SaveAs "C:\Users\Anon\Desktop\Project\" & Cl.Value & ".xlsx" 'path to save file
            [COLOR=#ff0000]wbTarget.Close False[/COLOR]
         End If
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,585
Messages
6,120,394
Members
448,957
Latest member
Hat4Life

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