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!
 

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
785
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
 

diadromous

New Member
Joined
Feb 4, 2014
Messages
2
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!
 

farmerscott

Well-known Member
Joined
Jan 26, 2013
Messages
785
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
 

Forum statistics

Threads
1,082,371
Messages
5,365,062
Members
400,820
Latest member
Gossow

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top