Using VB to locate a comma

apgriffiths

Board Regular
Joined
Jun 2, 2006
Messages
99
Hi all

I have some code but i need to put a logical argument in that is looking for a comma within a string.
Can this be done??


Cheers

Antony
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Antony

Can we see your current code?
Code:
strval = "Test , String"

MsgBox strval & " has a comma at position " & InStr(strval, ",")
 
Upvote 0
Hi

Here is a snippet of the code. Where i have put a * in the first If statement is where i want it to have looked for a comma but it cant find one.

<font face=Courier New>stFullNames = Cells(1, "D").Value
iCommaLoc = InStr(1, stFullNames, ",")

<SPAN style="color:#00007F">If</SPAN> * <SPAN style="color:#00007F">Then</SPAN>
bComma = <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">Else</SPAN>
bComma = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
<SPAN style="color:#00007F">If</SPAN> bComma = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
<SPAN style="color:#00007F">GoTo</SPAN> NoComma
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN></FONT>
 
Upvote 0
Antony

Why not just check the value of iCommaLoc?
Code:
If iCommaLoc>0 Then
   bComma = True
Else
   bComma = False
End If
Or even this.
Code:
bComma = iCommaLoc>0
By the way why are you using Goto?
 
Upvote 0
The GoTo was just something i took from another line from error handling i just havent got rid of it yet.

That code has worked. I feel so stupid cause that was a simply solution that i have used many times. Anyway cheers for that.

Antony
 
Upvote 0

Forum statistics

Threads
1,214,639
Messages
6,120,679
Members
448,977
Latest member
dbonilla0331

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