How do I convert text ie apple to string "apple" in excel or VBA?

marc005

Board Regular
Joined
Apr 21, 2013
Messages
58
How do I convert text ie apple to string "apple" in excel or VBA?
I need the text as a string to use MATCH. I have looked everywhere but cant find a way.

for example: if you put kk in cell A1, pp in cell B1 and rr in cell C1 and then in some
other cell you put =MATCH(kk,A1:C1) it will not work you need to
put =MATCH("kk",A1:C1)

Also if you want to convert text to string it will not work to put for
example ="a1". if you manually type =MATCH("kk",A1:C1) it is ok but since
the code run in VBA I cant type this manually hence I need to convert all
text elements in a range to strings. Complicated!
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Why not just A1?

=MATCH(A1,A1:C1)

Works for me.
 
Upvote 0
Like this.
Code:
Dim Res As Variant

Res = Application.Match(Range("A1").Value, Range("A1:C1").Value)
 
Upvote 0
Is That What You Want (Replacing The Text Between Brackets As The Following EX: range("d1")
Code:
Range(Type Any Address Here).Formula = "=MATCH(""kk"",A1:C1)"
ZAX
 
Upvote 0
@Nori,

Public Sub ff()
Application.Match(Sheets("Sheet3").Range("A1").Value, Sheets("Sheet3").Range("A1:C1").Value)
End Sub

does not work! It gives an error "syntax error".

@ZAX I will get back to you. I will try that :)
 
Last edited:
Upvote 0
Hi,

I think that in all formulas suggested is missing the 3rd argument = 0 (exact match).

M.
 
Upvote 0
Maybe...

Code:
Sub aTest()
    Dim myVar As Long, lookupValue As String
    
    With Sheets("Sheet1")
        lookupValue = .Range("M1").Value ' Assumes M1 houses "kk"
        myVar = Application.Match(lookupValue, .Range("A1:C1"), 0)
        MsgBox myVar
    End With
End Sub

Assumes data in Sheet1

M.
 
Upvote 0
@ZAX that works great :) I have been looking for this for several days ha ha
One thing to note though:

Public Sub ff()
Range("A5").Formula = "=MATCH(""kk"",A1:C1)"
End Sub

works fine however the below does not work at all:

Public Sub ff()
Range(Sheets("Sheet3").Range("A5")).Formula = "=MATCH(""kk"",Sheets("Sheet3").Range("A1;C1"))"
End Sub

I have to check if that is also the case for @Noris answer.
 
Upvote 0
The code I posted does work, you just haven't assigned the result to anything.

This will only work if A5 on Sheet3 contains a valid address.
Code:
Public Sub ff()
 Range(Sheets("Sheet3").Range("A5")).Formula = "=MATCH(""kk"",Sheets("Sheet3").Range("A1;C1"))"
 End Sub
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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