Consulting

Results 1 to 5 of 5

Thread: One line of code - what is wrong?

  1. #1

    One line of code - what is wrong?

    For some mysetious reason I am getting an error around the following line of code:


    Set workingsrange = Sheets(3).Range(Range("b2"), Range("e9"))


    "Application-defined or object-defined error"

    Any idea?

  2. #2
    Solved:

    Sheets(3).activate
    set workingsrange = Range(Range("b2"), Range("e9"))

  3. #3
    Knowledge Base Approver VBAX Guru GTO's Avatar
    Joined
    Sep 2008
    Posts
    3,368
    Location
    Rather than Select/Activate a sheet, you can set a good solid reference to the range you want. This way, it doesn't matter which sheet happens to be active.
        With ThisWorkbook.Worksheets(3)
            Set workingsrange = Range(.Range("b2"), .Range("e9"))
        End With
    Hope that helps,

    Mark

  4. #4
    VBAX Guru Kenneth Hobs's Avatar
    Joined
    Nov 2005
    Location
    Tecumseh, OK
    Posts
    4,956
    Location
    [VBA]Set workingsrange = Sheets(3).Range(Sheets(3).Range("b2"), Sheets(3).Range("e9"))
    [/VBA]

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For a bit of variety
    [VBA]Set workingsrange = Sheets(3).Range("b2").Resize(8, 4)
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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