Loop Insert Column and Function

exKW258

New Member
Joined
Dec 1, 2014
Messages
5
Hi, I have a 100+ column spreadsheet. For each column, I have to insert a new column (starting at column "C" to the last column) and fill it with text from the first cell of the column to the right. I use this code to make it work, but I have to copy it 100+ times for each column insert. Is there a way to loop this code?
'-------------------------
Sheets("Cost1").Select
Columns("C:C").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
Range("C2").Select
ActiveCell.FormulaR1C1 = "=LEFT(R1C4,6)"
Selection.AutoFill Destination:=Range("C2:C" & lastrow)
'-------------------------
Thanks for your help.
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
.
.

Try this:

Code:
Sub InsertColumns()

    Dim intLstCol As Integer
    Dim intColNum As Integer
    Dim lngLstRow As Long
    
    Application.ScreenUpdating = False
    intLstCol = Cells(2, Columns.Count).End(xlToLeft).Column
    For intColNum = intLstCol To 3 Step -1
        lngLstRow = Cells(Rows.Count, intColNum).End(xlUp).Row
        Columns(intColNum).Insert xlToRight
        Range(Cells(2, intColNum), Cells(lngLstRow, intColNum)).Formula = _
            "=LEFT(" & Cells(2, intColNum + 1).Address(False, False) & ",6)"
    Next intColNum
    Application.ScreenUpdating = True

End Sub
 
Upvote 0
Thank you very much. I just need to fix one small thing. I only need to fill the column with data from the first cell. So how do I further limit this statement to only copy/fill text from the first cell of the column "=LEFT($" & Cells(1, intColNum + 1).Address(False, False) & ",6)" Thanks.
 
Upvote 0
Thanks for your help gpeacock. The code works perfectly. I made a slight modification to suit my purposes. Here it is for anyone else who may have a similar request...

"=LEFT($" & Cells(1, intColNum + 1).Address(True, False) & ",6)"
 
Upvote 0

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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