1

I have to create a Dashboard, column C would have different derivative products with their maturities known as the short name. Example IRS July 18.

There are 20 family names, sometimes, column c could have the trade short name in lower or upper characters.

I have created a nested if Statement which in part does work, it only fails if a certain character is in upper or lower case. I therefore have to add this variation to the if statement, for it to work. Secondly, it is not picking up short names where the product is not listed.

Can I do this in such a way that it only looks for the characters and ignores upper and lowercase.

My last else statement, if a shortname is added with a new family, I am keen for it to be picked up. any thoughts how I can update this?

Instead of giving the full list I kept it very small

Example

If DerivativeID Like "*irs *" Then
    DerivativeFamily = "Interest Rate Swap "
ElseIf DerivativeID Like "*cds *" Then
    DerivativeFamily = "credit default swap "
Else
    DerivativeFamily = "Add to List"
End If
1
  • Convert the test value to lower case then compare to lower case: if lcase$(DerivativeID) Like "*cds *" Then ... Commented May 15, 2017 at 18:46

4 Answers 4

3

Sounds like a job for SELECT ... CASE. Try this:

DerivativeID = UCase(DerivativeID)

Select Case True
     Case DerivativeID like "*IRS*"
       DerivativeFamily = "Interest Rate Swap "
     Case DerivativeID like "*CDS*"
       DerivativeFamily = "Credit Default Swap "
     Case Else
       DerivativeFamily = "Add to List"
End Select
Sign up to request clarification or add additional context in comments.

3 Comments

Fixed both. Thanks! Keep forgetting the subtle differences between VB.NET and VBA. UCase and LIKE should work here instead of .ToUpper and .Contains.
With your approach, you would modify the original variable.
There was no indication of it being used anywhere else in the OP. I'm sure if the original string had to be used elsewhere in the program with its case intact, a new variable could be used for this bit.
2

Use just the LCase function:

If LCase$(DerivativeID) Like "*irs *" Then
    DerivativeFamily = "Interest Rate Swap "
ElseIf LCase$(DerivativeID) Like "*cds *" Then
    DerivativeFamily = "credit default swap "
Else
    DerivativeFamily = "Add to List"
End If

Comments

2

Specify Option Compare Text at the top of the module. The default string comparison mode is Binary, which makes "A" not equal to "a" in string comparisons.

When doing direct comparisons using the = operator, you can also use the StrComp function from the VBA.Strings module, to override the Option Compare setting:

Debug.Print StrComp("A", "A", vbBinaryCompare) 'prints 0
Debug.Print StrComp("A", "a", vbBinaryCompare) 'prints -1

Debug.Print StrComp("A", "A", vbTextCompare) 'prints 0
Debug.Print StrComp("A", "a", vbTextCompare) 'prints 0

As other answers suggest, you could also convert your string's casing. However in doing that you should be aware of a few things:

  • If comparing very large strings multiple times (e.g. in a loop), performance will be degraded
  • If using LCase or UCase, you're using a Variant-returning alias, which incurs additional implicit type conversion overhead which can easily be avoided by using their String-returning counterparts, LCase$ and UCase$.

2 Comments

Good Alternative, but this would changing the comparison behaviour for the whole module, which might be unintended.
@Holger correct. and in well-designed code, there's not 200 things going on in the same module either.
1

Try the LCase or UCase functions

Something like:

Set DerivativeID = LCase(DerivativeID)

if...Then

3 Comments

The Set keyword is used for assigning object references; a string isn't an object reference.
These are very good responses, but I want it to ignore the upper and lower cases, but focus more on the characters, as long as it matches the characters it should be successful. As I have created a counter so it loops through each of the cells from column c into the if statement and gives an output regarding the derivative family.
Everyone thank you so much for your comments, I just came across something which has solved my issue, Option Compare Text

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.