Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 200
  • Last Modified:

When do you need to Activate

I had set wb = this workbook and
set ws = wb.sheets("orders")

i = application.CountA(Range("A":"A"))

This did not seem to provide the correct count unless I was in the orders worksheet.  Finally I did ws.activate and it worked fine.  2 questions

1 - When do you need to Activate
2 - Could I have avoided Activate by being explicit in my range selection......ws.range("A:A")

Don't yell at me I know I need to always explicitly name my range...I will..I just overlooked this range name somehow..
0
upobDaPlaya
Asked:
upobDaPlaya
  • 2
  • 2
1 Solution
 
zorvek (Kevin Jones)ConsultantCommented:
Always fully qualify your object references to avoid this problem:

i = Application.CountA(ws.Range("A":"A"))

Kevin
0
 
zorvek (Kevin Jones)ConsultantCommented:
And do not use the Activate method unless absolutely necessary.

Kevin
0
 
upobDaPlayaAuthor Commented:
thanks..i had a separate question where I learned the downfalls of activate, but was still a little foggy...it is defintely sinking in now..thanks for providing a great best practice foundation
0
 
upobDaPlayaAuthor Commented:
Ooops..that did not work..but I left out some information..further below I have

ws.range(D2:F7:).Select
selection.autofill destination:=Range("D2:F" & j)
......

I get select method of range class failed...

I assume you have to activate for a selection.  If so perhaps there is a better way of doing an autofill that does not involve a selection ?
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now