+ Reply to Thread
Results 1 to 2 of 2

problem with Range statement in With-End With loop

Hybrid View

dschmitt problem with Range statement... 04-22-2010, 04:16 AM
DonkeyOte Re: problem with Range... 04-22-2010, 04:19 AM
  1. #1
    Valued Forum Contributor
    Join Date
    02-21-2010
    Location
    Tokyo, Japan
    MS-Off Ver
    Excel 2007
    Posts
    502

    problem with Range statement in With-End With loop

    In the sub below the following code gives an error.

    With Sheets("Author ranking").Range(Cells(2, LastColumn), Cells(LastRow, LastColumn))
    The problem is definetely with this line, because if I specify the Range to Range("D:D") the sub is working.
    What is wrong here?
    I attached a test Excel sheet with the macro.

    Sub Macro1()
    
        Dim LastColumn As Long
        Dim LastColumnJ As Long
        Dim LastRow As Long
        Dim LastRow2 As Long
        Dim JAuth2Rw    As Long
        Dim Auth2Rw     As Long
        Dim NextRw      As Long
        Dim JAuthName   As String
        Dim AuthName    As String
        Dim strFirstAddress As String
        
        Sheets("J-Author ranking").Select
        
        LastColumn = Sheets("Author ranking").Cells(1, Columns.Count).End(xlToLeft).Column
        LastColumnJ = Sheets("J-Author ranking").Cells(1, Columns.Count).End(xlToLeft).Column
        
        LastRow = Sheets("Author ranking").Cells(Rows.Count, LastColumn).End(xlUp).Row
        LastRowJ = Sheets("J-Author ranking").Cells(Rows.Count, LastColumnJ).End(xlUp).Row
    
        
        For JAuth2Rw = 2 To LastRowJ
            JAuthName = Sheets("J-Author ranking").Cells(JAuth2Rw, LastColumnJ)
                
            With Sheets("Author ranking").Range(Cells(2, LastColumn), Cells(LastRow, LastColumn))
                Set rngFoundCell = .Find(What:=JAuthName, After:=.Cells(1, 1), LookAt:=xlPart)
                If Not rngFoundCell Is Nothing Then
                    strFirstAddress = rngFoundCell.Address
                    Do
                        
                        Sheets("J-Author ranking").Hyperlinks.Add Anchor:=Cells(JAuth2Rw, LastColumnJ), Address:="", _
                            SubAddress:="'Author ranking'!" & rngFoundCell.Address
        
                        Set rngFoundCell = .FindNext(rngFoundCell)
                    Loop While Not rngFoundCell Is Nothing And rngFoundCell.Address <> strFirstAddress
                End If
            End With
        
        Next JAuth2Rw
    
    End Sub
    Attached Files Attached Files
    Last edited by dschmitt; 04-22-2010 at 04:24 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: problem with Range statement in With-End With loop

    Untested but basically in this context you would either need to use:

    With Sheets("Author ranking").Range(Sheets("Author ranking").Cells(2,LastColumn),Sheets("Author ranking").Cells(LastRow,LastColumn))
    or

    With Sheets("Author ranking")
        With .Range(.Cells(2,LastColumn),.Cells(LastRow,LastColumn))
    
        End With
    End With
    using the latter is obviously simpler - using the former it would generally be a good idea to assign the sheet object to a variable so you can use shorthand reference.

    (in essence each range object used must be qualified against the parent sheet object - Sheets("z").Range("D:D") works because you have one range object - using Range(Cells,Cells) you have three)

+ 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