+ Reply to Thread
Results 1 to 6 of 6

Changing range in VBA to lastrow

Hybrid View

  1. #1
    Registered User
    Join Date
    01-28-2015
    Location
    CA
    MS-Off Ver
    2007
    Posts
    67

    Changing range in VBA to lastrow

    So i have this VBA code

    Option Explicit
    
    Sub ValuesByColors()
    
    Const Blk = 0
    Const DkGray = 65535
    Const LtGray = 12632256
    Const OW = 16777215
    Dim r As Range
    
    Application.ScreenUpdating = False
    
    
    For Each r In Range("A1:Q11").Cells
        Select Case r.Interior.Color
           Case Is = Blk
                r.Value = 0
           
           Case Is = DkGray
                r.Value = 3
           
           Case Is = LtGray
                r.Value = 10
                     
            Case Is = OW
                r.Value = 21
        End Select
    Next r
    
    Application.ScreenUpdating = True
    
    
    End Sub
    but i want the range of which
    "For Each r In Range("A1:Q11").Cells"
    to change to last row but the following does not work


    Option Explicit
    
    Sub ValuesByColors()
    
    Dim lastrow As Long
     lastrow = Range("A" & Rows.Count).End(xlUp).Row
    
    Const Blk = 0
    Const DkGray = 65535
    Const LtGray = 12632256
    Const OW = 16777215
    Dim r As Range
    
    Application.ScreenUpdating = False
    
    
    For Each r In Range("D1").AutoFill Destination:=Range("D1:S" & lastrow)
        Select Case r.Interior.Color
           Case Is = Blk
                r.Value = 0
           
           Case Is = DkGray
                r.Value = 3
           
           Case Is = LtGray
                r.Value = 10
                     
            Case Is = OW
                r.Value = 21
        End Select
    Next r
    
    Application.ScreenUpdating = True
    
    
    End Sub

  2. #2
    Forum Expert
    Join Date
    10-09-2014
    Location
    Newcastle, England
    MS-Off Ver
    2003 & 2013
    Posts
    1,986

    Re: Changing range in VBA to lastrow

    In your 1st one you're not Autofilling? is that meant to be there in the amended code?
    If someone has helped you then please add to their Reputation

  3. #3
    Registered User
    Join Date
    01-28-2015
    Location
    CA
    MS-Off Ver
    2007
    Posts
    67

    Re: Changing range in VBA to lastrow

    Quote Originally Posted by pjwhitfield View Post
    In your 1st one you're not Autofilling? is that meant to be there in the amended code?
    Yeah thats the Original Code
    I'm trying to ammend that code to be the second code, but the second code doesn't work.

  4. #4
    Registered User
    Join Date
    01-28-2015
    Location
    CA
    MS-Off Ver
    2007
    Posts
    67

    Re: Changing range in VBA to lastrow

    Essentially i want the range of which the values are placed to be lastrow
    so the range would be
    "D1:S & Lastrow"

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,644

    Re: Changing range in VBA to lastrow

    Does this work?
    For Each r In Range("D1:S" & lastrow)
    Or do you only want to loop through a particular column?
    If posting code please use code tags, see here.

  6. #6
    Registered User
    Join Date
    01-28-2015
    Location
    CA
    MS-Off Ver
    2007
    Posts
    67

    Re: Changing range in VBA to lastrow

    Quote Originally Posted by Norie View Post
    Does this work?
    For Each r In Range("D1:S" & lastrow)
    Or do you only want to loop through a particular column?

    huh..... who knew the answer would be so simple.
    Thanks that works perfectly haha!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] How to find text then select RANGE form the text found to lastrow of Column J
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2014, 02:45 AM
  2. [SOLVED] How to select Range A1 to LastRow/LastColumn
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 03-29-2014, 03:40 AM
  3. Creating a table with VBA using lastrow as range
    By washout in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2013, 12:20 PM
  4. How to transpose data into range ("A1:G" & lastrow)
    By jasondu in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-06-2012, 10:39 AM
  5. LastRow on Changing Pivottable
    By rasonline in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-07-2011, 11:28 AM
  6. Copy From LastRow to LastRow+7
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2010, 06:56 AM
  7. Copy from named range down until lastrow?
    By jazbath in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 01-19-2007, 05:56 PM

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