Having Trouble with Macro to find first empty cell in a column

rjsnieg

New Member
Joined
Feb 1, 2022
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Hello All,

I have the following code which I would like to use to Cut the contents of the active cell, find the first empty cell in a range of rows in the active column. I would like the macro to use the active column and begin searching for an empty cell between rows 22-33 (omitting any empty cells in rows 1-21). The macro below only works if row 22 is blank, it does not search the rest of the range if 22 is not empty. After the empty cell is found I would like it to paste the values and comments only which doesn't seem to be working either. Thanks for the help!


VBA Code:
Sub Macro2()

ActiveCell.Cut
    On Error Resume Next
    Dim xCell As Range
   
    For Each xCell In ActiveSheet.Cells(22, ActiveCell.Column).Cells
        If Len(xCell) = 0 Then
            xCell.Select
            Exit For
        End If
    Next
    ActiveCell.PasteSpecial (xlPasteComments)
    ActiveCell.PasteSpecial (xlPasteValues)
End Sub
 
Last edited by a moderator:

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
There is only one cell (row 22) in "ActiveSheet.Cells(22, ActiveCell.Column)".

You have to copy something before you can paste.
 
Upvote 0
Thanks footoo. I apologize for my ignorance. I thought by having the "ActiveCell.Cut before the "On Error Resume next" would have taken care of the copying problem. Is there a way to list a range of rows, or loop through until it finds an empty cell in the range?
 
Upvote 0
Welcome to the board!

Please try the following to see if it helps. It looks like a long and complicated code but most of it are comments and it hopefully is not that complicated with the comments.
VBA Code:
Sub doIt()
Dim rng As Range
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33

    ' Define the beginning cell for searching
    Set rng = Cells(intStart - 1, ActiveCell.Column)
    ' Making sure the very first cell is not empty
    ' Otherwise .End method will skip the correct empty cell
    If Not IsEmpty(rng.Offset(1)) Then
        Set rng = rng.Cells(1, 1).End(xlDown)
    End If
    ' So, we are sure the next cell is empty
    Set rng = rng.Offset(1)
    ' If it is out of the allowed empty cells range, then do not copy
    ' So you can remove the MsgBox to not alert the user.
    If rng.Row > intEnd Then
        MsgBox "No empty cell in rows " & intStart & "-" & intEnd, vbOKOnly + vbExclamation, "No action"
        Exit Sub
    End If
    ' We got the correct cell to move the active cell's content
    ActiveCell.Cut rng
End Sub
 
Upvote 0
Welcome to the board!

Please try the following to see if it helps. It looks like a long and complicated code but most of it are comments and it hopefully is not that complicated with the comments.
VBA Code:
Sub doIt()
Dim rng As Range
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33

    ' Define the beginning cell for searching
    Set rng = Cells(intStart - 1, ActiveCell.Column)
    ' Making sure the very first cell is not empty
    ' Otherwise .End method will skip the correct empty cell
    If Not IsEmpty(rng.Offset(1)) Then
        Set rng = rng.Cells(1, 1).End(xlDown)
    End If
    ' So, we are sure the next cell is empty
    Set rng = rng.Offset(1)
    ' If it is out of the allowed empty cells range, then do not copy
    ' So you can remove the MsgBox to not alert the user.
    If rng.Row > intEnd Then
        MsgBox "No empty cell in rows " & intStart & "-" & intEnd, vbOKOnly + vbExclamation, "No action"
        Exit Sub
    End If
    ' We got the correct cell to move the active cell's content
    ActiveCell.Cut rng
End Sub
This worked perfectly. I appreciate the help!
 
