VBA to move rows of data up removing blank rows.

dbmathis

Well-known Member
Joined
Sep 22, 2002
Messages
1,064
Hi all,

I have the following senario.
Transaction Register Starting Dec-16-2005 Draft.xls
KLMN
10DesciprionAmount
11#121 Tags$177.640
12#123 Truck$279.890
13#122 Emmy's Car$280.000
140
150
16Burger King$6.900
170
180
190
200
210
220
230
24St.Albert$5.000
25HEB$49.060
260
270
280
290
300
310
Transaction Register


Due to the limitations of space on this forum I am only showing K11:M31 but my actual sheet has an area of K11:M41.

Does anyone know how to do the following with VBA? Basically move all blank lines to the bottom of the K11:M41 and move all rows that contain data to the top of the area?

The location of the rows of data within the area of K11:K41 varies from day to day depending on what's deleted, which is what creates these blank lines between data.

For example the end result would be:
Transaction Register Starting Dec-16-2005 Draft.xls
KLMN
10DesciprionAmount
11#121 Tags$177.640
12#123 Truck$279.890
13#122 Emmy's Car$280.000
14Burger King$6.900
15St.Albert$5.000
16HEB$49.060
170
180
Transaction Register


Thanks.
 
try
sub test()
dim r as range,a(),i as long
for each r in range("K11:K41")
if isempty(r) then
i=i+1:redim preserve a(1 to 2,1 to i)
a(1,i)=r.value
a(2,i)=r.offset(,1).value
end if
next
range("K11:M41").clearcontents
range("K11").resize(ubound(a,2),ubound(a,1))=application.transpose(a)
erase a
end sub
 
Upvote 0

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
That seemed to partially delete the data in the upper column K. It didn't remove any blanks.

bbiab. Wife is calling me :)

Thanks for your help btw.
 
Upvote 0
OOps!

it should read as

if not isempty(r) then

sub test()
dim r as range,a(),i as long
for each r in range("K11:K41")
if not isempty(r) then
i=i+1:redim preserve a(1 to 3,1 to i)
a(1,i)=r.value
a(2,i)=r.offset(,1).value
a(3,i)=r.offset(,2).value
end if
next
range("K11:M41").clearcontents
range("K11").resize(ubound(a,2),ubound(a,1))=application.transpose(a)
erase a
end sub
 
Upvote 0
I'm assuming that you have some data below the target range that you do not want to disturb. That is why you do not want to simply delete cells.

That being the case. Something like this should work:

Code:
Public Sub main()

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

Public Sub MoveData(ByRef oRange As Range)
  Dim i As Integer
  Dim j As Integer
  
  For i = 1 To oRange.Rows.Count
    If oRange.Value2(i, 1) = "" Then
      If Intersect(oRange, oRange(i, 1).End(xlDown)) Is Nothing Then Exit Sub
        For j = 1 To 3 Step 2
          oRange(i, j).Value = oRange(i, j).End(xlDown).Value
          oRange(i, j).End(xlDown).Value = ""
        Next
    End If
  Next
  
End Sub
 
Upvote 0
Thanks Mark and jindon got it working because of you two. Much appreciated.

Have a Merry Christmas!
 
Upvote 0
Mark,

Found a problem with the code you posted and don't seem to be able to figure out the solution. The following have a blank line above them and when I run the VBA it outputs the following.

Before:

Blank line
#123 Truck $279.89
#122 Emmy's Car $280.00

After:

#123 Truck $279.89
#123 Truck $279.89

These two screenshots should better describe the issue.

Before:

before.PNG


After:

after.PNG


Thanks for all your help.[/img]
 
Upvote 0
I am in range K11:K31 now. Sorry I didn't mention this, it didn't look like it effected the code, I just modified the range in the code to match the area on my sheet, but it wasn't working right when I had it set to K12:M41 either.

My Bad. Hope this helps.
 
Upvote 0
I think this fixes the problem:

Code:
Public Sub main()

  Dim oRange As Range
  
  Set oRange = Worksheets("Sheet1").Range("K12:M41")
  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
 
Upvote 0
Seems to be working like a champ. THANK YOU! :LOL:

I will go study that code now.

Merry Christmas.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,626
Members
449,093
Latest member
catterz66

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