VBA Help - For Each Statement to Build a Formula based on Cell Values

Johnny Thunder

Well-known Member
Joined
Apr 9, 2010
Messages
693
Office Version
  1. 2016
Platform
  1. MacOS
Hello All,

I am working on a dropdown list that will create a formula thru vba to do a validation based on a dropdown list value true or false, true being the range needs to be added to my sum formula.

Here is the dropdown list below, just need help with writing code to do this kind of thing. Any help is appreciated.

Excel Workbook
EFG
1Periods to ValidateTrue or FalseRange
2JanuaryFALSED154
3FebruaryFALSEE154
4MarchFALSEF154
5AprilFALSEG154
6MayFALSEH154
7JuneFALSEI154
8JulyFALSEJ154
9AugustFALSEK154
10SeptemberFALSEL154
11OctoberTRUEM154
12NovemberTRUEN154
13DecemberTRUEO154
ValidateTBT



So what the code will need to do is

Loop thru each Month Jan - Dec and if the offset value =True then add the range value to a sum formula.

So with the example above my formula should look something like =Sum(M154,N154,O154) based on the months that have the value True.

this formula needs to be dynamic enough to know how many cell ranges to add based on what a user selects as true.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi Johnny Thunder,

Try this while on the sheet in question:

Code:
Option Explicit
Sub Macro1()

    Dim rngMyCell  As Range
    Dim lngLastRow As Long
    Dim strFormulaRange As String
    
    Application.ScreenUpdating = False
    
    lngLastRow = Cells(Rows.Count, "E").End(xlUp).Row
    
    For Each rngMyCell In Range("E2:E" & lngLastRow)
        If CStr(rngMyCell.Offset(0, 1)) = "True" Then
            If strFormulaRange = "" Then
                strFormulaRange = rngMyCell.Offset(0, 2)
            Else
                strFormulaRange = strFormulaRange & "," & rngMyCell.Offset(0, 2)
            End If
        End If
    Next rngMyCell
    
    'Puts the formula in cell H2. Change to suit.
    Range("H2").Formula = "=SUM(" & strFormulaRange & ")"
    
    Application.ScreenUpdating = True

End Sub

Regards,

Robert
 
Upvote 0
Try:

Code:
Sub ElaborateFormula()
 Dim f As String, m As Range
  For Each m In Range("F2:F13")
   If m.Value = True Then f = f & m.Offset(, 1).Value & ","
  Next m
 f = Left(f, Len(f) - 1)
 [K2] = "=SUM(" & f & ")"
End Sub
 
Upvote 0
thank you @Trebor76 for the help on this!

I did however use @Osvaldo Palmeiro version since it was a much more condensed variation of the code.

Osvaldo, I wanted to ask, with your line "SUM(" & f & ")" is there a way to add a string to the results?

Current Results: "SUM(M154,N154,O154)

Updated Results: "Sum('" & NameofSheet & "'!" & M154, '" & NameofSheet & "'!" & N154, '" & NameofSheet & "'!" & O154)

NameofSheet = Sheet Name defined by a variable.



 
Upvote 0
Hi, Johnny.
Is there a criteria to associate the NameofSheet to the cell address which is in column G ?
For example, assuming that May is True, then what NameofSheet would be associated to H154? Another case, if November is True, what NameofSheet would be associate to cell N154?

Maybe an easy way to associate the NameofSheet to the cell's address would be to place the names of the respective sheets right side to the cell's address, that means in column H. Is this feasible?
 
Upvote 0
Do you need code for this?

=MMULT(Sheet2!D154:O154, --(F2:F13))
 
Upvote 0
Osvaldo, similar to the loop you provided I have another drop down list that looks into a folder to validate if a specific file has been saved within the folder.

On my dropdown list, if the cell adjacent to the file name is blank it means that the file has not yet been saved, if the cell has a time stamp, it means the file has been saved down to my folder and to run your piece of code to get a sum of the months in questions.

Hopefully that explains what I am doing. So, the NameofSheet variable continues to change as the loop runs.

Hopefully there is a way to parse in the NameofSheet into what you have provided.

Hi, Johnny.
Is there a criteria to associate the NameofSheet to the cell address which is in column G ?
For example, assuming that May is True, then what NameofSheet would be associated to H154? Another case, if November is True, what NameofSheet would be associate to cell N154?

Maybe an easy way to associate the NameofSheet to the cell's address would be to place the names of the respective sheets right side to the cell's address, that means in column H. Is this feasible?
 
Upvote 0
If you want to add a sheet name try this.
Code:
Dim cl As Range
Dim arrRefs()
Dim cnt As Long
Dim strSheetName As String

    ReDim arrRefs(1 To 12)

    strSheetName = "Your Sheet Name"

    For Each cl In Range("F2:F13")
        If cl.Value = True Then
            cnt = cnt + 1
            arrRefs(cnt) = "'" & strSheetName & "'!" & cl.Offset(, 1).Value
        End If
    Next cl

    ReDim Preserve arrRefs(1 To cnt)

    Range("K2").Formula = "=SUM(" & Join(arrRefs, ",") & ")"
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,287
Members
452,631
Latest member
a_potato

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