Inserting columns

Gaijin

Board Regular
Joined
Jul 21, 2002
Messages
56
I am sure there is an easy sollution to this problem, however I am not that good with VBA. What I am trying to do is insert a series of columns on a row corresponding to the number in the cell on the row in column A.

For example if column A contains a 3 in want ot insert 3 columns between A and B in that row only. I also need it to do this for the number of rows I select.

I searched the board but could not find anything like this.
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
In the example below, which is only valid for row 1, put the code in a module in a trial worksheet, place any number in A1 and something in B1 (to check it works) :P then run the macro.
Code:
Sub Insertcells()
    Dim iInsert As Byte
    
    Range("B1").Select
    For iInsert = 1 To Range("A1").Value
        Selection.Insert Shift:=xlToRight
    Next
End Sub

If you want it to extend to other rows then you would need to specify whether it was for a set of rows or for every row containing a number in the A cell.

HTH :)
 
Upvote 0
I need it to run for every row on the spreadsheet that contains a number in cell A. Thank you for your help.
 
Upvote 0
Try this and see if it gives desired results.

Code:
Sub InsertColumns()

Dim l As Long
Dim c As Integer

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

For i = 1 To l

    If IsNumeric(Cells(i, 1)) Then
        c = Cells(i, 1)
        Range(Cells(i, 2), Cells(i, 1 + c)).Insert Shift:=xlToRight
    End If

Next i

End Sub
 
Upvote 0
Sorry for the delay in replying but Tea was called! In the meantime you have a perfect solution from TommyGun.

Best wishes
 
Upvote 0
That is perfect. I guess I need to sit down and learn VBA one of these days. Mrexcel is 100% in coming up with solutions for me. As always thank you everyone.
 
Upvote 0

Forum statistics

Threads
1,221,700
Messages
6,161,378
Members
451,700
Latest member
Eccymarge

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