Macro to Shift Cells if Value not found

MrMatt

New Member
Joined
Apr 26, 2012
Messages
30
Hi

Hoping someone could help me with the following. I need a macro that will check all cells in Column A and if a colon is not present in the cell then shift the cell to the right.

Before:

Column AColumn BColumn C
123456789
12:40abcdef
Textghijkl

<tbody>
</tbody>


After:

Column AColumn BColumn CColumn D
123456789
12:40abcdef
Textghijkl

<tbody>
</tbody>




Thanks in advance.

Matt
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Here you go give this a shot:

Code:
Sub ShiftRight()
Dim LastRow     As Long
Dim LastCol     As Long
Dim i           As Long


LastRow = Range("A65536").End(xlUp).Row


For i = 1 To LastRow
    If InStr(1, Cells(i, 1).Text, ":") = 0 Then
    LastCol = Cells(i, Columns.Count).End(xlToLeft).Column
        Range(Cells(i, 1), Cells(i, LastCol)).Cut Destination:=Range(Cells(i, 2), Cells(i, LastCol + 1))
    End If
Next i




End Sub
 
Upvote 0
Beat me to it Comfy haha..

Here's my attempt at in anyway :P

Code:
Sub test()
Dim Rng As Range, i As Range

With ActiveSheet
    Set Rng = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
    For Each i In Rng
        If InStr(1, i.Text, ":", 1) = 0 Then i.Insert Shift:=xlToRight
    Next i
End With
End Sub
 
Upvote 0
Here you go give this a shot:

Code:
Sub ShiftRight()
Dim LastRow     As Long
Dim LastCol     As Long
Dim i           As Long


LastRow = Range("A65536").End(xlUp).Row


For i = 1 To LastRow
    If InStr(1, Cells(i, 1).Text, ":") = 0 Then
    LastCol = Cells(i, Columns.Count).End(xlToLeft).Column
        Range(Cells(i, 1), Cells(i, LastCol)).Cut Destination:=Range(Cells(i, 2), Cells(i, LastCol + 1))
    End If
Next i




End Sub

Thanks, this works :)
 
Upvote 0
Beat me to it Comfy haha..

Here's my attempt at in anyway :P

Code:
Sub test()
Dim Rng As Range, i As Range

With ActiveSheet
    Set Rng = .Range("A2:A" & .Range("A" & Rows.Count).End(xlUp).Row)
    For Each i In Rng
        If InStr(1, i.Text, ":", 1) = 0 Then i.Insert Shift:=xlToRight
    Next i
End With
End Sub

As does this :)

Thanks both for the prompt responses.
 
Upvote 0

Forum statistics

Threads
1,216,119
Messages
6,128,946
Members
449,480
Latest member
yesitisasport

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