Results 1 to 5 of 5

Determine Last Row And Column With Data In Sheets With Merged Cells

Threaded View

  1. #1
    Registered User
    Join Date
    06-12-2008
    Location
    Newbury
    MS-Off Ver
    Office 365 ProPlus
    Posts
    92

    Determine Last Row And Column With Data In Sheets With Merged Cells

    I am using the following functions to determine the highest row and column that contain data:

    
    Public Function LastColumn(ws As Worksheet) As Long
    
    Dim LColumn As Long
        
        If Application.CountA(ws.Cells) > 0 Then
            
            LColumn = ws.Cells.Find(What:="*", _
                                    after:=ws.Range("A1"), _
                                    SearchOrder:=xlByColumns, _
                                    SearchDirection:=xlPrevious).Column
        Else
        
            LColumn = 0
        
        End If
        
        
        LastColumn = LColumn
        
    
    End Function
    
    Public Function LastRow(ws As Worksheet) As Long
    
    Dim lrow As Long
        
        If Application.CountA(ws.Cells) > 0 Then
            
            lrow = ws.Cells.Find(What:="*", _
                                 after:=ws.Range("A1"), _
                                 SearchOrder:=xlByRows, _
                                 SearchDirection:=xlPrevious).Row
        Else
        
            lrow = 0
        
        End If
        
        LastRow = lrow
    
    End Function
    I've just inherited a spreadsheet which contains merged cells and it would seem these merged cells cause the above functions to fail, on the lines in bold, with a Run-time error '91' "Object variable or with block variable not set".

    The real problem is the spreadsheet, I've just started a new job and the spreadsheet I've inherited is horrible, 90+ sheets filled with all sorts of crap. However redoing the spreadsheet in a sane fashion isn't really an option at the moment and I need to get on with identifying all the cells with errors and fixing them so does anyone know of a way in which to deal with merged cells when trying to determine the highest column and highest row to contain data?
    Last edited by chergh; 01-28-2009 at 10:11 AM.

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