3

I have a problem with my code. In the data there are 60 rows, and in column T, there are 57 rows which have value i.e that are not blank. Cells 58,59,60 are blank. I am trying to find the last row for Column T. Whenever I run the code, it always gives me 60 instead of 57.

Here is my code

Dim lastrow As Long

lastrow = Cells(Rows.Count, 20).End(xlUp).Row

MsgBox "The Last Row of Data for Column T " & lastrow  

Range("B2:T" & lastrow).SpecialCells(xlCellTypeVisible).Select
With Selection.Font
    .Color = -16776961
    .TintAndShade = 0

enter image description here

enter image description here

Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
sephiroth
  • 43
  • 7
  • `row 58,59,60 is blank` Visually they may look blank but they may have data (spaces/non printable chars) are also data. Try this. Select the 3 cells and manually press delete. Then try your code. Does it still give you 60? – Siddharth Rout Apr 16 '21 at 05:08
  • Also try `Msgbox Len(Range("T60").Value)` See what does this give you – Siddharth Rout Apr 16 '21 at 05:09
  • Hi, i have tried `Msgbox Len(Range("T60").Value)` and the value is 0, also i clear content for the 3 cells, and now the last row become 57, is there possible for me to clear content for all blank cells first within the last row? – sephiroth Apr 16 '21 at 05:19
  • `also i clear content for the 3 cells, and now the last row become 57` So that means the cells were not empty :) `is there possible for me to clear content for all blank cells first?` Yes it is possible to clear the cells which just has blank spaces or just non printable characters. Store the values of that column in an array and then loop through the array and then use `Len(Trim())` to remove empty spaces. Finally write the data back to the column. – Siddharth Rout Apr 16 '21 at 05:23

2 Answers2

2

Another Last Row Issue

Intro

  • An empty cell is an empty cell.
  • A blank cell is either an empty cell, a cell containing a formula evaluating to "", a cell containing a ', etc. (if any).
  • If you want to select the non-blank cells, you have to use the xlValues parameter of the LookIn argument of the Find method. The parameter of this argument is saved each time you use the method. So by sheer chance, the accepted answer is working correctly since the parameter is set to xlValues (which is not the default value).
  • 'Proper' data does not contain blank rows at the bottom, so most often the xlFormulas parameter of the LookIn argument is the way to go.
  • In-depth studying of the Find method is crucial to revealing the countless possibilities.
  • The Find method may (will) fail if the worksheet is filtered and additionally may (will) fail with the xlValues parameter when rows are hidden (the xlFormulas parameter will handle hidden rows correctly).

Tip

  • This is a link to Siddharth Rout's legendary answer to the question Error in finding last used cell in Excel with VBA which is worth (highly recommended) studying as is the following.

The Examples

Option Explicit

' All four solutions assume that there are no hidden or filtered rows.

Sub testLastRowBeginner()
    
    Const FirstRow As Long = 2

    Dim lCell As Range
    Set lCell = Columns(20).Find("*", , xlValues, , , xlPrevious)
    
    ' Assuming that there is data at least in row 2 ('FirstRow') of the column.
    Dim LastRow As Long: LastRow = lCell.Row
    
    MsgBox "The Last Row of Data for Column T " & LastRow
    
    With Range("B" & FirstRow & ":T" & LastRow).SpecialCells(xlCellTypeVisible)
        With .Font
            .Color = -16776961
            .TintAndShade = 0
        End With
    End With
    
End Sub


