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

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
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)"

Yes519
No4
No3
No5
No2
Yes524
No4
No3
No5
No7
Yes929
No4
No6
No4
No6
Yes520
No4
No5
No6
YesYes

<colgroup><col width="64" span="3" style="width:48pt"> </colgroup><tbody>
</tbody>
 
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
You are welcome!
 
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,213,544
Messages
6,114,239
Members
448,555
Latest member
RobertJones1986

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