inserting new blank columns every nth column

grantdowie

New Member
Joined
Dec 5, 2017
Messages
4
Hello

I would like to add three new blank columns to an Excel spreadsheet using VBA after every 7 columns and I would very much appreciate any help to do this.

Thanks

Grant
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Hi & welcome to the board
Is this what you want?
Code:
Sub InsertColumns()

    Dim UsdCols As Long
    Dim Cnt As Long
    
    UsdCols = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column
    For Cnt = Int(UsdCols / 7) * 7 To 7 Step -7
        Columns(Cnt + 1).Resize(, 3).Insert
    Next Cnt
    
End Sub
 
Upvote 0
Glad to help & thanks for the feedback
 
Upvote 0
Hello Fluff

Sorry to trouble you again but I wondered whether you could help me further or direct me to a suitable article on the Forum.

Thanks to your excellent help I now have all my data in columns of varying row lengths separated by seven blank columns. Essentially I would like to populate these blank columns one by one via VBA by reference to this data. I would like to replicate the formula in each blank column cell for every eighth column cell until the end of the spreadsheet.

Thanks again.

All the best

Grant
 
Upvote 0
Something like this
Code:
Sub InsertFormula()

    Dim UsdCols As Long
    Dim Cnt As Long
    
    UsdCols = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column
    For Cnt = 8 To UsdCols Step 8
        Range(Cells(2, Cnt), Cells(Rows.Count, Cnt - 1).End(xlUp).Offset(, 1)).FormulaR1C1 = "=rc[-1]"
    Next Cnt
    
End Sub
 
Upvote 0
Hello Fluff

Thanks very much for the new routine. I think I am doing something wrong in running it though. For example I was trying to replicate the formula stored in D10 along the columns and I am not sure how I tell the spreadsheet how to do that via your routine.

All the best

Grant
 
Upvote 0
This will take the formula in D2 & copy it into columns 8,16,24 etc from row2 to the last used cell in the previous column.
Code:
Sub InsertFormula()

    Dim UsdCols As Long
    Dim Cnt As Long
    
    UsdCols = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column
    For Cnt = 8 To UsdCols Step 8
        Range(Cells(2, Cnt), Cells(Rows.Count, Cnt - 1).End(xlUp).Offset(, 1)).Formula = Range("D2").Formula
    Next Cnt
    
End Sub
 
Upvote 0
Hi & welcome to the board
Is this what you want?
Code:
Sub InsertColumns()

    Dim UsdCols As Long
    Dim Cnt As Long
   
    UsdCols = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).column
    For Cnt = Int(UsdCols / 7) * 7 To 7 Step -7
        Columns(Cnt + 1).Resize(, 3).Insert
    Next Cnt
   
End Sub
Thank you Fluff. I was able to use these codes for my project.
 
Upvote 0
Glad it helped & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,962
Messages
6,175,654
Members
452,664
Latest member
alpserbetli

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