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 :-

[TABLE="class: grid, width: 500, align: left"]
<tbody>[TR]
[TD]Original Bank Name[/TD]
[TD]Acquiring Bank[/TD]
[TD]Acquired Status [/TD]
[TD]Final Bank Name[/TD]
[/TR]
[TR]
[TD]Bank of America Corp.[/TD]
[TD][/TD]
[TD]No[/TD]
[TD]Bank of America Corp.[/TD]
[/TR]
[TR]
[TD]JPMorgan Chase & Co.[/TD]
[TD][/TD]
[TD]No[/TD]
[TD]JPMorgan Chase & Co.[/TD]
[/TR]
[TR]
[TD]Bancshares, Inc.[/TD]
[TD]Bank of the Ozarks, Inc.[/TD]
[TD]Yes[/TD]
[TD]JP Morgan [/TD]
[/TR]
[TR]
[TD]First National Bank of Shelby[/TD]
[TD]Bank of the Ozarks, Inc.[/TD]
[TD]Yes[/TD]
[TD]JP Morgan [/TD]
[/TR]
[TR]
[TD]Summit Bancorp, Inc.[/TD]
[TD]Bank of the Ozarks, Inc.[/TD]
[TD]Yes[/TD]
[TD]JP Morgan [/TD]
[/TR]
[TR]
[TD]Bank of the Ozarks, Inc.[/TD]
[TD]JP Morgan [/TD]
[TD]Yes[/TD]
[TD]JP Morgan [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]


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

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
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.

[TABLE="width: 603"]
<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>[TR]
[TD="class: xl65, width: 188"]Original Bank Name[/TD]
[TD="class: xl65, width: 188"]Acquiring Bank [/TD]
[TD="class: xl65, width: 188"]Acquired [/TD]
[TD="class: xl65, width: 77"]Defunct [/TD]
[TD="class: xl65, width: 163"]Final Bank Name[/TD]
[/TR]
[TR]
[TD="class: xl65"]Bank of America Corp.[/TD]
[TD="class: xl65"] [/TD]
[TD="class: xl65"]No[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]Bank of America Corp.[/TD]
[/TR]
[TR]
[TD="class: xl65"]JPMorgan Chase & Co.[/TD]
[TD="class: xl65"] [/TD]
[TD="class: xl65"]No[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]JPMorgan Chase & Co.[/TD]
[/TR]
[TR]
[TD="class: xl65"]Gateway Bancorp[/TD]
[TD="class: xl65"]Banc of California, Inc.[/TD]
[TD="class: xl65"]No[/TD]
[TD="class: xl65"]Y[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]Bank of Sydney[/TD]
[TD="class: xl65"] [/TD]
[TD="class: xl65"]Yes[/TD]
[TD="class: xl65"]Y[/TD]
[TD="class: xl65"] [/TD]
[/TR]
[TR]
[TD="class: xl65"]Bancshares, Inc.[/TD]
[TD="class: xl65"]Bank of the Ozarks, Inc.[/TD]
[TD="class: xl65"]Yes[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]JP Morgan [/TD]
[/TR]
[TR]
[TD="class: xl65"]First National Bank of Shelby[/TD]
[TD="class: xl65"]Bank of the Ozarks, Inc.[/TD]
[TD="class: xl65"]Yes[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]JP Morgan [/TD]
[/TR]
[TR]
[TD="class: xl65"]Summit Bancorp, Inc.[/TD]
[TD="class: xl65"]Bank of the Ozarks, Inc.[/TD]
[TD="class: xl65"]Yes[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]JP Morgan [/TD]
[/TR]
[TR]
[TD="class: xl65"]Bank of the Ozarks, Inc.[/TD]
[TD="class: xl65"]JP Morgan [/TD]
[TD="class: xl65"]Yes[/TD]
[TD="class: xl65"]N[/TD]
[TD="class: xl65"]JP Morgan [/TD]
[/TR]
</tbody>[/TABLE]
****** id="cke_pastebin" style="position: absolute; top: 0px; width: 1px; height: 1px; overflow: hidden; left: -1000px;">
ABCD
Original Bank NameAcquiring BankAcquired StatusFinal 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 MorganYesJP Morgan

<tbody>
[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]1[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]2[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]3[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]4[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]5[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]6[/TD]

[TD="bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=CACACA]#CACACA[/URL] , align: center"]7[/TD]

</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,224,820
Messages
6,181,157
Members
453,021
Latest member
Justyna P

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