![](/img/trans.png)
[英]Find the LAST blank cell (column) and last non-blank row to enter the NOW() formula
[英]Find last row of column without including blank cells
我的代碼有問題。 在數據中有60
行,在T
列中,有57
行具有值,即非空白。 單元60
58
是59
的。 我試圖找到 Column T
的最后一行。 每當我運行代碼時,它總是給我60
而不是57
。
這是我的代碼
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
介紹
""
的公式的單元格、包含'
的單元格等(如果有)。Find
方法的LookIn
xlValues
。 每次使用該方法時都會保存此參數的參數。 因此,由於參數設置為xlValues
(這不是默認值),因此完全有機會接受的答案正常工作。LookIn
參數通常是xlFormulas
的方式。Find
方法對於揭示無數可能性至關重要。Find
方法可能(將會)失敗,並且在隱藏行時可能(將會)因xlValues
參數而失敗( xlFormulas
參數將正確處理隱藏的行)。小費
例子
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
您可以使用 VBA function find()
使用以下代碼解決問題
lastRow = ActiveSheet.Range("T:T").Find("*", searchOrder:= xlByRows, SearchDirection:= xlPrevious).Row
"*"
表示查找任何值,因此可以找到具有值的任何單元格。
searchORder:= xlByRows
表示 function 將逐行查找值。
searchDirection:= xlPrevious
表示 function 將從工作表的末尾查找到工作表的開頭。
所以整個函數的參數意味着要在T列中找到一個有值的單元格,從工作表的末尾到工作表的頂部逐行查找。
使用.Row
屬性獲取行號,使用 +1 查找空行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.