VBA: Insert 2 columns if header matches

Fazila

Board Regular
Joined
Nov 19, 2014
Messages
163
I need to insert 2 columns if the header contains "*FFT Target". I've found this code, however, it does not move on to the next column containing "FFT Target" but inserts the two rows before the first column where the heading matched.

Sheet headers I currently have are:

English FFT TargetEnglish Teacher AssessmentEnglish EFGMaths FFT TargetMaths Teacher AssessmentMaths EFGScience FFT TargetScience Teacher AssessmentScience EFGArt FFT TargetArt Teacher AssessmentArt EFG

<tbody>
</tbody>

What I need is:

English FFT TargetEnglish Teacher AssessmentEnglish EFGMaths FFT TargetMaths Teacher AssessmentMaths EFGScience FFT TargetScience Teacher AssessmentScience EFGArt FFT TargetArt Teacher AssessmentArt EFG

<tbody>
</tbody>


The code I have is:

Code:
Sub inscols()

    Dim A As Range
    Dim lc As Long
    Dim i As Long
        
    Set A = Rows(1).Find(what:="*Target", LookIn:=xlValues, lookat:=xlPart)
    lc = Cells(1, Columns.Count).End(xlToLeft).Column
    
    For i = 2 To lc
          
            If A Is Nothing Then Exit Sub
            A.Resize(, 2).EntireColumn.Insert
    Next i
        
End Sub

I'm assuming instead of next i it should be next A or something?

Any help would be greatly appreciated.

Thanks
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
if anyone is interested in a solution I got a response from another forum which worked great.

<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">Sub x()

Dim A As Range
Dim lc As Long
Dim i As Long
Dim s As String

Set A = Rows(1).Find(What:="Target", after:=Cells(1, 1), LookIn:=xlValues, LookAt:=xlPart, _
SearchDirection
:=xlPrevious, MatchCase:=False, SearchFormat:=False)

If Not A Is Nothing Then
s
= A.Address
Do
A
.Resize(, 2).EntireColumn.Insert
s
= Range(s).Offset(, 2).Address
Set A = Rows(1).FindNext(A)
Loop Until A.Address = s
End If

End Sub</code>
 
Upvote 0

Forum statistics

Threads
1,214,598
Messages
6,120,441
Members
448,966
Latest member
DannyC96

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