Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Excel function does not work when dragging 1

Status
Not open for further replies.

KellyK

Programmer
Mar 28, 2002
212
US
I created a function within Excel VBA called FindNext(). When I enter "=FindNext()" into each individual cell, I get the desired results. However, if I enter "=FindNext()" into one cell and then drag the formula down a column, the column is populated with the first correct result, all the way down. By that I mean, if FindNext() returns a "7" in row 1, I will get a column full of "7s". However, if I manually paste the formula into each row, I'll get "7", "25", "32", whatever. I put a "DoEvents" in my code thinking that it just needs time to refresh but this did not fix the problem. Does anybody know what could be the issue? My code is below:

Code:
Function FindNext() As String

If Cells(ActiveCell.Row, "N") <> Empty Then
FindNext = Cells(ActiveCell.Row, "N")
Exit Function
End If

r = ActiveCell.Row + 1

Do While Cells(r, "N").Value = Empty
r = r + 1
Loop

FindNext = Cells(r, "N").Value

DoEvents

End Function

Kelly
 
I should also mention that I have Automatic Calculation selected in my formula options and I tried pressing F9 after dragging the formula and it STILL does not put in the correct values. The correct values are there only when I enter the formula into each individual cell.

Kelly
 
Your function should have the cell address as parameter instead of using ActiveCell.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
If you need to refer in UDF to the cell where you call it, use Application.ThisCell or Application.Caller (versions older than xp).

combo
 



Kelly,

You need to rethink what you're trying to do.

Here are some observations:

Empty
VBA_Help said:
The Empty keyword is used as a Variant subtype. It indicates an uninitialized variable value.
If a cell contains a formula, it is NOT empty, so the first IF statement is unnecessary. In the other case, use the IsEmpty function. Either
Code:
Do While IsEmpty(Cells(r, "N"))
...
or
Code:
Do While Cells(r, "N").Value <> ""
...
But all this function seem to do, is return the VALUE of the first row in column N containing a VALUE. Is that the purpose?

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Thanks for the help, guys. I'm going to try the suggestions you've made and see where that gets me. Skip, believe it or not, yes, that is all I'm trying to do. It's kind of similar to populating the blank cells of a pivot table with the value in the cell immediately above, but an upside down version. Hard to explain but our I.T. folks were not able (willing?) to produce a report in the exact format our customer needs, so I wrote this function to clean it up.

Kelly
 



If you explain the requirement in more detail, there may be a better solution. An specific example would help.

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top