+ Reply to Thread
Results 1 to 5 of 5

Syntax for Range command with a variable

Hybrid View

nebb Syntax for Range command with... 08-10-2007, 02:51 PM
Leith Ross Hello Nebb, Use this code.... 08-10-2007, 03:17 PM
nebb My thanks to Leith for his... 08-10-2007, 04:07 PM
Leith Ross Hello Nebb, The argument... 08-10-2007, 05:23 PM
nebb Thank you Leith. That did the... 08-11-2007, 12:14 PM
  1. #1
    Registered User
    Join Date
    04-30-2004
    Posts
    78

    Syntax for Range command with a variable

    In a macro, if I wanted to select all cells from A1 to F8, I would use
    Range ("A1:F8").select. If I were to use an input box to have the user input the location of the last cell to select (for example:
    LastC=inputbox("Enter Last Cell to be selected") , What is the syntax of the Range command to allow me to select these cells starting at A1?

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Nebb,

    Use this code. LastC needs to be a string, and must be a single cell reference like "A1".
    Range("A1", LastC).Select
    Sincerely,
    Leith Ross

  3. #3
    Registered User
    Join Date
    04-30-2004
    Posts
    78
    My thanks to Leith for his reply. His suggestion does work. However, it presents me with another question. If I use the following code:

    Dim LastC As String
    LastC = InputBox("Enter last cell location")
    Range("a1", LastC).Select

    The code works perfectly and the cells are properly selected. If, however, the code reads:

    Dim LastC As String
    LastC = F10
    Range("a1", LastC).Select
    I get a Run Time error '1004'
    Method 'Range' of object '_global' failed

    What should I have entered to prevent this run time error?
    Last edited by mudraker; 08-11-2007 at 06:44 PM.

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Nebb,

    The argument required by the Range Object is a String. Enclose F10 in quotes...
    Dim LastC As String
    LastC = "F10"
    Range("a1", LastC).Select
    Sincerely,
    Leith Ross

  5. #5
    Registered User
    Join Date
    04-30-2004
    Posts
    78
    Thank you Leith. That did the job.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1