Upvote 0
And the loop version (which actually makes more sense but is slower than the first one - of course, the slowness doesn't matter for this but is a good habit to consider that for a different scenario like looping through thousands of cells).

VBA Code:
Sub doItWithLoop()
Dim i As Integer
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33
   
    ' Looping in the allowed cells
    For i = intStart To intEnd
        If IsEmpty(Cells(i, ActiveCell.Column)) Then
            ActiveCell.Cut Cells(i, ActiveCell.Column)
        End If
    Next i
End Sub
 
Upvote 0
Welcome to the board!

Please try the following to see if it helps. It looks like a long and complicated code but most of it are comments and it hopefully is not that complicated with the comments.
VBA Code:
Sub doIt()
Dim rng As Range
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33

    ' Define the beginning cell for searching
    Set rng = Cells(intStart - 1, ActiveCell.Column)
    ' Making sure the very first cell is not empty
    ' Otherwise .End method will skip the correct empty cell
    If Not IsEmpty(rng.Offset(1)) Then
        Set rng = rng.Cells(1, 1).End(xlDown)
    End If
    ' So, we are sure the next cell is empty
    Set rng = rng.Offset(1)
    ' If it is out of the allowed empty cells range, then do not copy
    ' So you can remove the MsgBox to not alert the user.
    If rng.Row > intEnd Then
        MsgBox "No empty cell in rows " & intStart & "-" & intEnd, vbOKOnly + vbExclamation, "No action"
        Exit Sub
    End If
    ' We got the correct cell to move the active cell's content
    ActiveCell.Cut rng
End Sub
Hey smozgur, I ran this and it appears to search for the first row being empty then it overwrites the 2nd row (row 23) whether or not it is empty. I think it has to do with the

If Not IsEmpty(rng.Offset(1)) Then
Set rng = rng.Cells(1, 1).End(xlDown)

How do I tell it to keep going if the subsequent rows are empty?
 
Upvote 0
Yes, I now realize that it fails. In fact, even the loop sample I sent has the missing loop break and it fails as well.

For a quick solution, here is the fixed loop version. It should work without problems.
VBA Code:
Sub doItWithLoop()
Dim i As Integer
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33
   
    ' Looping in the allowed cells
    For i = intStart To intEnd
        If IsEmpty(Cells(i, ActiveCell.Column)) Then
            ActiveCell.Cut Cells(i, ActiveCell.Column)
            Exit For
        End If
    Next i
End Sub

For the first approach, could you please tell me if it is possible to have a blank cell in the row 22:33 range in between? I mean, do you edit cells in 22:33 manually, so maybe there might be something entered in row 26 but row 24 could be empty? Or, similarly, maybe you delete a cell value in the row 22:33 range? Basically, is the following situation ever possible? (See D23 is empty. Should the next empty cell be D23 OR D25 in this case - OR is this not even expected?)
Book3
D
1value1
2value2
3value3
4value4
5value5
6value6
7value7
8value8
9value9
10
11value11
12value12
13value13
14value14
15
16value16
17
18value18
19value19
20value20
21value21
22value10
23
24value17
25
26
27
28
29
30
31
32
33
Sheet1


If not, then I will approach finding the empty cell from the bottom of the 22:33 range.
 
Upvote 0
Yes, I now realize that it fails. In fact, even the loop sample I sent has the missing loop break and it fails as well.

For a quick solution, here is the fixed loop version. It should work without problems.
VBA Code:
Sub doItWithLoop()
Dim i As Integer
Dim intStart As Integer
Dim intEnd As Integer

    ' Defining the cell row numbers for allowed empty cells
    intStart = 22
    intEnd = 33
  
    ' Looping in the allowed cells
    For i = intStart To intEnd
        If IsEmpty(Cells(i, ActiveCell.Column)) Then
            ActiveCell.Cut Cells(i, ActiveCell.Column)
            Exit For
        End If
    Next i
End Sub

For the first approach, could you please tell me if it is possible to have a blank cell in the row 22:33 range in between? I mean, do you edit cells in 22:33 manually, so maybe there might be something entered in row 26 but row 24 could be empty? Or, similarly, maybe you delete a cell value in the row 22:33 range? Basically, is the following situation ever possible? (See D23 is empty. Should the next empty cell be D23 OR D25 in this case - OR is this not even expected?)
Book3
D
1value1
2value2
3value3
4value4
5value5
6value6
7value7
8value8
9value9
10
11value11
12value12
13value13
14value14
15
16value16
17
18value18
19value19
20value20
21value21
22value10
23
24value17
25
26
27
28
29
30
31
32
33
Sheet1


If not, then I will approach finding the empty cell from the bottom of the 22:33 range.
Yes there is a possibility to have an empty cell in between values in the range.
 
Upvote 0

Forum statistics

Threads
1,215,064
Messages
6,122,942
Members
449,094
Latest member
teemeren

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