Returning column number when searched for a string

kiran234

New Member
Joined
Aug 19, 2011
Messages
2
I wrote a function that search for required string and returns column number of that string. But it is not accurately returning the column numbers when duplicate of strings are in the range( ex: "abc" and "abc ghed" ; when I searched for word "abc" it is retuning the column number of "abc ghed"). Can you please help me to make it accurate.


Function ColumnNumber(SearStr As Variant)
Dim c As Variant Dim sh As Worksheet
Dim colcount As Long Set sh= Sheets("SheeName")
colcount = sheet.UsedRange.Columns.Count
For Each c In sheet.Range(sh.Cells(1, 1), sh.Cells(3, colcount))
If c.Value = SearStr Then
ColumnNumber = sh.Cells.Find(SearStr).Column
ColumnAddres = Mid(c.Address, 2, InStr(2, c.Address, "$") - 2)
End If
Next c
End Function '
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try this:

Code:
Function ColumnNumber(SearStr As Variant) As Long
    Dim cell        As Range
    Set cell = Worksheets("SheetName").Rows("1:3").Find(What:=SearStr, _
                                                       LookIn:=xlValues, _
                                                       LookAt:=xlWhole, _
                                                       MatchCase:=False)
    If Not cell Is Nothing Then colcount = cell.Column
End Function
 
Upvote 0
Oops:

Code:
Function ColumnNumber(SearStr As Variant) As Long
    Dim cell        As Range
    Set cell = Worksheets("SheetName").Rows("1:3").Find(What:=SearStr, _
                                                       LookIn:=xlValues, _
                                                       LookAt:=xlWhole, _
                                                       MatchCase:=False)
    If Not cell Is Nothing Then [COLOR=red]ColumnNumber[/COLOR] = cell.Column
End Function
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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