#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default strings

Hi Im trying to ask questions of a string. My string is in "A1". I
assign it to 'teststring' which looks like "A 2 3 A 4" This can be
any combination with or without A or Numbers and contain 3 values upto
5. Id like to ask this of my string.
Does it contain "A", If yes How many....if more than 1 then....how many
values in string altogether.....if 3 go do something if three add the
numbers up in the string and use the A's as 1 eg 'A A 2 3 4' = 11.

Does it contain A, if no add al the values.

I could do with knowing the position of the A's but that a minor prob.
When I was doing this with 2 values I used If InStr("A", TestString)
0, as advised prev and this was great.
Hope this makes sense.
Regards Robert

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default strings

This might work:

Dim nAces As Long
Dim nCards As Long
Dim TestString As String
Dim Total As Long

nAces = 0
Total = 0
TestString = Replace(TestString, " ", "")
nCards = Len(TestString)

For i = 1 To nCards
If Mid(TestString, i, 1) = "A" Then nAces = nAces + 1
Next i

If nAces 1 Then
If nCards 3 Then
Total = nAces
For i = 1 To nCards
If Mid(TestString, i, 1) < "A" Then Total = Total +
Val(Mid(TestString, i, 1))
Next i
Else
' do something else
End If
End If


"RobcPettit" wrote:

Hi Im trying to ask questions of a string. My string is in "A1". I
assign it to 'teststring' which looks like "A 2 3 A 4" This can be
any combination with or without A or Numbers and contain 3 values upto
5. Id like to ask this of my string.
Does it contain "A", If yes How many....if more than 1 then....how many
values in string altogether.....if 3 go do something if three add the
numbers up in the string and use the A's as 1 eg 'A A 2 3 4' = 11.

Does it contain A, if no add al the values.

I could do with knowing the position of the A's but that a minor prob.
When I was doing this with 2 values I used If InStr("A", TestString)
0, as advised prev and this was great.
Hope this makes sense.
Regards Robert


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default strings

This pseudo code might help:

