Consulting

Results 1 to 6 of 6

Thread: Solved: Range("A4").End(xlDown) Error

  1. #1
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location

    Solved: Range("A4").End(xlDown) Error

    I am receiving an error using the following code. I've noticed, however, that the error only occurs if the worksheet, "Transactions," is not the active worksheet. Is there a way to make this line of code work properly regardless worksheet tab that is selected?

    [VBA]
    Worksheets("Transactions").Range("A4").End(xlDown).Select
    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    If transactions is not active, you cannot select a range in that sheet, you have to activate it first.

    [vba]

    With Worksheets("Transactions")

    .Activate
    .Range("A4").End(xlDown).Select
    End With
    [/vba]
    ____________________________________________
    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

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Do you really need to select it? You can use the cell without selecting it first, regardless of which sheet is active

    [VBA]
    myVar = Worksheets("Transactions").Range("A4").End(xlDown).value
    [/VBA]

    Paul

  4. #4
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by xld
    If transactions is not active, you cannot select a range in that sheet, you have to activate it first.

    [vba]

    With Worksheets("Transactions")

    .Activate
    .Range("A4").End(xlDown).Select
    End With
    [/vba]
    Very good. Thanks, as always.

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    You can also use GoTo to select a range in the same or another open workbook.

    [vba]
    Application.Goto Worksheets("Transactions").Range("A4").End(xlDown)
    'or
    Application.Goto Workbooks("Test.xls").Worksheets("Transactions").Range("A4").End(xlDown)
    'or
    Application.Goto Workbooks("Test.xls").Worksheets("Transactions").Range("Data")



    [/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'

  6. #6
    VBAX Expert
    Joined
    Feb 2010
    Posts
    696
    Location
    Quote Originally Posted by mdmackillop
    You can also use GoTo to select a range in the same or another open workbook.

    [vba]
    Application.Goto Worksheets("Transactions").Range("A4").End(xlDown)
    'or
    Application.Goto Workbooks("Test.xls").Worksheets("Transactions").Range("A4").End(xlDown)

    [/vba]
    Thanks.

Posting Permissions

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