Consulting

Results 1 to 11 of 11

Thread: Activate range on a different sheet

  1. #1

    Activate range on a different sheet

    Hi all,

    I have created a commandbutton and following codes are associated with that :

    [VBA]Private Sub CommandButton1_Click()
    Sheets("Prob-1").Activate
    Range("a1").Activate
    End Sub
    [/VBA]


    However when I run this code then I get following error :

    Run time error '1004'
    activate method of range class failed.

    In the mean time I have gone through the help page corresponding to that error. And as par it's advise I have checked "trust access to visual basic project" under tool -> macro -> security. But still that error exists.

    Can anyone tell me where is the error on that? Your help will be highly appreciated.

    Regards,

  2. #2
    I am going to assume the code for the button click event is within a different sheet object than Sheet "Prob-1". If this is the case then you will need to fully qualify:

    Sheets("Prob-1").Activate
    Sheets("Prob-1").Range("A1").Select (or activate)

    If you don't want to have to fully qualify you can run your code in a module and reference that module in the click event of the sheet object, or there's probably another more elegant way of doing it..this is just all I know .
    Last edited by greymalkin; 06-24-2008 at 11:11 AM.

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    Range("A1").Select

    activate is only (as far as i know) used on sheets.
    select is used on cells

  4. #4
    I also have tried with "select" however getting same error

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    266
    Location
    did you use a capital A in Range("A1").Select?

  6. #6
    VBAX Mentor
    Joined
    Oct 2007
    Posts
    372
    Location
    Quote Originally Posted by Ago
    did you use a capital A in Range("A1").Select?
    shouldn't matter. I just did the same routine and it worked fine with

    [VBA]Range("a1").select[/VBA]

    Make sure you have a sheet named the same as the one you're trying to activate!

  7. #7
    Hi greymalkin, your suggestion really worked, thanks

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Ago
    Range("A1").Select

    activate is only (as far as i know) used on sheets.
    select is used on cells
    You can activate a range as well.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  9. #9
    VBAX Regular
    Joined
    Apr 2006
    Posts
    32
    Location
    Ago,

    Activate is not confined to Sheets. Activate may be used with Chart and ChartObjects, Worksheet objects, OLEObject objects, Pane objects, Range objects, Window objects, & Workbook objects. A cell is a Range object.

    Alan

  10. #10
    Moderator VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Hi aarun, I changed the title of your thread. Please try to give your threads meaningful names so people can find this problem with a search.
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  11. #11
    Hi Lucas, ya I agree this should be a better idea

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •