How can i replace this "a" without application ran code?

VBE313

Well-known Member
Joined
Mar 22, 2019
Messages
686
Office Version
  1. 365
Platform
  1. Windows
How can i replace the bold part?


Function test1234()
Dim a As Long
If ActiveSheet.Name = "Sheet1" Then
Else
End If
a = Application.Caller.Row + 1
Do While Cells(a, 1).Value <> "Yes"
If Cells(a, 2) = "Yes" Then Exit Do
test1234 = test1234 + Val(Cells(a, 2).Value)
a = a + 1
Loop
End Function
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can you explaim exactly what it is that you are trying to do?

Typically, with Functions, you do not call things directly from ranges and sheets, you work off of variables arguments that part of a Function's inputs.
And with a function that you typically have a value that is ouput.

Sub Procedures are what you use to perform actions on ranges.
 
Upvote 0
Hi Joe4,

I am trying to calculate a bill of materials. For example... If it says "Yes" in column A, then calculate all of the ones below it until there is a "Yes" again. For column C, to get 19, the formula is "=IF(A1="No","",test1234()+B1)"

[TABLE="width: 192"]
<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>[TR]
[TD="width: 64"]Yes[/TD]
[TD="width: 64, align: right"]5[/TD]
[TD="width: 64, align: right"]19[/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]2[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Yes[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]24[/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Yes[/TD]
[TD="align: right"]9[/TD]
[TD="align: right"]29[/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Yes[/TD]
[TD="align: right"]5[/TD]
[TD="align: right"]20[/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]4[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]No[/TD]
[TD="align: right"]6[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Yes[/TD]
[TD]Yes[/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 
Upvote 0
OK, here is how I would do it.

So here is what my function look like (I documented it so you can see what each step is doing):
Code:
Function Test1234(critCell As Range, valCell As Range) As Double
'   critCell is the cell on the current row holding the Yes/No values
'   valCell is the cell on the crruent row holding the values

    Dim numYes As Long
    Dim i As Long
    Dim temp As Double
    
    Application.Volatile
    
    Do Until i = 5000   'exit if loop counter gets to 5000
'       Count number of Yes entries
        If critCell.Offset(i, 0) = "Yes" Then numYes = numYes + 1
'       Exit on second occurence of Yes
        If numYes = 2 Then Exit Do
'       Add to running sum
        temp = temp + valCell.Offset(i, 0)
'       Increment counter
        i = i + 1
    Loop
    
'   Set value to running sum
    Test1234 = temp

End Function
Then, I place this function is cell C1 and copy down for all rows:
=IF(A1="No","",Test1234(A1,B1))
 
Upvote 0
Hi mikerickson,

Thank you for that! I have to use this code for 45 columns, which code would be faster?
 
Upvote 0

Forum statistics

Threads
1,224,986
Messages
6,182,154
Members
453,093
Latest member
Soffy

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