Combining similar Private Sub Worksheet Codes (VBA)

ctbanker

New Member
Joined
Aug 26, 2015
Messages
26
Hi All,

I am creating a Finance Assessment template and am trying to capture Cash Inflow Streams. If I select "Cost Savings" in cell B2, the corresponding rows are viewable (image 1), but if I select "N/A" in cell B2, the corresponding rows are hidden (image 2) (and this is exactly what I want it to do for the other dropdowns in B2 through B9). By default, all dropdowns will be N/A and the user will select which streams they want to view in cells B2:B9.

I'm having troubles applying the logic I've applied for "Cost Savings" to the other Cash Flow Streams in one code. Can anyone help with this? Thanks in advance!

In addition to the images, I've included a Mini Sheet here:

Finance Assessment Template_Mr. Excel V2.xlsm
ABCDEFGH
1Cash Inflows StreamsSelect from Dropdown:
2Cost Savings ->Cost Savings
3Employee Efficiencies ->N/A
4Non-Interest Revenue ->N/A
5Incremental Loan/Deposit Balances ->N/A
6Member Growth ->N/A
7Salvage Value (Existing Asset) ->N/A
8Costs Avoided ->N/A
9Foregone Revenues & Cannibalization Impact ->N/A
10
112023202420252026
12Cost Savings Input
13Cost Savings Type$$$$
14Example$ 10,000$ 15,000$ 20,000$ 10,000
15[Input Cost Savings Type]$ -$ -$ -$ -
16[Input Cost Savings Type]$ -$ -$ -
17
18Employee Efficiency Input
19Employee Efficiency TypeExample[Input Employee Efficiency Type][Input Employee Efficiency Type][Input Employee Efficiency Type]
20Hourly Rate$ 78---
21Hours Saved100000
22$ Total$ 7,800$ -$ -$ -
23
24Non-Interest Revenue
25Non-Interest Revenue Type$$$$
26Example$ 10,000$ 15,000$ 20,000$ 10,000
27[Input Interest-Revenue Type]$ -$ -$ -$ -
28[Input Interest-Revenue Type]$ -$ -$ -
Sheet1
Cell Formulas
RangeFormula
C22:F22C22=C20*C21
Cells with Data Validation
CellAllowCriteria
B2ListCost Savings, N/A
B3ListEmployee Efficiencies, N/A
B4ListNon-Interest Revenue, N/A
B5ListIncremental Loan/Deposit Balances, N/A
B6ListMember Growth, N/A
B7ListSalvage Value, N/A
B8ListCosts Avoided, N/A
B9ListForegone Revenues & Cannibalization Impact, N/A


Here is the code:
VBA Code:
Private Sub Worksheet_Change_CostSavings(ByVal Target As Range)
Dim PayType As Range
Set PayType = Range("B2")
If Intersect(Target, PayType) Is Nothing Then Exit Sub

'add as many data sets as required
Dim Rng1 As Range

'add as many options as you require
Dim FindHdg1 As Range

'put your headings in the brackets & add more headings if required
Set FindHdg1 = Cells.Find("Cost Savings Input")

Dim RowsToHide As Range
Set RowsToHide = Range("A12:A16")
'add a case for each option in your drop-down & and add more if required
    Select Case PayType
        Case Is = "N/A"
            Cells.EntireRow.Hidden = False
            RowsToHide.EntireRow.Hidden = True
        
        Case Is = "Cost Savings"
            Cells.EntireRow.Hidden = False
            Set Rng1 = FindHdg1.CurrentRegion
            RowsToHide.EntireRow.Hidden = True
            Rng1.EntireRow.Hidden = False
        
    End Select

    
End Sub
 

Attachments

  • Image 1.PNG
    Image 1.PNG
    40 KB · Views: 8
  • Image 2.PNG
    Image 2.PNG
    50.5 KB · Views: 9
I like the table idea and something I considered - but this will need to be used in conjunction with Kevin's code right?
Actually, now that I think about it, the table method would only work with something similar to my original code - but we'd need a code for each table range (which is what I don't know how to accomplish)
 
