Search for exact match of string in excel row using VBA Macro

vamshedhar

New Member
Joined
Oct 3, 2013
Messages
8
I am having a row with values from 21 to 45 and continued again from 1 to 25

Some unknown number of rows in the beginning2526...4041424344451234..2425

<tbody>
</tbody>

to find column position of "1" i am using code

Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

But it is returning column number of 31 and when i delete or hide that column it returns column number of 41.

Cant i search for exact value 1 in my row???
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I am having a row with values from 21 to 45 and continued again from 1 to 25

Some unknown number of rows in the beginning
25
26
...
40
41
42
43
44
45
1
2
3
4
.
.
24
25

<TBODY>
</TBODY>

to find column position of "1" i am using code

Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

But it is returning column number of 31 and when i delete or hide that column it returns column number of 41.

Cant i search for exact value 1 in my row???


Try

Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

M.
 
Upvote 0
I am having a row with values from 21 to 45 and continued again from 1 to 25

Some unknown number of rows in the beginning
25
26
...
40
41
42
43
44
45
1
2
3
4
.
.
24
25

<TBODY>
</TBODY>

to find column position of "1" i am using code

Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column

But it is returning column number of 31 and when i delete or hide that column it returns column number of 41.

Cant i search for exact value 1 in my row???

change this: LookAt:=xlPart,

To this: LookAt:=xlWhole,
 
Upvote 0
Runtime error '91':

Object variable or With block variable not set

Try

Code:
Sub aTest()
    Dim myCol As Long
            
    myCol = Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    MsgBox myCol
        
End Sub

M.
 
Upvote 0
Try

Code:
Sub aTest()
    Dim myCol As Long
            
    myCol = Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False).Column
    MsgBox myCol
        
End Sub

M.

No improvement Sir... Same error repeats again.
 
Upvote 0
No improvement Sir... Same error repeats again.

Try this

Code:
Sub aTest()
    Dim myCol As Long, rngFound As Range
    
    Set rngFound = Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    If Not rngFound Is Nothing Then
        myCol = rngFound.Column
        MsgBox myCol
    Else
        MsgBox "Not Found"
    End If
    
End Sub

What message do you get?

M.
 
Upvote 0
Try this

Code:
Sub aTest()
    Dim myCol As Long, rngFound As Range
    
    Set rngFound = Worksheets("sheet1").Rows(2).Find(What:="1", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:=False)
    If Not rngFound Is Nothing Then
        myCol = rngFound.Column
        MsgBox myCol
    Else
        MsgBox "Not Found"
    End If
    
End Sub

What message do you get?

M.

Sir,
It says Not Found But there exists a 1 in the row...
When i tried to search for blank field keeping What:="" it gave correct position
and sir the values 25, 26, 27 are week numbers generated by N2=WEEKNUM(N3,1) where N3=1/Sep
 
Last edited:
Upvote 0
Sir,
It says Not Found But there exists a 1 in the row...
When i tried to search for blank field keeping What:="" it gave correct position
and sir the values 25, 26, 27 are week numbers generated by =WEEKNUM(N3,1) where N3=1/Sep

Sorry, I'm not following you. If there is a 1 in row 2 of Sheet1 it should work.

Worked for me



A

B

C

D

E

F

G

H

I

J

K

L

M

N

O

P

Q

R

S

2

25​

26​

27​

28​

29​

30​

31​

32​

33​

34​

35​

36​

37​

38​

39​

40​

41​

1​

2​

<TBODY>
</TBODY>


Returned 18, i.e., column R

M.
 
Upvote 0

Forum statistics

Threads
1,216,117
Messages
6,128,935
Members
449,480
Latest member
yesitisasport

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