Help with code to move blank cells

steveo0707

Board Regular
Joined
Mar 4, 2013
Messages
85
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
The following is the code I have for moving the blank rows located throughout a table in Excel. When I run the code, it is only moving the 1st and 3rd column blanks down. I have 7 total columns. Any help on this would be much appreciated.

Code:
Public Sub main()

  Dim oRange As Range
  Set oRange = Worksheets("Sheet1").Range("A3:G28")
  MoveData oRange
End Sub


Public Sub MoveData(ByRef oRange As Range)


   Dim i As Integer
   Dim j As Integer
   Dim oMoveRange As Range


   For i = 1 To oRange.Rows.Count
     If oRange.Value2(i, 1) = "" Then
       Set oMoveRange = oRange(i, 1).End(xlDown)
       If Intersect(oRange, oMoveRange) Is Nothing Then Exit Sub
         For j = 0 To 2 Step 2
           oRange(i, j + 1).Value = oMoveRange.Offset(0, j).Value
           oMoveRange.Offset(0, j).Value = ""
         Next
     End If
   Next
   
End Sub
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Not entirely sure what you are trying to move :confused:

This moves a row to the bottom of range if column A is empty

Code:
Sub MoveBlankRows()
    Dim ws As Worksheet:    Set ws = Worksheets("Sheet1")
    Dim cel As Range, r As Long
    Application.ScreenUpdating = False
    For r = 28 To 3 Step -1
        Set cel = Range("A" & r)
        If cel = "" Then
            cel.Resize(, 7).Cut
            Range("A29").Insert Shift:=xlDown
        End If
    Next r
End Sub
 
Last edited:
Upvote 0
Hello Yongle,

Thank you for the response. I am trying to scan data table and move all blank rows to the bottom.
 
Upvote 0
Try this minor amendment

Code:
Sub MoveBlankRows()
    Dim ws As Worksheet:    Set ws = Worksheets("Sheet1")
    Dim cel As Range, r As Long
    Application.ScreenUpdating = False
    For r = 28 To 3 Step -1
        Set cel = Range("A" & r)
        [COLOR=#ff0000]If WorksheetFunction.CountBlank(Rows(cel.Row)) = Columns.Count[/COLOR] Then
            cel.Resize(, 7).Cut
            Range("A29").Insert Shift:=xlDown
        End If
    Next r
End Sub

You could amend the same line to simply test the columns in the table are blank
Code:
[COLOR=#ff0000]If WorksheetFunction.CountBlank([/COLOR][COLOR=#ff0000]cel.Resize(, 7)) = 7[/COLOR] Then
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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