Upvote 0

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Actually, now that I think about it, the table method would only work with something similar to my original code - but we'd need a code for each table range (which is what I don't know how to accomplish)
As the table method is not what I suggested, I'll assume this is addressed to @Jimmypop
 
Upvote 0
Hi there… @ctbanker

Ok I have played around a bit and came up with the following… Not sure if it would be useful but can always give it a go…

What I have done is create tables according to your different sections…

  1. Cost Savings Input
  2. Employee Efficiency Input
  3. Non-Interest Revenue Input
  4. Incremental Loan/Deposit Balances Input
  5. Member Growth Input
  6. Salvage Value (Existing Asset) Input
  7. Costs Avoided Input
  8. Foregone Revenues & Cannibalization Impact Input

I then played around a bit and came up with the code below which should hide the relevant tables depending on whether it is N/A or not…
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TriggerRanges(1 To 8) As Range
    Dim i As Integer
    Set TriggerRanges(1) = Range("B2")
    Set TriggerRanges(2) = Range("B3")
    Set TriggerRanges(3) = Range("B4")
    Set TriggerRanges(4) = Range("B5")
    Set TriggerRanges(5) = Range("B6")
    Set TriggerRanges(6) = Range("B7")
    Set TriggerRanges(7) = Range("B8")
    Set TriggerRanges(8) = Range("B9")
    For i = 1 To 8
        If Not Application.Intersect(TriggerRanges(i), Target) Is Nothing Then
            If TriggerRanges(i).Value = "N/A" Then
                Range(GetInputRange(i)).EntireRow.Hidden = True
            Else
                Range(GetInputRange(i)).EntireRow.Hidden = False
            End If
        End If
    Next i
End Sub
Function GetInputRange(ByVal index As Integer) As String
    Select Case index
        Case 1
            GetInputRange = "Cost_Savings_Input"
        Case 2
            GetInputRange = "Employee_Efficiency_Input"
        Case 3
            GetInputRange = "Non_Interest_Revenue_Input"
        Case 4
            GetInputRange = "Incremental_Loan_Deposit_Balances_Input"
        Case 5
            GetInputRange = "Member_Growth_Input"
        Case 6
            GetInputRange = "Salvage_Value_Existing_Asset_Input"
        Case 7
            GetInputRange = "Costs_Avoided_Input"
        Case 8
            GetInputRange = "Foregone_Revenues"
    End Select
End Function

You can now add or delete rows from the table without having to worry about updating the code every time in respect of referencing your ranges… I tested it and works on my side... Code was cobbled together from various others and some of my own projects I have currently...

I have added a link to the file below…

CTBanker Test1
 
Upvote 0
Solution
This worked perfectly
Hi there… @ctbanker

Ok I have played around a bit and came up with the following… Not sure if it would be useful but can always give it a go…

What I have done is create tables according to your different sections…

  1. Cost Savings Input
  2. Employee Efficiency Input
  3. Non-Interest Revenue Input
  4. Incremental Loan/Deposit Balances Input
  5. Member Growth Input
  6. Salvage Value (Existing Asset) Input
  7. Costs Avoided Input
  8. Foregone Revenues & Cannibalization Impact Input

I then played around a bit and came up with the code below which should hide the relevant tables depending on whether it is N/A or not…
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TriggerRanges(1 To 8) As Range
    Dim i As Integer
    Set TriggerRanges(1) = Range("B2")
    Set TriggerRanges(2) = Range("B3")
    Set TriggerRanges(3) = Range("B4")
    Set TriggerRanges(4) = Range("B5")
    Set TriggerRanges(5) = Range("B6")
    Set TriggerRanges(6) = Range("B7")
    Set TriggerRanges(7) = Range("B8")
    Set TriggerRanges(8) = Range("B9")
    For i = 1 To 8
        If Not Application.Intersect(TriggerRanges(i), Target) Is Nothing Then
            If TriggerRanges(i).Value = "N/A" Then
                Range(GetInputRange(i)).EntireRow.Hidden = True
            Else
                Range(GetInputRange(i)).EntireRow.Hidden = False
            End If
        End If
    Next i
