首页 文章

Excel VBA用于确定列中的最后一个非值(IE可能有公式但没有值)

提问于
浏览
2

我有一个列在每个行字段中都有一个公式 . 该公式传播来自另一个excel spreasheet的数据 . 但是,如果行字段中没有任何内容,则该行仍为空白 .

我在谷歌上找到了许多例子来获取列中的最后一行 . 但是,它们失败了,因为它们将公式检测为包含其中某些内容的行 . 那讲得通 . 但是,如何才能获取忽略公式的列中的最后一行,并且只尝试检测列字段中的值?

我目前正在使用两种方法来搜索列失败的最后一行:

Function lastRowA(rngInput As Range) As Variant
    Dim WorkRange As Range
    Dim i As Integer, CellCount As Integer
    Application.Volatile
    Set WorkRange = rngInput.Rows(1).EntireRow
    Set WorkRange = Intersect(WorkRange.Parent.UsedRange, WorkRange)
    CellCount = WorkRange.Count
    For i = CellCount To 1 Step -1
        If Not IsEmpty(WorkRange(i)) Then
            lastRowA = WorkRange(i).Value
            Exit Function
        End If
    Next i
End Function

function lastRow(column as string, optional plusOne as boolean)
    If (plusOne = False) then
        plusOne=False
    End If

    if (plusOne = False) Then
        lastRow = Replace(Range(column & "65536").End(xlUp).Address, "$", "")
    Else
        lastRow = Range(column & "65536").End(xlUp).Address
        lastRow = Cells(lastRow)
        ' Replace(, "$", "")
    End If
End Function

3 回答

  • 2

    如果要查找包含非空值的最后一行(由公式生成或输入常量),请尝试此操作

    Sub FindLastValue()
    Dim jLastRow As Long
    jLastRow = ActiveSheet.Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    End Sub
    
  • 2

    这是一种查找不包含公式的列中最后一个单元格的简单方法 . 如果没有没有公式的单元格,它将为0 .

    Sub Test()
    
    Dim i As Long, tempLast As Long, lastRow As Long
    tempLast = Range("A" & Rows.Count).End(xlUp).Row
    
    For i = tempLast To 1 Step -1
        If Len(Cells(i, 1)) <> 0 Then
            If Not Cells(i, 1).HasFormula Then
                lastRow = i
                Exit For
            End If
        End If
    Next
    
    MsgBox lastRow
    End Sub
    

    请注意,您应该使用“rows.count”而不是65536,因为它不再是较新版本的Excel中的最后一行 . 无论版本还是用户设置,Rows.count都可以正常工作 . 还应该避免使用范围,因为有一个奇怪的错误,你需要刷新usedrange或你会得到错误的结果 .

  • 3

    我从来没有做过这样的事情,但它似乎对于相当大的区域正常和快速地工作 . 即使您说列是所有公式,这也会考虑值和公式的混合,因此外部循环逐步跨越区域:

    Function GetLastFormulaBlank(rngInput As Excel.Range) As Excel.Range
    
    Dim rngFormulas As Excel.Range
    Dim rngArea As Excel.Range
    Dim CellCounter As Long
    Dim AreaCounter As Long
    Dim varAreaCells As Variant
    Dim rngLastFormulaBlank As Excel.Range
    
    Set rngFormulas = rngInput.SpecialCells(xlCellTypeFormulas)
    For AreaCounter = rngFormulas.Areas.Count To 1 Step -1
        Set rngArea = rngFormulas.Areas(AreaCounter)
        varAreaCells = rngArea.Value2
        If IsArray(varAreaCells) Then
            For CellCounter = UBound(varAreaCells) To LBound(varAreaCells) Step -1
                If varAreaCells(CellCounter, 1) = "" Then
                    Set rngLastFormulaBlank = rngArea.Cells(CellCounter)
                    Exit For
                End If
            Next CellCounter
        Else
            If varAreaCells = "" Then
                Set rngLastFormulaBlank = rngArea.Cells(1)
            End If
        End If
        If Not rngLastFormulaBlank Is Nothing Then
            Exit For
        End If
    Next AreaCounter
    
    Set GetLastFormulaBlank = rngLastFormulaBlank
    End Function
    

    你这样称呼它:

    Sub test()
    Dim rngLastFormulaBlank As Excel.Range
    
    Set rngLastFormulaBlank = GetLastFormulaBlank(ActiveSheet.Range("A:A"))
    If Not rngLastFormulaBlank Is Nothing Then
        MsgBox rngLastFormulaBlank.Address
    Else
        MsgBox "no formulas with blanks in range"
    End If
    End Sub
    

相关问题