View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bill Renaud Bill Renaud is offline
external usenet poster
 
Posts: 417
Default Locating end of used range- not returning expected value

You have not specified all of the arguments to the Find method, which might
be causing some of the problem. This can be very troublesome, as a macro
will work for a while, then quit working when a user uses the Find feature
from the user interface and changes some of the options.

From the topic "Find Method" in Visual Basic Help:

"The settings for LookIn, LookAt, SearchOrder, and MatchByte are saved each
time you use this method. If you don’t specify values for these arguments
the next time you call the method, the saved values are used. Setting these
arguments changes the settings in the Find dialog box, and changing the
settings in the Find dialog box changes the saved values that are used if
you omit the arguments. To avoid problems, set these arguments explicitly
each time you use this method."

If you insist on using the Find method in VBA code, I would ALWAYS set ALL
of the above mentioned 4 arguments!

In this situation with variable length blocks of data, I generally use the
UsedRange property and then use code something like the following. (Note
that the .Row property is the TOP row of data in rngUsedRange, and
..Rows.Count is the number of rows of data in rngUsedRange. Hence the reason
that you add both of them together to locate the next available row below
the block of data.)

Public Sub Demo()
Dim rngUsedRange As Range
Dim lngNextRow As Long

Set rngUsedRange = Sheet4.UsedRange

With rngUsedRange
lngNextRow = .Row + .Rows.Count 'In case of blank rows above data.
End With

'More code to add new data.
End Sub

--
Regards,
Bill Renaud