# Macro moving cells

#### alvbnp

##### Board Regular
Sample data:

1234567-123
abcde | fghijk

to

1234567-123 | abcde | fghijk

If A1 = 1234567-123 Then
move A2 and B2 to B1 and C1

For the whole column A

A1 will always be numeric format as shown.

Thanks.

### Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi
something like
Code:
``````Sub test()
Dim r As Range
For Each r In Range("a1",Range("a" & Rows.Count).End(xlUp))
If r.Text = "1234567-123" Then r.Offset(,1).Value = r.Offset(1).Value
Next
End Sub``````

Sorry, maybe I didn't make it clear enough. The numeric "1234567-123" can be any other number throughout column A.

eg:
0123456-456
7894561-789
9876543-987

Sorry, maybe I didn't make it clear enough. The numeric "1234567-123" can be any other number throughout column A.

eg:
0123456-456
7894561-789
9876543-987

Are the cells formated as "0000000-000" ?

Sorry, maybe I didn't make it clear enough. The numeric "1234567-123" can be any other number throughout column A.

eg:
0123456-456
7894561-789
9876543-987

Are the cells formated as "0000000-000" ?

Yes.

try
Code:
``````Sub sample()
Dim r As Range
With CreateObject("VBScript.RegExp")
.Pattern = "^\d{7}-\d{3}\$"
For Each r In Range("a1",Range(2a" & Rows.Count).End(xlUp))
If .test(r.Text) Then r.Offset(,1).Value = r.Offset(1).Value
Next
End With
End Sub``````

try
Code:
``````Sub sample()
Dim r As Range
With CreateObject("VBScript.RegExp")
.Pattern = "^\d{7}-\d{3}\$"
For Each r In Range("a1",Range("a" & Rows.Count).End(xlUp))
If .test(r.Text) Then r.Offset(,1).Value = r.Offset(1).Value
Next
End With
End Sub``````

result:

1234567-123
abcde | fghijk

to

1234567-123 | abcde

'fghijk' is not moved.

Ah...
Code:
``````Sub sample()
Dim r As Range
With CreateObject("VBScript.RegExp")
.Pattern = "^\d{7}-\d{3}\$"
For Each r In Range("a1",Range(2a" & Rows.Count).End(xlUp))
If .test(r.Text) Then r.Offset(,1).Resize(,2).Value = r.Offset(1).Resize(,2).Value
Next
End With
End Sub``````

Perfect. Thanks a lot, Jindon. You have helped me a lot.

Replies
13
Views
163
Replies
5
Views
221
Replies
2
Views
3K
Replies
2
Views
123
Replies
12
Views
283

1,219,959
Messages
6,151,154
Members
451,011
Latest member
Pigdog89

### 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.

### Which adblocker are you using?

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

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