+ Reply to Thread
Results 1 to 3 of 3

How to use named ranges in VBA code using "Columns" and a variation on "Range"

Hybrid View

  1. #1
    Registered User
    Join Date
    02-28-2014
    Location
    Raleigh
    MS-Off Ver
    Excel 2010
    Posts
    4

    How to use named ranges in VBA code using "Columns" and a variation on "Range"

    I am trying to clean up my code by using named ranges so that it will still work if/when others add columns or rows to the spreadsheet. These should be pretty easy solutions for most of you but I can't seem to find any answers online.

    Here are 2 examples of the code I am trying to update:

    I was thinking that replacing "A" with "namedRange" would work but I suppose that's not how the Columns application works. How can I modify the code below to work with a named range instead of the fixed column "A"?
    ' Moves cursor to the first empty row.
    
            Columns("A").Find("", Cells(Rows.Count, "A")).Select
    For the following I would have thought that replacing "AU" with "namedRange" would do the trick as that's how things have worked for me in the past using the Range application. Unfortunately I get an error when I make the change. I'm sure it's something pretty simple but I can't figure it out.

    ' Creates separators between unique samplings
    
            Dim LR As Long, i  As Long, j As Long
                LR = Range("AU" & Rows.Count).End(xlUp).Row
            For i = LR + 1 To 2 Step -1
                With Range("AU" & i)
                    If .Value <> .Offset(-1).Value Then
                        .Offset(-1).EntireRow.Borders(xlEdgeBottom).Weight = xlMedium
                    End If
                End With
            Next i

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

    Re: How to use named ranges in VBA code using "Columns" and a variation on "Range"

    How are you defining the named ranges?
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    02-28-2014
    Location
    Raleigh
    MS-Off Ver
    Excel 2010
    Posts
    4

    Re: How to use named ranges in VBA code using "Columns" and a variation on "Range"

    I am highlighting the range I want named, then entering the desired name in the excel "Name Box".

+ 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 USE """"" cells count """"" change font color
    By austin123456 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-09-2013, 06:14 AM
  2. [SOLVED] Pasting values between ranges in different workbooks: "Range" works but "Cells" doesn't
    By Flaubert in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-13-2013, 01:19 PM
  3. Replies: 0
    Last Post: 01-09-2013, 06:52 PM
  4. Replies: 3
    Last Post: 06-04-2011, 10:56 AM
  5. Macro Error with code "Range("A65536").End(xlUp).EntireRow.Insert"
    By lukasj13 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-30-2010, 08:48 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