+ Reply to Thread
Results 1 to 10 of 10

With Range .... why not working?

Hybrid View

  1. #1
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    With Range .... why not working?

    I'm fairly new working with Range variables and various actions. FYI, FindRows and FindCols are private functions that return number of non-empty rows as long and number of non-empty columns as long.

    Not sure why this isn't working:

    Dim NumberOfRows As Long
    Dim NumberOfColumns As Long
    NumberOfRows = FindRows("Report", 2, 1)
    NumberOfColumns = FindCols("Report", 1, 2)
            
    Dim FormatRng1 As Range
    Set FormatRng1 = Range(Cells(1, NumberOfColumns), Cells(NumberOfRows, NumberOfColumns))
    
    With Sheets("Report").Range(FormatRng1).Borders(xlEdgeLeft)
       .LineStyle = xlContinuous
       .ColorIndex = xlAutomatic
       .TintAndShade = 0
       .Weight = xlThin
    End With

  2. #2
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: With Range .... why not working?

    Try

    Dim NumberOfRows As Long
    Dim NumberOfColumns As Long
    NumberOfRows = FindRows("Report", 2, 1)
    NumberOfColumns = FindCols("Report", 1, 2)
            
    Dim FormatRng1 As Range
    Set FormatRng1 = Sheets("Report").Range(Cells(1, NumberOfColumns), Cells(NumberOfRows, NumberOfColumns))
    
    With FormatRng1.Borders(xlEdgeLeft)
       .LineStyle = xlContinuous
       .ColorIndex = xlAutomatic
       .TintAndShade = 0
       .Weight = xlThin
    End With
    Martin

  3. #3
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: With Range .... why not working?

    Thanks. Tried it and got a Run-time error "1004": Application-defined or object-defined error

  4. #4
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: With Range .... why not working?

    On which line did the error occur?

  5. #5
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: With Range .... why not working?

    This one:
    Set FormatRng1 = Sheets("Report").Range(Cells(1, NumberOfColumns), Cells(NumberOfRows, NumberOfColumns))

  6. #6
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: With Range .... why not working?

    Ok - try

    Set FormatRng1 = Sheets("Report").Range(Sheets("Report").Cells(1, NumberOfColumns), Sheets("Report").Cells(NumberOfRows, NumberOfColumns))

  7. #7
    Registered User
    Join Date
    11-25-2011
    Location
    Alaska
    MS-Off Ver
    Excel 2003
    Posts
    42

    Re: With Range .... why not working?

    Yep... that worked. Not sure what the proper terminology is, but it seems the "cells" has to be fully specified.

  8. #8
    Forum Expert mrice's Avatar
    Join Date
    06-22-2004
    Location
    Surrey, England
    MS-Off Ver
    Excel 2013
    Posts
    4,967

    Re: With Range .... why not working?

    It depends which sheet is active when the line of code runs. The default is the active sheet. From what you reported, it looks like an sheet other than the Report sheet was active at the time.

  9. #9
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MO Prof Plus 2016
    Posts
    6,913

    Re: With Range .... why not working?

    To streamline a little.

    With Sheets("Report")
        Set FormatRng1 = .Range(.Cells(1, NumberOfColumns), .Cells(NumberOfRows, NumberOfColumns))
    End With
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  10. #10
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: With Range .... why not working?

    Or perhaps
    Set FormatRng1 = Sheets("Report").Cells(1, NumberOfColumns).Resize(NumberOfRows)
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ 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. Copy+Paste using 'Range=Range' not working
    By toastergirl in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-20-2016, 01:25 PM
  2. [SOLVED] range not working
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-17-2015, 04:43 PM
  3. [SOLVED] range code not working
    By ammartino44 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-18-2015, 01:14 PM
  4. How do I know if Dynamic Range is working?
    By livemusic in forum Excel General
    Replies: 9
    Last Post: 07-02-2011, 11:16 PM
  5. [SOLVED] Working with a range
    By Dan Chupinsky in forum Excel General
    Replies: 3
    Last Post: 07-06-2005, 02:05 AM
  6. Working out first and last column and row in a range
    By Peter Rooney in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-28-2005, 10:06 AM
  7. [SOLVED] Working with range
    By François in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-28-2005, 09:06 AM

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