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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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