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
 
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
Can the values copied across be formatted as date?
The B Column cells are originally formatted as that style of date
 
Upvote 0

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Comment (put an apostrophe in front) or delete first two lines.
Code:
            With c
'                .NumberFormat = "@"    '<----Comment or remove
'                .HorizontalAlignment = xlRight    '<----Comment or remove
                .Value = Format(c.Offset(, -1).Value, "d-mmm")
                .Offset(, -1).ClearContents
            End With
 
Upvote 0
@jolivanes
Thanks for all the help. Code now works exactly how I wanted.
Tomorrow I'll have to dissect your code to understand what it is actually doing, and maybe learn something, lol
Thanks again!
 
Upvote 0
Thanks for the update and good luck.
The code is pretty simple (otherwise I would not have been able to put it together!!!), so you should have no problem figuring it out.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,118
Members
453,021
Latest member
Justyna P

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