VBA to move range if cell value is not empty

fotodj

New Member
Joined
Jul 19, 2014
Messages
36
Office Version
  1. 2016
Platform
  1. Windows
11-12-2024 11-03-30 AM.jpg



VBA Code:
Sub MoveRange()

 Dim i As Long
    For i = 2 To 10
 
        If Not IsEmpty(Range("A" & i)) Then _
            Range("F" & i) = Range("A" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("G" & i) = Range("B" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("H" & i) = Range("C" & i)
       
        If Not IsEmpty(Range("A" & i)) Then _
            Range("C" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("B" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("A" & i).ClearContents
  
                   
    Next i
 
End Sub


So far my "poor" coding attempt moves the Range ("A:C") to Range("F:H") and clear contents of Range ("A:C") where A cell was not empty... I need help to modify it...
the goal is to move Range(A:C) to next empty row in Range ("F:H") if Col A not empty. After the move, the range("A:C") where A cell was not empty should be deleted.
 
Last edited:

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
View attachment 119184


VBA Code:
Sub MoveRange()

 Dim i As Long
    For i = 2 To 10
 
        If Not IsEmpty(Range("A" & i)) Then _
            Range("F" & i) = Range("A" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("G" & i) = Range("B" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("H" & i) = Range("C" & i)
      
        If Not IsEmpty(Range("A" & i)) Then _
            Range("C" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("B" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("A" & i).ClearContents
 
                  
    Next i
 
End Sub


So far my "poor" coding attempt moves the Range ("A:C") to Range("F:H") and clear contents of Range ("A:C") where A cell was not empty... I need help to modify it...
the goal is to move Range(A:C) to next empty row in Range ("F:H") if Col A not empty. After the move, the range("A:C") where A cell was not empty should be deleted.
What version of Excel are you using as it may be possible by just using a formula?
 
Upvote 0
I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
View attachment 119184


VBA Code:
Sub MoveRange()

 Dim i As Long
    For i = 2 To 10
 
        If Not IsEmpty(Range("A" & i)) Then _
            Range("F" & i) = Range("A" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("G" & i) = Range("B" & i)
        If Not IsEmpty(Range("A" & i)) Then _
            Range("H" & i) = Range("C" & i)
      
        If Not IsEmpty(Range("A" & i)) Then _
            Range("C" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("B" & i).ClearContents
        If Not IsEmpty(Range("A" & i)) Then _
            Range("A" & i).ClearContents
 
                  
    Next i
 
End Sub


So far my "poor" coding attempt moves the Range ("A:C") to Range("F:H") and clear contents of Range ("A:C") where A cell was not empty... I need help to modify it...
the goal is to move Range(A:C) to next empty row in Range ("F:H") if Col A not empty. After the move, the range("A:C") where A cell was not empty should be deleted.

Try this.

Make sure that the sheet is the active sheet.

VBA Code:
Public Sub subRearrange()
Dim rngLeft As Range
Dim rngRight As Range
Dim arr() As Variant
Dim i As Integer

  Set rngLeft = Range("A2:C7") ' <= Change this to the block of data indicated
                                  ' in orange on the example sheet.
  
  Set rngRight = Range("F3:H3") ' <= Change this to the last row of data
                                  ' as indicated in purple on the example sheet.

  arr = rngLeft.Value
  
  rngLeft.ClearContents
  
  Set rngLeft = rngLeft.Cells(1).Resize(1, 3).Offset(-1, 0)
  
  For i = 1 To UBound(arr)
    
    If arr(i, 1) <> "" Then
    
      Set rngRight = rngRight.Offset(1, 0)
      
      rngRight.Value = Array(arr(i, 1), arr(i, 2), arr(i, 3))
      
    Else
      
      Set rngLeft = rngLeft.Offset(1, 0)
      
      rngLeft.Value = Array(arr(i, 1), arr(i, 2), arr(i, 3))
    
    End If
  
  Next i
  
End Sub

VBA to move range if cell value is not empty.xlsm
ABCDEFGH
1Col ACol BCol CCol FCol GCol H
2456FGHEEE111TTTOOO
3789GHNFFF222UUUWWW
4WER
5724DFGHHH
6MKY
7455HYODDD
Sheet1
 
Upvote 0
Solution

Forum statistics

Threads
1,223,770
Messages
6,174,433
Members
452,563
Latest member
mvdk

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