Home » excel » excel – Finding last cell with content in a column -> why is my version of the code giving an error?

excel – Finding last cell with content in a column -> why is my version of the code giving an error?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to find the last cell containing information in a specific column and have found the below listed solution in another thread. However I tried to do it without defining ‘ws as Worksheet’.
If I do this, the compiler does not accept ‘.Rows’ within .cells(). Why?

Additional question:

  1. In the first version, what does ‘Set’ do exactly? Why do i need this?

Code found in thread:

Sub testprint_UtilAnal()

Dim ws As Worksheet
Dim xrowrange As Range
Dim xrowprint As Long

Set ws = Sheets("Database_UtilAnal")
With ws
Set xrowrange = .Cells(.Rows.Count, "B").End(xlUp)
Set xrowprint = xrowrange.Rows
End With

End Sub

My code:

Sub testprint_UtilAnal_Alt()

Dim xrowrange As Range
Dim xrowprint As Long

xrowrange = Sheets("Database_UtilAnal").Cells(.Rows.Count, "B").End(xlUp)
xrowprint = xrowrange.Rows

End Sub
How to&Answers:

Last Cell/Row in Column

Like you use Set for workbooks and worksheets, you have to use it for other objects, too, e.g. ranges.

If you need the last cell (object) in column “B“, you can use this:

Sub testprint_UtilAnal()

    Dim xRowRange As Range

    With Sheets("Database_UtilAnal")
        Set xRowRange = .Cells(.Rows.Count, "B").End(xlUp)
    End With

    Debug.Print xRowRange.Address

    Set xRowRange = Nothing

End Sub

If you need only the last row (number), you can use this:

Sub testprint_UtilAnal()

    Dim xRowPrint As Long

    With Sheets("Database_UtilAnal")
        xRowPrint = .Cells(.Rows.Count, "B").End(xlUp).Row
    End With

    Debug.Print xRowPrint

End Sub

The following two versions demonstrate how to get one by using the other:

Sub testprint_UtilAnal()

    Dim xRowPrint As Long
    Dim xRowRange As Range

    With Sheets("Database_UtilAnal")
        Set xRowRange = .Cells(.Rows.Count, "B").End(xlUp)
        xRowPrint = xRowRange.Row
    End With

    Debug.Print xRowRange.Address
    Debug.Print xRowPrint

    Set xRowRange = Nothing

End Sub

Sub testprint_UtilAnal()

    Dim xRowPrint As Long
    Dim xRowRange As Range

    With Sheets("Database_UtilAnal")
        xRowPrint = .Cells(.Rows.Count, "B").End(xlUp).Row
        Set xRowRange = .Cells(xRowPrint, "B")
    End With

    Debug.Print xRowPrint
    Debug.Print xRowRange.Address

    Set xRowRange = Nothing

End Sub

Answer:

The reason your code is not executing is because of Sheets("Database_UtilAnal").Cells(.Rows.Count, "B").End(xlUp)

The failing piece being .Rows.Count

The best way to imagine this is by visualizing a With statement. For all sakes and purposes, you can say that a With statement will append the parameter of said statement to any leading “.”

That is to say

With ThisWorkbook.Worksheets(1)
    .Range("A1")
End With

Is the same as

ThisWorkbook.Worksheets(1).Range("A1")

So why doesn’t Sheets("Database_UtilAnal").Cells(.Rows.Count, "B").End(xlUp) execute successfully?

Notice the leading “.” at .Rows.Count. Without a With statement, the compiler doesn’t see anything to append to that, and therefore it cannot execute.

EDIT:

The Set keyword is used to assign objects (such as Range("A1")) to certain non-primitive datatypes (such as a Range).