0

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 Target English Teacher Assessment English EFG Maths FFT Target Maths Teacher Assessment Maths EFG

What I need is

[blank column] [blank column] English FFT Target English Teacher Assessment English EFG [blank column] [blank column] Maths FFT Target Maths Teacher Assessment Maths EFG

The code I have is:

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

Unfortunately, this code inserts all the columns before English FFT Target rather than moving on and inserting columns before the next column containing FFT Target.

Any help would be greatly appreciated.

Thanks

1
  • You need a loop. A returns a single range and is never updated. Also, please post a screenshot as your layout is not clear from your question. Commented Feb 28, 2019 at 11:16

2 Answers 2

2

i think this could helps you:

Option Explicit

Sub Insert()

    Dim LastColumn As Long, i As Long, Position As Long

    With ThisWorkbook.Worksheets("Sheet1")

        'Get the last column of Sheet 1, row 1
        LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column

        For i = LastColumn To 1 Step -1

            Position = InStr(1, .Cells(1, i), "FFT Target")

            If Position <> 0 Then

                .Range(.Cells(, i), .Cells(, i + 1)).EntireColumn.Insert

            End If

        Next i

    End With

End Sub
Sign up to request clarification or add additional context in comments.

5 Comments

++ for posting a similar solution before me :D However you can shorten your code...
Really appreciate your tips!
Thanks for posting Error 1004 unfortunately didn't work I think the issue was finding FFT Target it needs to match part of the cell rather than the whole cell.
@Fazila: What happens if you change InStr(1, .Cells(1, i), "FFT Target") to InStr(1, .Cells(1, i).Value, "FFT Target", vbTextCompare)
@Fazila: or InStr(1, .Cells(1, i).Value, "Target", vbTextCompare) in case FFT is not that important...
1

In case you want to stick with Find. As you are inserting columns, you need a bit of jiggery-pokery to check you are not recycling previously found values, and you need to set the search direction. Setting other parameters is also good practice.

However, I think Error 1004's approach makes more sense here.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.