Search For String and add Column next to it

fredlo2008

Active Member
Joined
Jan 12, 2012
Messages
254
Hello,

I am trying to get a code that will look for a string in the first row of my workbook and insert a column next to the found cell.

Thanks a lot
 

Some videos you may like

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try like this

Code:
Sub atest()
Dim Found As Range
Set Found = Rows(1).Find(what:="b", LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then Found.EntireColumn.Insert
End Sub
 

fredlo2008

Active Member
Joined
Jan 12, 2012
Messages
254
Thanks a lot. It works. But the column is inserted before the Column the text was found in. Is there a way to set it so it inserts in in front of it?
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Maybe

Code:
Sub atest()
Dim Found As Range
Set Found = Rows(1).Find(what:="b", LookIn:=xlValues, lookat:=xlWhole)
If Not Found Is Nothing Then Found.Offset(, 1).EntireColumn.Insert
End Sub
 

drsarao

Well-known Member
Joined
Sep 9, 2009
Messages
1,143
Office Version
  1. 2007
Platform
  1. Windows

ADVERTISEMENT

Maybe
Set Found = Rows(1).Find(what:="b", LookIn:=xlValues, lookat:=xlWhole)

VoG,
How does ="b" work to identify ANY string?
Thanks.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
It doesn't work for any string. I just used b for testing. You need to replace b with the search string.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,518
Messages
5,596,631
Members
414,082
Latest member
sasmita

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
Top