簡體   English   中英

查找列的最后一行而不包括空白單元格

[英]Find last row of column without including blank cells

我的代碼有問題。 在數據中有60行,在T列中,有57行具有值,即非空白。 單元60 5859的。 我試圖找到 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

在此處輸入圖像描述

在此處輸入圖像描述

另一個最后一行問題

介紹

  • 空單元格是空單元格。
  • 空白單元格是空單元格、包含計算結果為""的公式的單元格、包含'的單元格等(如果有)。
  • 如果要 select 非空白單元格,則必須使用Find方法的LookIn xlValues 每次使用該方法時都會保存此參數的參數。 因此,由於參數設置為xlValues (這不是默認值),因此完全有機會接受的答案正常工作。
  • “正確”數據的底部不包含空白行,因此 LookIn 參數的LookIn參數通常是xlFormulas的方式。
  • 深入研究Find方法對於揭示無數可能性至關重要。
  • 如果工作表被過濾, Find方法可能(將會)失敗,並且在隱藏行時可能(將會)因xlValues參數而失敗( xlFormulas參數將正確處理隱藏的行)。

小費

  • 是 Siddharth Rout 對問題Error in find last used cell in Excel with VBA問題的傳奇答案的鏈接,值得(強烈推薦)研究如下。

例子

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM