move text to top problem

123excel

New Member
Joined
Jan 18, 2017
Messages
34
Hi, I am trying to move text up in a range when there is empty rows above or in between. I thought I had got it to work but I just found out a big problem, it clears and copies the wrong rows.

Does anyone of you experts think you could help me out with this problem?


Here is an example of the problem:

If I got:
1
empty
3
4
5

This should be:
1
3
4
5

But now the outcome is:
1
3
3
4
(as you can see, 3 is copied and 5 is deleted. The text should just move up).



The code I am using is this:

Code:
Public Sub RowRange()


  Dim oRange As Range
  
  Set oRange = Range("A36:G164")
  MoveTextUp oRange
  
End Sub


Public Sub MoveTextUp(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 6 Step 1
          oRange(i, j).Value = oRange(i, j).End(xlDown).Value
          oRange(i, j).End(xlDown).Value = ""
        Next
    End If
  Next

End Sub
 
Last edited:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try this

Code:
Sub Macro9()
    On Error Resume Next
    Range("A36:G164").SpecialCells(xlCellTypeBlanks).Delete Shift:=xlUp
End Sub
 
Upvote 0
Thanks, but the problem is that I cant use delete, i need move the empty rows in range A36:G164 down and the ones with text to the top. Any idea here?
 
Upvote 0
Thanks, but the problem is that I cant use delete, i need move the empty rows in range A36:G164 down and the ones with text to the top. Any idea here?


rows or cells?
In which column of the range "A36:G164" do you want to verify?
 
Upvote 0
Cells. I want to verify if any of the cells in A:G is having any text. Lets say A36:G36 is empty, and A37:G37 also is empty except B37 that have som text, then all the cells in A37:G37 should move over A36:G36. Did this clarify it?
 
Upvote 0
Cells. I want to verify if any of the cells in A:G is having any text. Lets say A36:G36 is empty, and A37:G37 also is empty except B37 that have som text, then all the cells in A37:G37 should move over A36:G36. Did this clarify it?

move or pass the value from A37 to A36 and then A37 = empty?

If the above is correct then try this:

Code:
Sub RowRange()
    Dim r As Range, ini As Long, i As Long
    Dim rs As Range, k As Long, wcount As Long
    
    Application.ScreenUpdating = False
    Set r = Range("A36:G164")
    For Each rs In r.Rows
        wcount = WorksheetFunction.CountA(rs)
        If wcount = 0 Then
            k = 1
            Do While k <= r.Rows.Count
                wcount = WorksheetFunction.CountA(rs.Offset(k))
                If wcount > 0 Then
                    rs.Value = rs.Offset(k).Value
                    rs.Offset(k).Value = ""
                    Exit Do
                End If
                k = k + 1
            Loop
        End If
    Next
    Application.ScreenUpdating = True
    MsgBox "End"
End Sub
 
Upvote 0
Cross posted https://www.excelforum.com/excel-pr...in-range-to-top-and-move-empty-rows-down.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}</style>Hi Fluff and DanteAmor, I should of course have mention cross posting, sorry about that. I was in a hurry so I needed help fast due to a deadline. Will definitly tell about cross posting next time. Anyway I do really appreciate the help I got and togheter we actually got two solutions, which I will also paste here. The other alternative solution is from WideBoyDixon in the cross post here: https://www.excelforum.com/excel-pr...top-and-move-empty-rows-down.html#post5136240

Alternative code:

<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Public Sub MoveTextUp(ByRef oRange As Range)

Dim thisRow As Long
Dim rowOffset As Long
Dim lastRow As Long

lastRow = oRange.Rows.Count
rowOffset = lastRow
thisRow = 1
Do While thisRow <= lastRow
If oRange.Cells(thisRow, 1).Value = "" Then
oRange.Cells(thisRow, 1).Resize(1, oRange.Columns.Count).Cut
oRange.Cells(thisRow, 1).Offset(rowOffset, 0).Insert shift:=xlDown
lastRow = lastRow - 1
Else
thisRow = thisRow + 1
rowOffset = rowOffset - 1
End If
Loop

End Sub</code>
His solution worked as it should so that is an alternative if someone would rather use that, but I found it more slow than DanteAmors solution, which is really fast and works perfect!

Thanks a lot DanteAmor for your help, I do really appreciate it and it helped me met the deadline!
 
Upvote 0
<style type="text/css">p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica; min-height: 14.0px}</style>Hi Fluff and DanteAmor, I should of course have mention cross posting, sorry about that. I was in a hurry so I needed help fast due to a deadline. Will definitly tell about cross posting next time. Anyway I do really appreciate the help I got and togheter we actually got two solutions, which I will also paste here. The other alternative solution is from WideBoyDixon in the cross post here: https://www.excelforum.com/excel-pr...top-and-move-empty-rows-down.html#post5136240

His solution worked as it should so that is an alternative if someone would rather use that, but I found it more slow than DanteAmors solution, which is really fast and works perfect!

Thanks a lot DanteAmor for your help, I do really appreciate it and it helped me met the deadline!


I'm glad to help you. I appreciate your kind comments.
 
Upvote 0

Forum statistics

Threads
1,223,705
Messages
6,173,985
Members
452,540
Latest member
haasro02

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