Shift rows to the right based on the value in a cell

gwilkins

New Member
Joined
Dec 14, 2023
Messages
4
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I want to shift each row to the right based on the value in the cell in column A. Skip the header row.
If row 2 has a 1 in cell A2, then the entire second row shifts to the right one column.
If row 3 has a 2 in cell A3, then the entire third row shifts to the right 2 columns and so on through all rows on the active sheet.
ChatGPT failed me. :-()

Thanks!
 

Attachments

  • 2023-12-14 14_52_04-15000-06.xlsx - Excel.png
    2023-12-14 14_52_04-15000-06.xlsx - Excel.png
    18.7 KB · Views: 8

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
What triggers the shift? One time execution, or each time a value is changed in column A? Does the shift include column A or start at column B?
 
Upvote 0
What triggers the shift? One time execution, or each time a value is changed in column A? Does the shift include column A or start at column B?
one time. I'm just cleaning up the spreadsheet the way I want.
 
Upvote 0
Alright, two options then:

Including column A:
VBA Code:
Private Sub DataShiftA()
Dim lRow As Long, shiftN As Long, i As Long
Dim rng As Range, c As Range

lRow = Range("A" & Rows.Count).End(xlUp).Row

Set rng = Range("A2:A" & lRow)

Application.ScreenUpdating = False
For Each c In rng
    shiftN = c.Value
    For i = 1 To shiftN
        c.Insert Shift:=xlToRight
    Next i
Next c
Application.ScreenUpdating = True
End Sub

Excluding column A:
VBA Code:
Private Sub DataShiftB()
Dim lRow As Long, shiftN As Long, i As Long
Dim rng As Range, c As Range

lRow = Range("A" & Rows.Count).End(xlUp).Row

Set rng = Range("B2:B" & lRow)

Application.ScreenUpdating = False
For Each c In rng
    shiftN = c.Offset(, -1).Value
    For i = 1 To shiftN
        c.Insert Shift:=xlToRight
    Next i
Next c
Application.ScreenUpdating = True
End Sub
 
Upvote 1
Solution
Alright, two options then:

Including column A:
VBA Code:
Private Sub DataShiftA()
Dim lRow As Long, shiftN As Long, i As Long
Dim rng As Range, c As Range

lRow = Range("A" & Rows.Count).End(xlUp).Row

Set rng = Range("A2:A" & lRow)

Application.ScreenUpdating = False
For Each c In rng
    shiftN = c.Value
    For i = 1 To shiftN
        c.Insert Shift:=xlToRight
    Next i
Next c
Application.ScreenUpdating = True
End Sub

Excluding column A:
VBA Code:
Private Sub DataShiftB()
Dim lRow As Long, shiftN As Long, i As Long
Dim rng As Range, c As Range

lRow = Range("A" & Rows.Count).End(xlUp).Row

Set rng = Range("B2:B" & lRow)

Application.ScreenUpdating = False
For Each c In rng
    shiftN = c.Offset(, -1).Value
    For i = 1 To shiftN
        c.Insert Shift:=xlToRight
    Next i
Next c
Application.ScreenUpdating = True
End Sub
You sir, can now claim that you are smarter than ChatGPT!! Thank you very much!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
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