VBA How do I insert a column to the left of a search result in a header?

Slavio

Board Regular
Joined
Mar 28, 2021
Messages
59
Office Version
  1. 365
Platform
  1. Windows
  2. Web
This code below adds a column next to the search term in the header on the right. How do I change this to add a column on the left?
Will there be a good soul here that will help me solve the mystery?

At the beginning
AutoBy foot
Current status
AutoBikeBy foot
Expected result
BikeAutoBy foot

VBA Code:
Sub AddRows()

    Sheets("Sheet1").Select
    Dim rngUsernameHeader As Range
    Dim rngHeaders As Range
          
    Set rngHeaders = Range("1:1") 'Looks in entire first row.
    Set rngUsernameHeader = rngHeaders.Find(what:="Auto", After:=Cells(1, 1))

    rngUsernameHeader.Offset(0, 1).EntireColumn.Insert
    rngUsernameHeader.Offset(0, 1).Value = "Bike"
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Try
VBA Code:
Sub AddRows()

    Sheets("Sheet1").Select
    Dim rngUsernameHeader As Range
    Dim rngHeaders As Range
          
    Set rngHeaders = Range("1:1") 'Looks in entire first row.
    Set rngUsernameHeader = rngHeaders.Find(what:="Auto", After:=Cells(1, 1))
    rngUsernameHeader.EntireColumn.Insert
    rngUsernameHeader.Offset(0, -1).Value = "Bike"
End Sub
 
Upvote 0
Solution
Try
VBA Code:
Sub AddRows()

    Sheets("Sheet1").Select
    Dim rngUsernameHeader As Range
    Dim rngHeaders As Range
         
    Set rngHeaders = Range("1:1") 'Looks in entire first row.
    Set rngUsernameHeader = rngHeaders.Find(what:="Auto", After:=Cells(1, 1))
    rngUsernameHeader.EntireColumn.Insert
    rngUsernameHeader.Offset(0, -1).Value = "Bike"
End Sub
Super Master Zot. thank you for your correct answer :) It works perfectly!
 
Upvote 0

Forum statistics

Threads
1,223,275
Messages
6,171,121
Members
452,381
Latest member
Nova88

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