Sub testLastRowIntermediate()
    
    Const FirstRow As Long = 2

    Dim lCell As Range
    
    ' For non-empty cells (most often):
    'Set lCell = Columns(20).Find("*", , xlFormulas, , , xlPrevious)
    
    ' For non-blank cells which is most probably your case,
    ' because you have formulas evaluting to "":
    Set lCell = Columns(20).Find("*", , xlValues, , , xlPrevious)
    
    Dim LastRow As Long
    If Not lCell Is Nothing Then
        LastRow = lCell.Row
        If LastRow < FirstRow Then
            MsgBox "The last row is smaller then the first.", _
                vbCritical, "Last Row Too Small"
        End If
    Else
        MsgBox "The column range is blank.", vbCritical, "No Last Cell"
        Exit Sub
    End If
    
    MsgBox "The Last Row of Data for Column T " & LastRow
    
    With Range("B" & FirstRow & ":T" & LastRow).SpecialCells(xlCellTypeVisible)
        With .Font
            .Color = -16776961
            .TintAndShade = 0
        End With
    End With
    
End Sub

Sub testLastRowAdvanced()
    
    Const First As String = "T2"

    With Range(First)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - .Row + 1) _
            .Find("*", , xlValues, , , xlPrevious)
        If lCell Is Nothing Then
            MsgBox "No data.", vbExclamation, "Fail"
            Exit Sub
        Else
            LastRow = lCell.Row
        End If
    End With
    
    MsgBox "The Last Row of Data for Column T " & LastRow
    
    With Range("B" & FirstRow & ":T" & LastRow).SpecialCells(xlCellTypeVisible)
        With .Font
            .Color = -16776961
            .TintAndShade = 0
        End With
    End With
    
End Sub

Sub testLastRowExpert()
    
    Const Cols As String = "B:T"
    Const FirstRow As Long = 2

    Dim rg As Range
    With Columns(Cols).Rows(FirstRow)
        Dim lCell As Range
        Set lCell = .Resize(.Worksheet.Rows.Count - FirstRow + 1) _
            .Find("*", , xlValues, , xlByRows, xlPrevious)
        If lCell Is Nothing Then
            'MsgBox "No data.", vbExclamation, "Failure"
            Exit Sub
        End If
        Set rg = .Resize(lCell.Row - .Row + 1)
    End With
    'Debug.Print rg.Address
    
    With rg.SpecialCells(xlCellTypeVisible)
        With .Font
            .Color = -16776961
            .TintAndShade = 0
        End With
    End With
    
End Sub
VBasic2008
  • 44,888
  • 5
  • 17
  • 28
0

You can use the VBA function find() to solve the problem using the following code

lastRow = ActiveSheet.Range("T:T").Find("*", searchOrder:= xlByRows, SearchDirection:= xlPrevious).Row

"*" means find any value, so it is possible to locate any cell with a value.

searchORder:= xlByRows means that the function will find the value row by row.

searchDirection:= xlPrevious means that the function will look from the end of the sheet to the beginning of the sheet.

So the entire function's parameter means that to find a cell with value in column T, row by row from the end of the sheet to the top of the sheet.

Use .Row attribute to obtain the row number, use +1 to find the empty row.

3LexW
  • 343
  • 2
  • 18
  • omg this works perfectly for my current data, thankss! , and i will test few more datas with this – sephiroth Apr 16 '21 at 06:25
  • @sephiroth: This is not the right answer. This will give you `60` again if there is a space in cell T60. Go ahead try it. It gave you 57 because you already cleared the cells as mentioned by you in the comments above ;) I have covered about finding the last row [HERE](https://stackoverflow.com/questions/11169445/error-in-finding-last-used-cell-in-excel-with-vba) which also talks about `.Find`. I did not suggest `.Find` delibrately because it doesnt meet your requirements :) – Siddharth Rout Apr 16 '21 at 07:03
  • @SiddharthRout I've tried the code with same data (original) without clear the content, and it detect the last row for Column T is `57`. Other than that, I tried on different data with similar blanks but different row, and it still works. Also, when I check the blanks by using Go to Special -> Blanks, the Cells for `58`, `59`, `60` still detect as Non-Blanks Cells, so I'm not really sure about this, – sephiroth Apr 16 '21 at 07:35
  • Go to cell T60. Press F2 and then press spacebar once. This will create a space in that cell. Press Enter and now try the above code :) @sephiroth – Siddharth Rout Apr 16 '21 at 07:36