ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Wildcards in a case statement (https://www.excelbanter.com/excel-programming/432854-wildcards-case-statement.html)

DG

Wildcards in a case statement
 
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan



smartin

Wildcards in a case statement
 
DG wrote:
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan



You can do it like this:

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case Else
If Cells(x,9) Like "C*" Then
Rejected = True
End If
End Select

Of course, if you are only testing two conditions you don't really need
Select Case.

Peter T

Wildcards in a case statement
 
In theory you could do something like this

Select case True
Case Left$(Cells(x, 9), 1) = "C"
' other Case's must evaluate to a boolean


However for your two conditions simply use If...ElseIf

s = Ucase(Cells(x,9)) ' assuming case insensitive
If s = "BQ" then

Elseif Left$(s,1) + "C" then

ElseIf blah then

End If

Regards,
Peter T


"DG" wrote in message
...
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan




Peter T

Wildcards in a case statement
 
typo!!

Elseif Left$(s,1) + "C" then


should of course read

Elseif Left$(s,1) = "C" then


Peter T


"Peter T" <peter_t@discussions wrote in message
...
In theory you could do something like this

Select case True
Case Left$(Cells(x, 9), 1) = "C"
' other Case's must evaluate to a boolean


However for your two conditions simply use If...ElseIf

s = Ucase(Cells(x,9)) ' assuming case insensitive
If s = "BQ" then

Elseif Left$(s,1) + "C" then

ElseIf blah then

End If

Regards,
Peter T


"DG" wrote in message
...
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan






Don Guillett

Wildcards in a case statement
 
try this. change activecell to cells(x,9)

Sub iflefttext()
If UCase(Left(ActiveCell, 1)) = "C" Then
rejected = True
ElseIf UCase(ActiveCell) = "BQ" Then
rejected = False
End If
MsgBox rejected
End Sub

--
Don Guillett
Microsoft MVP Excel
SalesAid Software

"DG" wrote in message
...
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan



Dave Peterson

Wildcards in a case statement
 
I'm nost sure what etc means here, but you may want to be explicit:

Dim x As Long
Dim Rejected As Boolean

x = 1
With ActiveSheet
Select Case UCase(.Cells(x, 9).Value)
'always use upper case here!
Case "BQ"
Rejected = False
Case "CA", "CD", "CQ", "C0" To "C5"
Rejected = True
End Select
End With

DG wrote:

I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan


--

Dave Peterson

DG

Wildcards in a case statement
 
Thanks, I knew I could find it here.


"DG" wrote in message
...
I have a column that contains a 2 character code. How can I use only one
case statement to be true for all cases where the cell started with a C?
For example the cell could contain CA, CD, CQ, C5 etc.

Select Case Cells(x,9)
Case "BQ"
Rejected = False
Case "C*"
Rejected = True
End Select

Dan





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

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