Some Help with a loop

ilya2004

Board Regular
Joined
Mar 17, 2011
Messages
135
Hi folks,

I have a loop tests to see if a cell matches a value within an array. The code is as follows:

Code:
 Function MultiCalc(MyArray As String, MyRange As Range, Cnt As Integer)
     Dim MyCell As Range
     For Each MyCell In MyRange
     MyText= MyCell.Text
     For n = 1 To Cnt
     If MyArray(n) = MyText Then
     MultiCalc = True ' "Next" here
     End If
    Next
 End Function

In order to save time, I would like the loop to quit as soon it finds a match so I would like to put a "next" where I have it marked in the comment. That is to say, "Once you have found a match, go on to the next cell". If I were to put a "next" there, I get the error "Next without For". Any ideas?

-Ilya
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
MultiCalc = True : Exit Function
 
Upvote 0
What is the function supposed to do? MyArray is not declared as an array in the signature, so it won't compile, and if you did, you couldn't pass a string array to a UDF.
 
Upvote 0
What is the function supposed to do? MyArray is not declared as an array in the signature, so it won't compile, and if you did, you couldn't pass a string array to a UDF.

Thanks, I'll try that.

In this case what you said should work, but what would happen if I have other code in the function that I would like to keep running, I only want to exit the loop?
 
Last edited:
Upvote 0
Oh, thanks for letting me know. Will this work as a module? I am just trying be able to test a bunch of cells if they equal any of the values in a defined array.
 
Upvote 0
Thanks, I'll try that.

In this case what you said should work, but what would happen if I have other code in the function that I would like to keep running, I only want to exit the loop?

Exit For

Did you check the help files?
 
Upvote 0
Oh, thanks for letting me know. Will this work as a module? I am just trying be able to test a bunch of cells if they equal any of the values in a defined array.
You should really quote something from the message you are answering (when there is more than one possibility) so we know for sure who your message is responding to.

Anyway, we can help you, but our problem in trying to do so is in knowing what you are actually trying to do or accomplish. My question is... what do you mean by "defined array"? Is the array you are trying to pass into this function really a String array? In other words, how is it declared in the code you are calling this function from and how did you populate that array?
 
Upvote 0

Forum statistics

Threads
1,224,524
Messages
6,179,308
Members
452,904
Latest member
CodeMasterX

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