Looping limitation - Help on Copying Rows to New File

diadromous

New Member
Joined
Feb 4, 2014
Messages
2
Hello -

I have a database that needs breaking down in order to fulfill a request.

I've been trying to create a macro to copy one row to a new workbook (starting from row 3), file name save as a value of the cell (C1), and move onto the next row. However, my code appears to only loop through 26 entries and then stops.

Code:
 Dim row As Long
    Dim refname
    
    row = 3
    Do While Cells(row).Value <> ""
    Rows(row).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
      
    DirPath = MyPath
    refname = ActiveSheet.Range("C1")
    ActiveWorkbook.SaveAs DirPath & refname
    ActiveWindow.Close

    row = row + 1
    
    Loop

Is there something I am doing wrong? Any help would be a HUGE help!
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Welcome to the forum........

What do you have (or not have) in row 27?

Try changing your code to-

Code:
Sub copy()
 Dim refname
Dim x as range
Dim lr as long
    

lr = Worksheets("Sheet1").Cells(Rows.Count, "C").End(xlUp).Row

For x= 3 to lr
   
    if Cells(x,3).Value <> ""
    Rows(x).Select
    Selection.Copy
    Workbooks.Add
    ActiveSheet.Paste
    Application.CutCopyMode = False
      
    DirPath = MyPath
    refname = ActiveSheet.Range("C1")
    ActiveWorkbook.SaveAs DirPath & refname
    ActiveWindow.Close

   Next x

End Sub

FarmerScott
 
Upvote 0
Thanks for the feedback farmerscott! I've reviewed the database, but can't seem to find what is in row 27. From what I can tell, all the data seems to be uniform.

However, I finally figured out through trial and error that if I add a space in between the quotes
Code:
Do While Cells(row).Value <> " "
it runs beyond the said row.

I guess this is a rookie mistake.

But a huge thanks for helping out - your code is much more of an elegant solution than mine, and already understand a bit more about VBA by looking at what you did!
 
Upvote 0
Diadromous,

Based on your changing the <>"" to <>"[space] ".....

Where has your database come from? Has it been imported from somewhere?
Often imported data can have spaces in the cells.
Do a test on the cell in row 27 using =ISBLANK(cell), and see if it comes up with true or false.

I am no VBA expert, I am just learning as I go, but by all means have a look at the codes written on this forum to get ideas on how the experts go about it. VBA has some great flexibility to go about the same problem from different solutions.

Hope that helps,

FarmerScott
 
Upvote 0

Forum statistics

Threads
1,214,520
Messages
6,120,013
Members
448,935
Latest member
ijat

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