Teststring = Application.Trim(TestString)
if instr(1,teststring,"A",vbTextCompare) then
num = len(teststring)-len(replace(teststring,"A",""))
if num 1 then
s = Application.Replace(TestString," ","")
if instr(1,s,"AAAA",vbTextCompare) then
' greater than 3
s1 = Application.Replace(Ucase(TestString),"A","1")
s1 = Application.Replace(s1," ",",")
tot = Application.Evaluate("Sum(" & s1 & ")"
msgbox Tot
elseif instr(1,s,"AAA") then
' 3 only
else
' 2 only

end if
else
' 1 A's
end if
else
' No A's
s1 = Application.Replace(TestString," ",",")
tot = Evaluate("Sum(" & s1 & ")"
msgbox "No A's, Tot: " & tot
end if

your example of what to do when there are more than 3 A's

'A A 2 3 4' = 11

Only contains 2 A's.

--
Regards,
Tom Ogilvy

"RobcPettit" wrote:

Hi Im trying to ask questions of a string. My string is in "A1". I
assign it to 'teststring' which looks like "A 2 3 A 4" This can be
any combination with or without A or Numbers and contain 3 values upto
5. Id like to ask this of my string.
Does it contain "A", If yes How many....if more than 1 then....how many
values in string altogether.....if 3 go do something if three add the
numbers up in the string and use the A's as 1 eg 'A A 2 3 4' = 11.

Does it contain A, if no add al the values.

I could do with knowing the position of the A's but that a minor prob.
When I was doing this with 2 values I used If InStr("A", TestString)
0, as advised prev and this was great.
Hope this makes sense.
Regards Robert


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default strings

Thankyou Charlie. This is fantastic. Thanks for taking the time to
write this. I can use this to do almost anything with the cards now.
Regards Robert
Charlie wrote:

This might work:

Dim nAces As Long
Dim nCards As Long
Dim TestString As String
Dim Total As Long

nAces = 0
Total = 0
TestString = Replace(TestString, " ", "")
nCards = Len(TestString)

For i = 1 To nCards
If Mid(TestString, i, 1) = "A" Then nAces = nAces + 1
Next i

If nAces 1 Then
If nCards 3 Then
Total = nAces
For i = 1 To nCards
If Mid(TestString, i, 1) < "A" Then Total = Total +
Val(Mid(TestString, i, 1))
Next i
Else
' do something else
End If
End If


"RobcPettit" wrote:

Hi Im trying to ask questions of a string. My string is in "A1". I
assign it to 'teststring' which looks like "A 2 3 A 4" This can be
any combination with or without A or Numbers and contain 3 values upto
5. Id like to ask this of my string.
Does it contain "A", If yes How many....if more than 1 then....how many
values in string altogether.....if 3 go do something if three add the
numbers up in the string and use the A's as 1 eg 'A A 2 3 4' = 11.

Does it contain A, if no add al the values.

I could do with knowing the position of the A's but that a minor prob.
When I was doing this with 2 values I used If InStr("A", TestString)
0, as advised prev and this was great.
Hope this makes sense.
Regards Robert



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 703
Default strings

Now how did I know those were cards? :)

(do I say "hit me", or "15-for-2, 15-for-4, and a double-double-run of 4
makes...", or "GIN!" ?)

"RobcPettit" wrote:

Thankyou Charlie. This is fantastic. Thanks for taking the time to
write this. I can use this to do almost anything with the cards now.
Regards Robert
Charlie wrote:

This might work:

Dim nAces As Long
Dim nCards As Long
Dim TestString As String
Dim Total As Long

nAces = 0
Total = 0
TestString = Replace(TestString, " ", "")
nCards = Len(TestString)

For i = 1 To nCards
If Mid(TestString, i, 1) = "A" Then nAces = nAces + 1
Next i

If nAces 1 Then
If nCards 3 Then
Total = nAces
For i = 1 To nCards
If Mid(TestString, i, 1) < "A" Then Total = Total +
Val(Mid(TestString, i, 1))
Next i
Else
' do something else
End If
End If


"RobcPettit" wrote:

Hi Im trying to ask questions of a string. My string is in "A1". I
assign it to 'teststring' which looks like "A 2 3 A 4" This can be
any combination with or without A or Numbers and contain 3 values upto
5. Id like to ask this of my string.
Does it contain "A", If yes How many....if more than 1 then....how many
values in string altogether.....if 3 go do something if three add the
numbers up in the string and use the A's as 1 eg 'A A 2 3 4' = 11.

Does it contain A, if no add al the values.

I could do with knowing the position of the A's but that a minor prob.
When I was doing this with 2 values I used If InStr("A", TestString)
0, as advised prev and this was great.
Hope this makes sense.
Regards Robert






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 108
Default strings

Thaks for your replys. Your right, its cards, Blackjack. Starting with
two cards Im trying to calculate al the hand probabilitys. So far Ive
used your code Charlie, Havent explored your yet Tom, Thankyou. One
little problem Ive got is I can get the code to do everything I want
with cards 2 through to 9, and ace, jack, queen and king. But not 10.
This is because if I have A 10 the len of teststring returns 3, do you
know of anyway around this.
I can simply call a ten card 'T', which itried and is ok. I use some
code first to deal the cards, then your code to calculate values.
Thanks for your help.
Regards Robert

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
find and replace numeric strings in larger text strings Mr Molio Excel Worksheet Functions 8 November 9th 11 05:17 PM
How to find number of pairs of strings from list of strings? greg_overholt Excel Worksheet Functions 5 January 27th 06 10:42 PM
How can I count strings within strings Paul W Excel Worksheet Functions 4 June 14th 05 12:39 PM
Finding strings within strings Rod[_6_] Excel Programming 1 December 2nd 03 05:34 PM
Finding strings within strings Rod[_6_] Excel Programming 1 December 2nd 03 05:19 PM


All times are GMT +1. The time now is 07:37 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"