Using a CASE than If

rollingzep

Board Regular
Joined
Nov 18, 2013
Messages
214
Office Version
  1. 365
Platform
  1. Windows
Hi,

I need to code for a certain condition like below

If Col AZ is Banks or Local, AND Col AK is “AAA”, “AA+”, “AA”, or “AA-”, then populate the Col BJ as “1”
If Col AZ is Banks or Local, AND Col AK is “A+”, “A”, or “A-”, then populate the Col C as “2”
If Col AZ is Banks or Local, AND Col AK is “BBB+”, “BBB”, “BBB-”, “BB+”, “BB”, “BB-”, “B+”, “B”, and “B-”, then populate the Col BJ as “3”
If Col AZ is Banks or Local, AND Col AK is “CCC+”, “CCC”, “CCC-”, “CC”, “C”, and “D”, then populate the Col BJ as “4”
If Col AZ is Banks or Local, AND Col AK is not rated, then populate the Col BJ as “5”
If Col AZ is Institutions or Corps, AND Col AK is “AAA”, “AA+”, “AA”, or “AA-”, then populate the Col BJ as “6”
If Col AZ is Institutions or Corps, AND Col AK is “A+”, “A”, “A-”,“A+”, “A”, “A-”, “BBB+”, “BBB”, “BBB-”, “BB+”, “BB”, “BB-”, “B+”, “B”, “B-”, “CCC+”, “CCC”, “CCC-”, “CC”, “C”, “D”, or unrated then populate the Col BJ as “7”

I use the

LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If (ws.Range("AZ" & i).Value = "Banks" Or ws.Range("AZ" & i).Value = "Local") AND (ws.Range("AK" & i).Value = "AAA" Or ws.Range("AK" & i).Value = "AA+") Then
ws.Range("BJ" & i).Value = "1"
.................................
..............................
End If

Next i

How to use the CASE statement rather than IF? Will that make this faster?
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
You can nest them like
VBA Code:
For i = 2 To Lastrow
   Select Case Ws.Range("AZ" & i).Value
      Case "Banks", "Local"
         Select Case Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 1
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
      Case "Institutions", "Corps"
         Select Case Ws.Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 6
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
   End Select
Next i
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
First off, I apologize to the Moderators (Fluff) for starting a different thread.
I was unaware of the rules!

The NEST is not returning any values.

This is my code
VBA Code:
For i = 2 To Lastrow
   Select Case Ws.Range("AZ" & i).Value
      Case "Banks", "Local"
         Select Case Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 1
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
      Case "Institutions", "Corps"
         Select Case Ws.Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 6
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
   End Select
Next i

Two questions

How/where to add CASE Else and If Col AZ is Banks or Local, AND Col AK is not rated, then populate the Col BJ as “5”
If col AK is Null, how do I include that condition.
 
Upvote 0
For Banks & Local you just ned to add the other Case lines in the same manner as I showed.
For Institutions or Corps, you can change the 2nd Case like
VBA Code:
For i = 2 To Lastrow
   Select Case Ws.Range("AZ" & i).Value
      Case "Banks", "Local"
         Select Case Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 1
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
      Case "Institutions", "Corps"
         Select Case Ws.Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 6
            Case Else
               Ws.Range("BJ" & i).Value = 7
         End Select
   End Select
Next i
 
Upvote 0
Solution
For Banks & Local you just ned to add the other Case lines in the same manner as I showed.
For Institutions or Corps, you can change the 2nd Case like
VBA Code:
For i = 2 To Lastrow
   Select Case Ws.Range("AZ" & i).Value
      Case "Banks", "Local"
         Select Case Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 1
            Case "A+", "A", "A-"
               Ws.Range("BJ" & i).Value = 2
         End Select
      Case "Institutions", "Corps"
         Select Case Ws.Range("AK" & i)
            Case "AAA", "AA+", "AA", "AA-"
               Ws.Range("BJ" & i).Value = 6
            Case Else
               Ws.Range("BJ" & i).Value = 7
         End Select
   End Select
Next i
I modified as

VBA Code:
    LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
    
    For i = 2 To LastRow
    Select Case ws.Range("AZ" & i).Value
       Case "Commercial Banks", "State and Local Gov. Agency"
          Select Case ws.Range("AK" & i).Value
             Case "AAA", "AA+", "AA", "AA-"
                ws.Range("BJ" & i).Value = 1
             Case "A+", "A", "A-"
                ws.Range("BJ" & i).Value = 2
             Case "BBB+", "BBB", "BBB-", "BB+", "BB", "BB-", "B+", "B", "B-"
                ws.Range("BJ" & i).Value = 3
             Case "CCC+", "CCC", "CCC-", "CC", "C", "D"
                ws.Range("BJ" & i).Value = 4
            Case Else
               ws.Range("BJ" & i).Value = 5  'if not rated, then populate the “RWA – Counterparty” column as “5”
          End Select
       Case "Financial Institution", "Brokers", "Corporations"
          Select Case ws.Range("AK" & i).Value
             Case "AAA", "AA+", "AA", "AA-"
                ws.Range("BJ" & i).Value = 6
             Case Else
                ws.Range("BJ" & i).Value = 7    'if “A+”, “A”, “A-”,“A+”, “A”, “A-”, “BBB+”, “BBB”, “BBB-”, “BB+”, “BB”, “BB-”, “B+”, “B”, “B-”, “CCC+”, “CCC”, “CCC-”, “CC”, “C”, “D”, or unrated
          End Select
    End Select
    Next i

Only values for 5 and 7 are populated and not for other conditions.
 
Upvote 0
Does col AK have the exact text like "AAA" or "AA+" on it's own in the cell?
 
Upvote 0
Does col AK have the exact text like "AAA" or "AA+" on it's own in the cell?
yes

1668626919670.png
 
Upvote 0
Ok, check that they don't have any leading/trailing spaces.
 
Upvote 0

Forum statistics

Threads
1,215,046
Messages
6,122,852
Members
449,096
Latest member
Erald

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
Back
Top