Loop to test if cell not empty then move value to next column?

hazmat

New Member
Joined
Jun 14, 2019
Messages
39
Office Version
  1. 2016
Platform
  1. Windows
Looking for code help to loop through A1:A10, and if A is Not Empty, the value of A will be moved over 1 column, to B.
Existing Data In B cannot be disturbed. The Data in Column A is being generated by a Formula.

I have the following code for the 1st 3 cells, but just don't have the knowledge to create a loop for the whole range of cells (Which will ultimately be AE8:AE157 values moved to AF8:AF157, respectively).
Been searching for hours with no luck. Thanks for any help.
VBA Code:
Private Sub DataTransfer()

If Not IsEmpty(Range("a1").Value) Then
Range("B1").Value = Range("A1").Value
Range("A1").Clear
End If

If Not IsEmpty(Range("a2").Value) Then
Range("B2").Value = Range("A2").Value
Range("A2").Clear
End If

If Not IsEmpty(Range("a3").Value) Then
Range("B3").Value = Range("A3").Value
Range("A3").Clear
End If

End Sub

From:
Screenshot 2024-08-05 183339.png

To This:
Screenshot 2024-08-05 183509.png
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
A1, A3, and A8 are not empty. Why are the values in these cells not moved to column B ?
Also what is meant by "Existing Data In B cannot be disturbed" ?
 
Upvote 0
Try so.
Code:
Sub Maybe()
Dim lr As Long, c As Range
lr = Range("A:B").Find("*", , xlValues, xlPart, xlByRows, xlPrevious).Row
    For Each c In Range("B1:B" & lr)
        If Len(c) = 0 And c.Offset(, -1).Value <> "" Then
            With c
                .NumberFormat = "@"
                .Value = Format(c.Offset(, -1).Value, "d-mmm")
            End With
        End If
    Next c
End Sub
 
Upvote 0
Solution
A1, A3, and A8 are not empty. Why are the values in these cells not moved to column B ?
Also what is meant by "Existing Data In B cannot be disturbed" ?
A1, A3 and A8 do get moved to B
The first image is what I start with, and the 2nd image is what i need to happen.
The snippet of code i shown works fine, but I would have to copy it 147 more times for the whole range i ultimately want to move, if the Not IsEmpty is met.
I just don't know how to write the loop, to make the the code more efficient.

The "Existing Data In B cannot be disturbed" means that the data In B cannot be moved, overwritten, deleted etc., even temporarily.
The way my Workbook works is that if there is data in a particular Cell in Column A, the adjacent Cell in B will always be empty
 
Upvote 0
Try this :
VBA Code:
Sub v()
With [B:B].SpecialCells(xlCellTypeBlanks)
    .FormulaR1C1 = "=IF(RC[-1]="""","""",RC[-1])"
    [B:B].Value = [B:B].Value
    .Offset(0, -1).ClearContents
End With
End Sub
 
Upvote 0
What is the result when running the macro from Post #3?

To delete the copied value from Column A, change the "With.....End With" to so
Code:
            With c
                .NumberFormat = "@"
                .Value = Format(c.Offset(, -1).Value, "d-mmm")
                .Offset(, -1).ClearContents    '<---- Insert this line
            End With
 
Last edited:
Upvote 0
What is the result when running the macro from Post #3?

To delete the copied value from Column A, change the "With.....End With" to so
Code:
            With c
                .NumberFormat = "@"
                .Value = Format(c.Offset(, -1).Value, "d-mmm")
                .Offset(, -1).ClearContents    '<---- Insert this line
            End With
With the added ClearContents line, it is almost there. See image below....Something to do with the Format?
Screenshot 2024-08-05 223812.png
 
Upvote 0
The values copied across are formatted as text.

Insert one more line.
Code:
            With c
                .NumberFormat = "@"
                .HorizontalAlignment = xlRight    '<---- Insert this line
                .Value = Format(c.Offset(, -1).Value, "d-mmm")
                .Offset(, -1).ClearContents
            End With
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,176
Members
451,543
Latest member
cesymcox

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