How to use continuous If else loop in Excel with

tusharsharma24

New Member
Joined
Sep 3, 2018
Messages
4
I have a data set with 4 columns - Original Bank , Acquiring Bank, Acquired Status ( Yes/ No) , Final Bank Name

Depending on whether the acquired status is yes or no, I want to copy the bank name to column Final Bank Name. Following rules apply :-

a) If the bank under column Original bank has been acquired, the acquiring bank name will be in second column and this name will be copied to Final bank name

b) If the bank under Original bank has not been acquired, then Final bank name will have name from Original Bank name

The complication is that a particular bank can occur in both Original bank name list and acquiring bank name list. Here is a sample table below :-

Original Bank NameAcquiring BankAcquired Status Final Bank Name
Bank of America Corp.NoBank of America Corp.
JPMorgan Chase & Co.NoJPMorgan Chase & Co.
Bancshares, Inc.Bank of the Ozarks, Inc.YesJP Morgan
First National Bank of ShelbyBank of the Ozarks, Inc.YesJP Morgan
Summit Bancorp, Inc.Bank of the Ozarks, Inc.YesJP Morgan
Bank of the Ozarks, Inc.JP Morgan YesJP Morgan

<tbody>
</tbody>


The column Final Bank name needs to show the name of final parent. How can I do this in excel ? Does it need VBA formula ? Can anyone help with this.
 
Last edited by a moderator:

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
You could try this user-defined function. To implement ..
1. Right click the sheet name tab and choose "View Code".
2. In the Visual Basic window use the menu to Insert|Module
3. Copy and Paste the code below into the main right hand pane that opens at step 2.
4. Close the Visual Basic window.
5. Enter the formula as shown in the screen shot below and copy down.
6. Your workbook will need to be saved as a macro-enabled workbook (*.xlsm)

Code:
Function FinalBank(rData As Range, sOriginalBank As String) As String
  Dim d As Object
  Dim aData As Variant
  Dim i As Long
  Dim sNewBank As String
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  aData = rData.Value
  For i = 1 To UBound(aData)
    d(aData(i, 1)) = IIf(IsEmpty(aData(i, 2)), aData(i, 1), aData(i, 2))
  Next i
  sNewBank = sOriginalBank
    Do While d.exists(sNewBank) And d(sNewBank) <> sNewBank
      sNewBank = d(sNewBank)
    Loop
  FinalBank = sNewBank
End Function

Excel Workbook
ABCD
1Original Bank NameAcquiring BankAcquired StatusFinal Bank Name
2Bank of America Corp.NoBank of America Corp.
3JPMorgan Chase & Co.NoJPMorgan Chase & Co.
4Bancshares, Inc.Bank of the Ozarks, Inc.YesJP Morgan
5First National Bank of ShelbyBank of the Ozarks, Inc.YesJP Morgan
6Summit Bancorp, Inc.Bank of the Ozarks, Inc.YesJP Morgan
7Bank of the Ozarks, Inc.JP MorganYesJP Morgan
Sheet1
 
Upvote 0
Peter,

Thanks a ton. This is working.

One more thing - If I had a column " Defunct ( Y/ N )" in my data and I wanted Final Bank Name to have the Final bank name value only if Column Defunct had value "N". How can we Put in this check in our current Macro ? I am providing a sample output table below to explain what I mean.

Original Bank NameAcquiring Bank Acquired Defunct Final Bank Name
Bank of America Corp. NoNBank of America Corp.
JPMorgan Chase & Co. NoNJPMorgan Chase & Co.
Gateway BancorpBanc of California, Inc.NoY
Bank of Sydney YesY
Bancshares, Inc.Bank of the Ozarks, Inc.YesNJP Morgan
First National Bank of ShelbyBank of the Ozarks, Inc.YesNJP Morgan
Summit Bancorp, Inc.Bank of the Ozarks, Inc.YesNJP Morgan
Bank of the Ozarks, Inc.JP Morgan YesNJP Morgan

<colgroup><col width="188" span="3" style="mso-width-source:userset;mso-width-alt:6875; width:141pt"> <col width="77" style="mso-width-source:userset;mso-width-alt:2816;width:58pt"> <col width="163" style="mso-width-source:userset;mso-width-alt:5961;width:122pt"> </colgroup><tbody>
</tbody>
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
ABCD
1Original Bank NameAcquiring BankAcquired StatusFinal Bank Name
2Bank of America Corp.NoBank of America Corp.
3JPMorgan Chase & Co.NoJPMorgan Chase & Co.
4Bancshares, Inc.Bank of the Ozarks, Inc.YesJP Morgan
5First National Bank of ShelbyBank of the Ozarks, Inc.YesJP Morgan
6Summit Bancorp, Inc.Bank of the Ozarks, Inc.YesJP Morgan
7Bank of the Ozarks, Inc.JP MorganYesJP Morgan

<tbody>
</tbody>
</body>
 
Upvote 0
If I had a column " Defunct ( Y/ N )" in my data and I wanted Final Bank Name to have the Final bank name value only if Column Defunct had value "N". How can we Put in this check in our current Macro ?
1. Add the extra code as shown in blue
Rich (BB code):
Function FinalBank(rData As Range, sOriginalBank As String, sStatus As String) As String
  Dim d As Object
  Dim aData As Variant
  Dim i As Long
  Dim sNewBank As String
  
  Set d = CreateObject("Scripting.Dictionary")
  d.CompareMode = 1
  aData = rData.Value
  If sStatus = "N" Then
    For i = 1 To UBound(aData)
      d(aData(i, 1)) = IIf(IsEmpty(aData(i, 2)), aData(i, 1), aData(i, 2))
    Next i
    sNewBank = sOriginalBank
      Do While d.exists(sNewBank) And d(sNewBank) <> sNewBank
        sNewBank = d(sNewBank)
      Loop
    FinalBank = sNewBank
  End If
End Function

2. Include the extra parameter when you use the function in the worksheet as shown

Excel Workbook
ABCDE
1Original Bank NameAcquiring BankAcquiredDefunctFinal Bank Name
2Bank of America Corp.NoNBank of America Corp.
3JPMorgan Chase & Co.NoNJPMorgan Chase & Co.
4Gateway BancorpBanc of California, Inc.NoY
5Bank of SydneyYesY
6Bancshares, Inc.Bank of the Ozarks, Inc.YesNJP Morgan
7First National Bank of ShelbyBank of the Ozarks, Inc.YesNJP Morgan
8Summit Bancorp, Inc.Bank of the Ozarks, Inc.YesNJP Morgan
9Bank of the Ozarks, Inc.JP MorganYesNJP Morgan
Sheet2



An alternative would have been to keep the UDF exactly as it was in post 2 and use this formula in the worksheet:
=IF(D2="N",FinalBank(A$2:B$9,A2),"")



I don't understand rows 4 & 5 though. Although they don't matter for this example since the rows are both marked as 'Defunct', how can ..
- in row 4, column C show 'No' to Acquired when there is an Acquiring Bank listed in column B?
- in row 5, column C show 'Yes' to Acquired when there is not an Acquiring Bank shown in column B?
 
Last edited:
Upvote 0
Thanks Peter. I was using the workaround suggested in the second option by you. But the extra piece of code in the macro itself is certainly much efficient.

Appreciate all the help.
 
Upvote 0

Forum statistics

Threads
1,214,875
Messages
6,122,044
Members
449,063
Latest member
ak94

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