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

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
Antony

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

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

apgriffiths

Board Regular
Joined
Jun 2, 2006
Messages
99
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>
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,303
Office Version
  1. 365
Platform
  1. Windows
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?
 

apgriffiths

Board Regular
Joined
Jun 2, 2006
Messages
99
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
 

Forum statistics

Threads
1,141,016
Messages
5,703,748
Members
421,312
Latest member
Mooncake1

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