Consulting

Results 1 to 5 of 5

Thread: Solved: Why using Range() works but Cells() doesn't??

  1. #1

    Solved: Why using Range() works but Cells() doesn't??

    Hi,

    I need to select a range but needed to define a dynamic variable as things change. My variable is call endcol_d and it is the column I need to put in the range.
    So I have
    [VBA]
    basebook.Sheets("Protect Population").Range(Cells(5, endcol_d), Cells(5, endcol_d + 1)).Select[/VBA]

    But that doesn't work. It gives and error. But when I test it using Range("") for one instance it works. [VBA]basebook.Sheets("Protect Population").Range("B5:C5").Select[/VBA]
    How can I make it work using Cells??

    Appreciate any help!!

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You need to dot qualify everything

    [vba]

    With basebook.Sheets("Protect Population")

    .Range(.Cells(5, endcol_d), .Cells(5, endcol_d + 1)).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
    Ah that fixed it! Thanks!!

  4. #4
    VBAX Regular
    Joined
    Nov 2010
    Posts
    22
    Location
    endcol_d = 4
    ThisWorkbook.Sheets(1).Range(Cells(5, endcol_d), Cells(5, endcol_d + 1)).Select

  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 Resize
    [VBA]basebook.Sheets("Protect Population").Cells(5, endcol_d).Resize(,2).Select[/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
  •