ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   strings (https://www.excelbanter.com/excel-programming/367001-strings.html)

RobcPettit[_2_]

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


Charlie

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



Tom Ogilvy

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



RobcPettit[_2_]

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




Charlie

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





RobcPettit[_2_]

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



All times are GMT +1. The time now is 02:02 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com