How can I shift all cells right based on a cell value?

grooveriw

New Member
Joined
May 1, 2017
Messages
3
I am looking for a way to shift all the cells in a row to the right based on the value in one of the cells in that row as my manually created sample below shows.
In this example, I want to shift the cells right based on the value in column A.
Original Spreadsheet:-
ABCD
10111111John54
21111211Bob34
32111212Fred2
42111214Chris0
51111215Jane1
61111220Joan17
72121101Sally25
83121110Harry4
92121113Henry9

<tbody>
</tbody>

After:-
ABCDEFG
10111111John54
21111211Bob34
32111212Fred2
42111214Chris0
51111215Jane1
61111220Joan17
72121101Sally25
83121110Harry4
92121113Henry9

<tbody>
</tbody>

Thank you in advance.
I just know that some genius out there will know how to do it.
many regards
Graham
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Code:
Dim rng As Range, cel As Range
Set rng = Range([A1], Cells(Rows.Count, 1).End(xlUp))
For Each cel In rng
    If cel > 0 Then cel.Resize(, cel).Insert Shift:=xlToRight
Next
 
Upvote 0
Thanks footoo,
I have tried entering the code you posted but get an error

Compile error:
Invalid outside procedure

Haven't a clue what this means.

What am I doing wrong?

thanks
 
Upvote 0
You need it to look like this:
Code:
Sub My_Sub()
Dim rng As Range, cel As Range
Set rng = Range([A1], Cells(Rows.Count, 1).End(xlUp))
For Each cel In rng
    If cel > 0 Then cel.Resize(, cel).Insert Shift:=xlToRight
Next
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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