• Last cell containing value (Win98 / Excel97)

    Home » Forums » AskWoody support » Productivity software by function » Visual Basic for Applications » Last cell containing value (Win98 / Excel97)

    Author
    Topic
    #409303

    I’m familiar with Word objectmodel and VBA, but not at all with Excel. Using VBA, how can I get the coordinates of the last cell (row/column) that contains a value (is not empty)?
    A client receives worksheets that contain a varying number of rows and sometimes 1 or 2 extra columns and she wants to put the Sum() of the last column two rows below the last cell with a value in that column.

    Viewing 3 reply threads
    Author
    Replies
    • #871394

      (Edited by JohnBF on 01-Sep-04 08:36. )

      Worksheets(“mysheet”).SpecialCells(xlLastCell).Offset(2, 0).FormulaR1C1 = “=SUM(R[*]C[*]:R[*]C[*])”

      where for “*” you will need to supply the coordinates for the beginning and ending range for the sum, should get you started.

      • #871507

        John: That method can return unexpected results on at least some versions of Excel (XL2K which I use for example). If rows and/or columns have been deleted from the worksheet, and the workbook has not been saved, then your method can return a cell that is much below and/or to the right of the actual last used cell. The VBA function below should be more reliable:

        Public Function FindLastCell(strWorksheet As String) As Range
        Dim oUsed As Range
        Dim I As Long, lLastRow As Long, lLastCol As Long, lLast As Long
            Set oUsed = Worksheets(strWorksheet).UsedRange
            lLastCol = 0
            For I = 0 To oUsed.Rows.Count - 1
                lLast = oUsed.Offset(I, oUsed.Columns.Count + 1).End(xlToLeft).Column
                If lLast > lLastCol Then
                    lLastCol = lLast
                End If
            Next I
            lLastRow = 0
            For I = 0 To oUsed.Rows.Count - 1
                lLast = oUsed.Offset(oUsed.Rows.Count + 1, I).End(xlUp).Column
                If lLast > lLastRow Then
                    lLastRow = lLast
                End If
            Next I
            Set FindLastCell = Range("A1").Offset(lLastRow - 1, lLastCol - 1)
        End Function
        
        • #871616

          hmmn

          Function lastusedcell() As String
          Dim intCol As Integer
          Dim lngRow As Long
          For intCol = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
          If Application.WorksheetFunction.CountBlank(Columns(intCol)) < ActiveSheet.Rows.Count Then Exit For
          Next intCol
          For lngRow = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
          If Application.WorksheetFunction.CountBlank(Rows(lngRow)) < ActiveSheet.Columns.Count Then Exit For
          Next lngRow
          lastusedcell = ActiveSheet.Cells(lngRow, intCol).Address
          End Function

        • #871617

          hmmn

          Function lastusedcell() As String
          Dim intCol As Integer
          Dim lngRow As Long
          For intCol = ActiveSheet.UsedRange.Columns.Count To 1 Step -1
          If Application.WorksheetFunction.CountBlank(Columns(intCol)) < ActiveSheet.Rows.Count Then Exit For
          Next intCol
          For lngRow = ActiveSheet.UsedRange.Rows.Count To 1 Step -1
          If Application.WorksheetFunction.CountBlank(Rows(lngRow)) < ActiveSheet.Columns.Count Then Exit For
          Next lngRow
          lastusedcell = ActiveSheet.Cells(lngRow, intCol).Address
          End Function

      • #871508

        John: That method can return unexpected results on at least some versions of Excel (XL2K which I use for example). If rows and/or columns have been deleted from the worksheet, and the workbook has not been saved, then your method can return a cell that is much below and/or to the right of the actual last used cell. The VBA function below should be more reliable:

        Public Function FindLastCell(strWorksheet As String) As Range
        Dim oUsed As Range
        Dim I As Long, lLastRow As Long, lLastCol As Long, lLast As Long
            Set oUsed = Worksheets(strWorksheet).UsedRange
            lLastCol = 0
            For I = 0 To oUsed.Rows.Count - 1
                lLast = oUsed.Offset(I, oUsed.Columns.Count + 1).End(xlToLeft).Column
                If lLast > lLastCol Then
                    lLastCol = lLast
                End If
            Next I
            lLastRow = 0
            For I = 0 To oUsed.Rows.Count - 1
                lLast = oUsed.Offset(oUsed.Rows.Count + 1, I).End(xlUp).Column
                If lLast > lLastRow Then
                    lLastRow = lLast
                End If
            Next I
            Set FindLastCell = Range("A1").Offset(lLastRow - 1, lLastCol - 1)
        End Function
        
    • #871395

      (Edited by JohnBF on 01-Sep-04 08:36. )

      Worksheets(“mysheet”).SpecialCells(xlLastCell).Offset(2, 0).FormulaR1C1 = “=SUM(R[*]C[*]:R[*]C[*])”

      where for “*” you will need to supply the coordinates for the beginning and ending range for the sum, should get you started.

    • #871624

      There is no need to run a loop…

      ‘=============================
      ‘Function BottomRightCorner
      ‘Jim Cone – San Francisco, CA
      ‘=============================
      Function BottomRightCorner(ByRef objSheet As Worksheet) As Range
      On Error GoTo NoCorner
      Dim BottomRow As Long
      Dim LastColumn As Long

      If objSheet.FilterMode Then objSheet.ShowAllData

      BottomRow = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      LastColumn = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
      Set BottomRightCorner = objSheet.Cells(BottomRow, LastColumn)

      Exit Function

      NoCorner:
      beep
      Set BottomRightCorner = objSheet.Cells(1, 1)
      End Function

      • #871717

        John, Legare, Jim thankyou
        You surely taught me a few things!

      • #871718

        John, Legare, Jim thankyou
        You surely taught me a few things!

    • #871625

      There is no need to run a loop…

      ‘=============================
      ‘Function BottomRightCorner
      ‘Jim Cone – San Francisco, CA
      ‘=============================
      Function BottomRightCorner(ByRef objSheet As Worksheet) As Range
      On Error GoTo NoCorner
      Dim BottomRow As Long
      Dim LastColumn As Long

      If objSheet.FilterMode Then objSheet.ShowAllData

      BottomRow = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
      LastColumn = objSheet.Cells.Find(what:=”*”, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
      Set BottomRightCorner = objSheet.Cells(BottomRow, LastColumn)

      Exit Function

      NoCorner:
      beep
      Set BottomRightCorner = objSheet.Cells(1, 1)
      End Function

    Viewing 3 reply threads
    Reply To: Last cell containing value (Win98 / Excel97)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: