0

To find the last row or column, I'll typically I'll use something like:

LR = Cells(Rows.Count, 1).End(xlUp).Row
LC = Cells(1, Columns.Count).End(xlToLeft).Column

However, I'm trying to find the furthest right column with data in a case where there are no column headers, and the column I'm hoping to find could be column B (IE "B27") or it could be column Z (IE "Z1000"), or any random cell.

Similarly, I'm also trying to find the last row on the same sheet, with no column headers, where the last row could be in any column.

Knowing how to do that could help in several scenario's, but to elaborate on my current scenario, I'm trying to concatenate some lines that were copied from other sources and had many tabs. Sometimes tabs are at the beginning of the line and sometimes they are in the middle. This of course pushes each line into multiple cells, sometimes very far to the right.

Usually these documents are not more than 10k lines, so I thought about writing a "brute-force" type of code like:

Private Sub Concatenate_20k_Rows()

    Dim ws As Worksheet
    Dim cell As Range
    Dim LC As Long, i As Long
    
    Set ws = ActiveSheet
    
    For Each cell In ws.Range("A1:A20000")
        LC = Cells(cell.Row, Columns.Count).End(xlToLeft).Column
        For i = 1 To LC
            cell.Value = cell.Value & " " & cell.Offset(0, i).Value
        Next i
        cell.Value = Application.Trim(cell.Value)
    Next cell

End Sub

I suppose finding the last column with unknown headers won't be as important for my scenario, but finding the last row could be useful so I don't have to brute-force through 20k rows if there's only 2k rows, or don't have to risk not going far enough in the off chance I have more than 20k rows.

E_net4
  • 27,810
  • 13
  • 101
  • 139
  • 2
    Maybe refer to [here](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba)? – findwindow Apr 13 '22 at 18:39
  • 1
    What I usually do when I am unsure of which row/column to consider is - I will find the lastrow/lastcolumn of multiple random rows/columns and compare those and get the max value from it. – Charlie Apr 14 '22 at 04:26

1 Answers1

1

You can use the Range.Find method.
As written, this will return a 2 element array where

  • the first element = Row number
  • and the second element = column number.
Option Explicit
Function LastRC(Worksht As String) As Long()
'Uncomment if on worksheet
'Application.Volatile
    Dim WS As Worksheet, R As Range
    Dim LastRow As Long, LastCol As Long
    Dim L(1) As Long
    Dim searchRng As Range

Set WS = Worksheets(Worksht)
Set searchRng = WS.Cells

With searchRng
    Set R = .Cells.Find(what:="*", after:=.Cells(1, 1), _
                    LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByRows, _
                    searchdirection:=xlPrevious)

    If Not R Is Nothing Then
        LastRow = R.Row
        LastCol = .Cells.Find(what:="*", after:=.Cells(1, 1), _
                    LookIn:=xlFormulas, lookat:=xlPart, searchorder:=xlByColumns, _
                    searchdirection:=xlPrevious).Column
    Else
        LastRow = 1
        LastCol = 1
    End If
End With

L(0) = LastRow
L(1) = LastCol
LastRC = L
End Function

Ron Rosenfeld
  • 53,870
  • 7
  • 28
  • 60
  • I added your function to my module and pushed the results into a variant using this sub: `Private Sub lrcheck() Dim v As Variant, LastR As Long, LastC As Long v = LastRC(ActiveSheet.Name) LastR = v(0) LastC = v(1) End Sub` Might be worth adding the sub to your answer, but your answer worked like a charm. Thank you – Sabatino Ognibene Apr 14 '22 at 15:33