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:-
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]0[/TD]
[TD]111111[/TD]
[TD]John[/TD]
[TD]54[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]1[/TD]
[TD]111211[/TD]
[TD]Bob[/TD]
[TD]34[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]2[/TD]
[TD]111212[/TD]
[TD]Fred[/TD]
[TD]2[/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]2[/TD]
[TD]111214[/TD]
[TD]Chris[/TD]
[TD]0[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]1[/TD]
[TD]111215[/TD]
[TD]Jane[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]1[/TD]
[TD]111220[/TD]
[TD]Joan[/TD]
[TD]17[/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD]2[/TD]
[TD]121101[/TD]
[TD]Sally[/TD]
[TD]25[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD]3[/TD]
[TD]121110[/TD]
[TD]Harry[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD]2[/TD]
[TD]121113[/TD]
[TD]Henry[/TD]
[TD]9[/TD]
[/TR]
</tbody>[/TABLE]

After:-
[TABLE="class: grid, width: 500, align: center"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]0[/TD]
[TD]111111[/TD]
[TD]John[/TD]
[TD]54[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]111211[/TD]
[TD]Bob[/TD]
[TD]34[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[TD]111212[/TD]
[TD]Fred[/TD]
[TD]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[TD]111214[/TD]
[TD]Chris[/TD]
[TD]0[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]111215[/TD]
[TD]Jane[/TD]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD]1[/TD]
[TD]111220[/TD]
[TD]Joan[/TD]
[TD]17[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[TD]121101[/TD]
[TD]Sally[/TD]
[TD]25[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]3[/TD]
[TD]121110[/TD]
[TD]Harry[/TD]
[TD]4[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD]2[/TD]
[TD]121113[/TD]
[TD]Henry[/TD]
[TD]9[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]

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

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
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,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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