End Sub
Function GetInputRange(ByVal index As Integer) As String
    Select Case index
        Case 1
            GetInputRange = "Cost_Savings_Input"
        Case 2
            GetInputRange = "Employee_Efficiency_Input"
        Case 3
            GetInputRange = "Non_Interest_Revenue_Input"
        Case 4
            GetInputRange = "Incremental_Loan_Deposit_Balances_Input"
        Case 5
            GetInputRange = "Member_Growth_Input"
        Case 6
            GetInputRange = "Salvage_Value_Existing_Asset_Input"
        Case 7
            GetInputRange = "Costs_Avoided_Input"
        Case 8
            GetInputRange = "Foregone_Revenues"
    End Select
End Function

You can now add or delete rows from the table without having to worry about updating the code every time in respect of referencing your ranges… I tested it and works on my side... Code was cobbled together from various others and some of my own projects I have currently...

I have added a link to the file below…

CTBanker Test1
This worked
Hi there… @ctbanker

Ok I have played around a bit and came up with the following… Not sure if it would be useful but can always give it a go…

What I have done is create tables according to your different sections…

  1. Cost Savings Input
  2. Employee Efficiency Input
  3. Non-Interest Revenue Input
  4. Incremental Loan/Deposit Balances Input
  5. Member Growth Input
  6. Salvage Value (Existing Asset) Input
  7. Costs Avoided Input
  8. Foregone Revenues & Cannibalization Impact Input

I then played around a bit and came up with the code below which should hide the relevant tables depending on whether it is N/A or not…
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim TriggerRanges(1 To 8) As Range
    Dim i As Integer
    Set TriggerRanges(1) = Range("B2")
    Set TriggerRanges(2) = Range("B3")
    Set TriggerRanges(3) = Range("B4")
    Set TriggerRanges(4) = Range("B5")
    Set TriggerRanges(5) = Range("B6")
    Set TriggerRanges(6) = Range("B7")
    Set TriggerRanges(7) = Range("B8")
    Set TriggerRanges(8) = Range("B9")
    For i = 1 To 8
        If Not Application.Intersect(TriggerRanges(i), Target) Is Nothing Then
            If TriggerRanges(i).Value = "N/A" Then
                Range(GetInputRange(i)).EntireRow.Hidden = True
            Else
                Range(GetInputRange(i)).EntireRow.Hidden = False
            End If
        End If
    Next i
End Sub
Function GetInputRange(ByVal index As Integer) As String
    Select Case index
        Case 1
            GetInputRange = "Cost_Savings_Input"
        Case 2
            GetInputRange = "Employee_Efficiency_Input"
        Case 3
            GetInputRange = "Non_Interest_Revenue_Input"
        Case 4
            GetInputRange = "Incremental_Loan_Deposit_Balances_Input"
        Case 5
            GetInputRange = "Member_Growth_Input"
        Case 6
            GetInputRange = "Salvage_Value_Existing_Asset_Input"
        Case 7
            GetInputRange = "Costs_Avoided_Input"
        Case 8
            GetInputRange = "Foregone_Revenues"
    End Select
End Function

You can now add or delete rows from the table without having to worry about updating the code every time in respect of referencing your ranges… I tested it and works on my side... Code was cobbled together from various others and some of my own projects I have currently...

I have added a link to the file below…

CTBanker Test1
This worked perfectly @Jimmypop! Thank you so much!
 
Upvote 0
Hi @Jimmypop , I have a follow-up question - how would we modify the formula if we wanted the macro to apply to locked/unlocked cells? The cells highlighted in manila with blue text are the unlocked input cells, while everything else should be locked.

