+ Reply to Thread
Results 1 to 6 of 6

Using variable in Range in Sub

Hybrid View

  1. #1
    Registered User
    Join Date
    03-03-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    53

    Using variable in Range in Sub

    Hi

    I seem to be confusing the use of variables in ranges somehow so would appreciate help. The code below is intended to highlight one row as defined by the variable r which is a for next counter, but instead it highlights the whole sheet.

    Range("A" & r & ":S" & r).Select
        With Selection.Interior
            .ColorIndex = 15
            .Pattern = xlSolid
        End With
    The routines below to clear certain columns and remove all formatting before running work fine but these variables are not part of a loop counter.
        Range("A" & firstrow & ":A" & lastrow).Select
        Selection.clearcontents
        
        Range("R" & firstrow & ":S" & lastrow).Select
        Selection.clearcontents
    
        Range("A" & firstrow & ":S" & lastrow).Select
        Selection.Interior.ColorIndex = xlNone
    Some insight from the more experienced as to what I am doing wrong would be greatly appreciated.
    Last edited by Orada; 03-03-2011 at 12:48 PM.

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Using variable in Range in Sub

    Instead of
    Range("A" & r & ":S" & r).Select
        With Selection.Interior
            .ColorIndex = 15
            .Pattern = xlSolid
        End With
    Try:
    With Range("A" & r , "S" & r).Interior
       .ColorIndex = 15
       .Pattern = xlSolid
    End With
    Instead of
        Range("A" & firstrow & ":A" & lastrow).Select
        Selection.clearcontents
        
        Range("R" & firstrow & ":S" & lastrow).Select
        Selection.clearcontents
    
        Range("A" & firstrow & ":S" & lastrow).Select
        Selection.Interior.ColorIndex = xlNone
    Try:
    Range("A" & firstrow , "A" & lastrow).ClearContents
    Range("R" & firstrow , "S" & lastrow).ClearContents
    Range("A" & firstrow , "S" & lastrow).Interior.ColorIndex = xlNone

  3. #3
    Registered User
    Join Date
    03-03-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Using variable in Range in Sub

    Quote Originally Posted by stnkynts View Post
    Try:
    With Range("A" & r , "S" & r).Interior
       .ColorIndex = 15
       .Pattern = xlSolid
    End With
    Try:
    Range("A" & firstrow , "A" & lastrow).ClearContents
    Range("R" & firstrow , "S" & lastrow).ClearContents
    Range("A" & firstrow , "S" & lastrow).Interior.ColorIndex = xlNone
    Thanks but got runtime error - Method chance of object global failed.

  4. #4
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: Using variable in Range in Sub

    Could you submit your whole code or a copy of the workbook for review?

  5. #5
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Using variable in Range in Sub

    Your first bit of code should select A1:S1 (or whatever r equals) - are you saying it doesn't? Apart from unnecessary use of Select your use of variables looks fine to me.

  6. #6
    Registered User
    Join Date
    03-03-2011
    Location
    England
    MS-Off Ver
    Excel 2003
    Posts
    53

    Re: Using variable in Range in Sub

    Quote Originally Posted by StephenR View Post
    Your first bit of code should select A1:S1 (or whatever r equals) - are you saying it doesn't? Apart from unnecessary use of Select your use of variables looks fine to me.
    OK, I'm losing the variable, when r is null then the complete worksheet gets selected for format.

    I'll get it sorted now, thanks, I had been calling the routine from a workbook

+ 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