loop next again

rplim2016

Board Regular
Joined
Jun 28, 2016
Messages
76
hello mr. excel users,

just trying to create a simple add function but below code is missing

Public Function myAdd(num1 As Range) As Long
Dim temp_total As Range
Dim final_total As Long

For Each temp_total In num1
If IsNumeric(temp_total.Value) = False Then ((("MISSING CODE"))
GoTo nextloop
End If

final_total = final_total + temp_total.Value
Next temp_total

myAdd = final_total


End Function

above red font, should do Next temp_total, how could it work?



thanks!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Try:
Code:
Public Function myAdd(num1 As Range) As Long
    Dim temp_total As Range
    Dim final_total As Long
    
    For Each temp_total In num1
        If IsNumeric(temp_total.Value) Then
            final_total = final_total + temp_total.Value
        End If
    Next temp_total
    myAdd = final_total
End Function
 
Upvote 0
You have made your function much more complicated than necessary. Here is a simpler looping version (note I have changed the function's argument and variable names to something more self descriptive than the names you used (note how the For Each statement reads like a meaningful sentence now)...
Code:
Public Function myAdd(RangeToAdd As Range) As Long
  Dim Cell As Range
  For Each Cell In RangeToAdd
    myAdd = myAdd + Val(Cell.Value)
  Next
End Function
Note that the Val function returns the leading number from text values that start with a numeric value (as well as the actual number for real numeric values)... if the text does not start with a number, Val returns 0; hence, no separate test is needed (so long as your text values will never start with a number; for example 3M for which Val would return 3).
 
Last edited:
Upvote 0
And, just so you know, your function can be done without a loop using just a single line of code...
Code:
Public Function myAdd(RangeToAdd As Range) As Long
  myAdd = WorksheetFunction.Sum(RangeToAdd)
End Function
 
Upvote 0
Thanks it works! :)

The Sum function already exists, but I guess you're trying to learn how to cycle a function and perform an operation. Good job. That's how you continue to learn.

I'm glad to help you. Thanks for the feedback.
 
Upvote 0
And, just so you know, your function can be done without a loop using just a single line of code...
Code:
Public Function myAdd(RangeToAdd As Range) As Long
  myAdd = WorksheetFunction.Sum(RangeToAdd)
End Function


Hey Rick, thanks for making it easy, I can do so much UDF now with that Worksheetfunction code. cheers.
 
Upvote 0
You have made your function much more complicated than necessary. Here is a simpler looping version (note I have changed the function's argument and variable names to something more self descriptive than the names you used (note how the For Each statement reads like a meaningful sentence now)...
Code:
Public Function myAdd(RangeToAdd As Range) As Long
  Dim Cell As Range
  For Each Cell In RangeToAdd
    myAdd = myAdd + Val(Cell.Value)
  Next
End Function
Note that the Val function returns the leading number from text values that start with a numeric value (as well as the actual number for real numeric values)... if the text does not start with a number, Val returns 0; hence, no separate test is needed (so long as your text values will never start with a number; for example 3M for which Val would return 3).


i appreciate your effort explaining this to me. I just replicate the existing UDF that I already have but i never thought that there is an easy and less complicated way to make UDF.


By any chance, do you know how to reiterate a Loop, For, Do? without doing adding goto function "Handler:"

sample

For i = 1 to 10
if activecell.value = "" then *code is to restart the next loop*

Code continues

Next i
 
Upvote 0

Forum statistics

Threads
1,214,652
Messages
6,120,746
Members
448,989
Latest member
mariah3

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