Finance Assessment Template_as of 6.20.23 V3.xlsm
ABCDEFGHIJKL
1Cash Outflow StreamsSelect from Dropdown:Notes:
2Capitalized Expenses (CapEx) àCapExProject will incur Capitalized Expense 
3Operating Expenses (OpEx) àOpExProject will incur Operating Expense 
4Post Project Operating Expenses (OpEx) àPost Project OpExAs result of the project BECU will incur incremental OpEx after the project concludes 
6DirectDirect - Post Project OpEx 
7IndirectIndirect - Post Project OpEx 
8Contingent / Consultant Costs àContingent / Consultant CostsContingent and Consultant Costs: The project will require contingent and/or consultant resources 
9Internal Labor Costs àInternal Labor CostsThe project will require internal employee resources or dedicated time from existing and/or new employees to implement and/or upkeep 
11Based on Hours EntryBased on Hours Entry 
12Estimated SalaryEstimated Salary 
13Open EntryOpen Entry 
14
15Incremental Cash Outflows
16
18Investment Cash Flows (Capatalized)Cash Flow Characteristics2023 - Q12023 - Q22023 - Q32023 - Q42024202520262027CapEx Categorization
19Cash Flow TypesDivision/Project Booking Expense
20Cost of the asset/equipment/software developmentProject$ 1,000$ 2,000$ 3,000$ 4,000$ 5,000$ 6,000$ 7,000$ 8,000CapEx
21Set-up expenditures - shipping, installation, etc.IT$ 20,000$ -$ -CapEx
22Continued investment in asset over years, if anyLGRC$ 1$ 1$ -$ -$ -CapEx
23Sales tax on purchases$ -$ -$ -$ -CapEx
24Workforce Env access to member Env HW - PAW [EXAMPLE]$ -$ -$ -$ -CapEx
25Citrix Deployment by LK Method [EXAMPLE]$ -$ -$ -$ -CapEx
26[Project specific capitalized expense]$ -$ -$ -$ -CapEx
27[Project specific capitalized expense]$ -$ -$ -$ -CapEx
28
29Total - Investment Cash Flows (Capatalized)$ 21,001$ 2,001$ 3,000$ 4,000$ 5,000$ 6,000$ 7,000$ 8,000
30
32Operating Cash Flows (Expensed during Project Implementation)Cash Flow Characteristics2023 - Q12023 - Q22023 - Q32023 - Q42024202520262027OpEx Categorization
33Cash Flow TypesDivision/Project Booking Expense
34Expenses funded by department OpEx budgets Project$ 2,000$ -$ -$ -$ -$ -$ -$ - OpEx
35Legal expensesOperations$ -$ 4,000$ -$ -$ -$ -$ -$ - OpEx
36Fraud prevention expensesMarketing/Co-Op Affairs$ -$ -$ 6,000$ -$ -$ -$ -$ - OpEx
37Hiring/recruiting expenses$ -$ -$ -$ 8,000$ -$ -$ -$ - OpEx
38Data management costs$ -$ -$ -$ -$ 10,000$ -$ -$ - OpEx
39Incremental transaction-related expenses$ -$ -$ -$ -$ -$ 12,000$ -$ - OpEx
40M365 license upgrade (true up) [EXAMPLE]$ -$ -$ -$ -$ -$ -$ 14,000$ - OpEx
41Fiserv vendor support [EXAMPLE]$ -$ -$ -$ -$ -$ -$ -$ 16,000 OpEx
42[Project specific operating expense]$ -$ -$ -$ -$ -$ -$ -$ - OpEx
43[Project specific operating expense]$ -$ -$ -$ -$ -$ -$ -$ - OpEx
44
45Total - Operating Cash Flows$ 2,000$ 4,000$ 6,000$ 8,000$ 10,000$ 12,000$ 14,000$ 16,000
B.CashOutflows
Cell Formulas
RangeFormula
F2F2=IF(AND(B2="N/A",SUM($C$20:$J$29)>0),"← REVIEW: There are inputs in CapEx Outflow Stream","")
F3F3=IF(AND(B3="N/A",SUM($C$34:$J$45)>0),"← REVIEW: There are inputs in OpEx Outflow Stream","")
F4F4=IF(AND(B4="N/A",OR(SUM($C$50:$J$63)>0,SUM($C$68:$J$74)>0)),"← REVIEW: There are inputs in OpEx Outflow Stream","")
F6F6=IF(AND(B6="N/A",SUM($C$50:$J$63)>0),"← REVIEW: There are inputs in Direct - Post Project OpEx Outflow Stream","")
F7F7=IF(AND(B7="N/A",SUM($C$68:$J$74)>0),"← REVIEW: There are inputs in Direct - Post Project OpEx Outflow Stream","")
F8F8=IF(AND(B8="N/A",SUM($C$79:$J$84)>0),"← REVIEW: There are inputs in Contingent/Consultant Outflow Stream","")
F9F9=IF(AND(B9="N/A",OR(SUM($C$91:$J$100)>0,SUM($C$105:$J$109)>0,SUM($C$115:$J$119)>0)),"← REVIEW: There are inputs in Internal Labor Costs Outflow Stream","")
F11F11=IF(AND(B11="N/A",SUM($C$91:$J$100)>0),"← REVIEW: There are inputs in Based on Hours Entry Outflow Stream","")
F12F12=IF(AND(B12="N/A",SUM($C$105:$J$109)>0),"← REVIEW: There are inputs in Based on Estimated Salary Outflow Stream","")
F13F13=IF(AND(B13="N/A",SUM($C$115:$J$119)>0),"← REVIEW: There are inputs in Open Entry Outflow Stream","")
C18,C32C18=YEAR(A.ProjectSummary!$C$4)&" - Q1"
D18,D32D18=YEAR(A.ProjectSummary!$C$4)&" - Q2"
E18,E32E18=YEAR(A.ProjectSummary!$C$4)&" - Q3"
F18,F32F18=YEAR(A.ProjectSummary!$C$4)&" - Q4"
G18,G32G18=YEAR(A.ProjectSummary!$C$4)+1
H18:J18,H32:J32H18=G18+1
C29:J29C29=SUM(C20:C27)
C45:J45C45=SUM(C34:C43)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
B3Expression=AND($B$3="N/A",SUM($C$34:$J$45)>0)textNO
B9Expression=AND(B9="N/A",OR(SUM($C$91:$J$100)>0,SUM($C$105:$J$109)>0,SUM($C$115:$J$119)>0))textNO
B13Expression=AND($B$13="N/A",SUM($C$115:$J$119)>0)textNO
B12Expression=AND($B$12="N/A",SUM($C$105:$J$109)>0)textNO
B4Expression=AND($B$4="N/A",OR(SUM($C$50:$J$63)>0),SUM($C$68:$J$74)>0)textNO
B6Expression=AND($B$6="N/A",SUM($C$50:$J$63)>0)textNO
B11Expression=AND($B$11="N/A",SUM($C$91:$J$100)>0)textNO
B7Expression=AND($B$7="N/A",SUM($C$68:$J$74)>0)textNO
B2Expression=AND($B$2="N/A",SUM($C$20:$J$29)>0)textNO
B8Expression=AND($B$8="N/A",SUM($C$79:$J$84)>0)textNO
Cells with Data Validation
CellAllowCriteria
B11ListBased on Hours Entry, N/A
B12ListEstimated Salary, N/A
B13ListOpen Entry, N/A
B2ListCapEx, N/A
B3ListOpEx, N/A
B4:B5ListPost Project OpEx, N/A
B6ListDirect - Post Project OpEx, N/A
B7ListIndirect - Post Project OpEx, N/A
B8ListContingent / Consultant Costs, N/A
B9ListInternal Labor Costs, N/A
L34:L43ListOpEx
L20:L27ListCapEx
B20:B27List=DropDownControl!$A$2:$A$10
B34:B43List=DropDownControl!$A$2:$A$10
 
Upvote 0
Hi there

I would suggest that the best would be to create a new thread with your question... This way is someone has similar question it is easier to find...will have a think in the meantime.
 
Upvote 0

Forum statistics

Threads
1,223,900
Messages
6,175,276
Members
452,629
Latest member
SahilPolekar

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