Find method syntax

G

Guest

Hi All,
I'd like to understand the difference between these two syntaxes below:

a./ lastcolumn = Cells.Find(What:="*", _
After:=Range("A1"), _
LookAt:=xlPart, _
LookIn:=xlValues, _
SearchOrder:=xlByColumns, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Column
This one works!

b./ lastcolumn = Cells.Find("*", Range("A1"), xlPart,
xlValues, xlByColumns, xlPrevious, False).Column

This one does not work at the same place in the code, but gives an error
message No. 1004 saying Find property of Class range not accessible!

Regards,
Stefi
 
B

Bob Phillips

That is because in the first example you have the Find arguments out of
order, LookIn comes before LookAt in the order. By specifying the keywords
that doesn't matter, but when you don't specify them you need to be in the
correct order.

So

lastcolumn = Cells.Find("*", Range("A1"), xlValues, xlPart,
xlByColumns, xlPrevious, False).Column

should work
 
G

Guest

Thanks, Bob, how obvious the answer and how difficult to notice the
difference for an unpracticed eye.
Stefi


„Bob Phillips†ezt írta:
 
C

Chip Pearson

That's why its good programming practice to use named arguments.
Sure, its a bit more typing, but it saves you from errors in
calling methods.


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
G

Guest

Yes, you are right, one can avoid errors by using named arguments, but I
learned what rules are to be kept to when using different syntaxes from my
own fault (and from Bob's answer of course).

Nonetheless I keep your advice in mind.
Regards,
Stefi


„Chip Pearson†ezt írta:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Selectblank cells 2
lastrow function help 6
Find Question 1
Can't get the macro to continue through a range 2
inserting columsns variable range 4
VB Question 3
syntax in VBA to create a pivot table 2
conso macro 0

Top