Stop macro looping if next cell is empty

Dharma_db

New Member
Joined
Jan 18, 2017
Messages
4
Need help to stop looping if next cell is empty. This is excel macro I use for copying multiple tabulation data to combine in a single data area. works but looping is not stopping.

Code:
Sub CombineToCommonColumns()
'
' CombineToCommonColumns Macro
'
 
'
    iRow = 1
    Do Until IsEmpty(Cells(iRow, 1))
   'Select to column till last
    Range(Selection, Selection.End(xlDown)).Select
    Selection.End(xlDown).Select
    'Select next free cell
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cell In ws.Columns(1).Cells
         If Len(cell) = 0 Then cell.Select: Exit For
    Next cell
    'Go to right and select till last right and last down, select and cut
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.End(xlToRight).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Cut
    'Got to home
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.End(xlDown).Select
    'Select next free cell
    For Each cell In ws.Columns(1).Cells
         If Len(cell) = 0 Then cell.Select: Exit For
    Next cell
    ActiveSheet.Paste
    iRow = iRow + 1
    Loop
End Sub
 
Last edited by a moderator:

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
are you testing your ability post, or are you asking a legitimate question, in which case we need to move this to a proper forum level
 
Upvote 0
Hello, it is a legitimate question. please move it proper forum so that I can get some suggestion/advise in order to fix my issue.

 
Upvote 0
Where do you think you are telling it to end the loop?
Try stepping through the code with F8 so you can see what is happening.
 
Last edited:
Upvote 0
As I have stated step through your code with F8, if (with your layout) data is being pasted in the next cell is Cells(iRow, 1) ever empty?

Test it with 5 rows of data after adding the red line below so you can see what cell it is referencing.

Code:
Do Until IsEmpty(Cells(iRow, 1))
    [COLOR="#FF0000"]MsgBox Cells(iRow, 1)[/COLOR]
        'Select to column till last
        Range(Selection, Selection.End(xlDown)).Select
 
Upvote 0
Sorry forgot to put the .Address at the end...

Code:
Do Until IsEmpty(Cells(iRow, 1))
    [COLOR="#FF0000"]MsgBox Cells(iRow, 1).Address[/COLOR]
        'Select to column till last
        Range(Selection, Selection.End(xlDown)).Select
 
Last edited:
Upvote 0
I am able to make this fix with below code. my next question is how do I modify to run in multiple sheets in same workbook?
Code:
Sub CombineToCommonColumns()'
' CombineToCommonColumns Macro
'
 
'
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    'Set Do loop to stop when an empty cell is reached.
    Do Until IsEmpty(ActiveCell)
    'Select to column till last
    Range(Selection, Selection.End(xlDown)).Select
    Selection.End(xlDown).Select
    'Select next free cell
    Dim ws As Worksheet
    Set ws = ActiveSheet
    For Each cell In ws.Columns(1).Cells
         If Len(cell) = 0 Then cell.Select: Exit For
    Next cell
    'Go to right and select till last right and last down, select and cut
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.End(xlToRight).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Cut
    'Got to home
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.End(xlDown).Select
    'Select next free cell
    For Each cell In ws.Columns(1).Cells
         If Len(cell) = 0 Then cell.Select: Exit For
    Next cell
    ActiveSheet.Paste
    ' Step down 1 row from present location.
    'ActiveCell.Offset(1, 0).Select
    Loop
    Application.DisplayAlerts = True
    Application.ScreenUpdating = False
    Dim Results(1 To 2) As String
End Sub
[/QUOTE]
 
Upvote 0
Your code needs a fair bit of tidying up but I haven't time as just going to work (and not much point turning off screenupdating if you are selecting cells) but try the below (untested) which runs on all the sheets (you need data in all the sheets).

Code:
Sub CombineToCommonColumns()    '
    ' CombineToCommonColumns Macro
    '

    Dim ws As Worksheet, cell As Range
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    
    For Each ws In ActiveWorkbook.Worksheets
    'Set Do loop to stop when an empty cell is reached.
    
    ws.Activate
    Do Until IsEmpty(ActiveCell)
        'Select to column till last
        Range(Selection, Selection.End(xlDown)).Select
        Selection.End(xlDown).Select
        'Select next free cell
      
        For Each cell In ws.Columns(1).Cells
            If Len(cell) = 0 Then cell.Select: Exit For
        Next cell
        'Go to right and select till last right and last down, select and cut
        Range(Selection, Selection.End(xlToRight)).Select
        Selection.End(xlToRight).Select
        Range(Selection, Selection.End(xlToRight)).Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.Cut
        'Got to home
        Range("A1").Select
        Range(Selection, Selection.End(xlDown)).Select
        Selection.End(xlDown).Select
        'Select next free cell
        For Each cell In ws.Columns(1).Cells
            If Len(cell) = 0 Then cell.Select: Exit For
        Next cell
        ActiveSheet.Paste
        ' Step down 1 row from present location.
        'ActiveCell.Offset(1, 0).Select
    Loop
    Next
    Application.DisplayAlerts = True
    Application.ScreenUpdating = False
    Dim Results(1 To 2) As String
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,679
Messages
6,126,183
Members
449,296
Latest member
tinneytwin

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