【问题标题】:vba access function for extract cell value用于提取单元格值的 vba 访问函数
【发布时间】:2015-03-27 09:22:27
【问题描述】:

我正在尝试编写一个函数,用于提取某个列的第一个先前的 valorized 单元格的单元格字符串值。

我试图更好地解释: 我有一个女巫专栏,并非所有的单元格都包含相同的值。 因此,如果我想构建一个接受参数 1 单元格的函数,例如'A5'。 如果进入 A5 单元格没有任何值,它会检查前一个单元格(A4)是否有一些值。如果再次失败,它会递归返回(A3..A2..A1)直到找到东西。

Public Function getPreviousValorizedCellValue(ByVal cell As Range) As String
If (cell.Value = "") Then
    Set cell = cell.Offset(-1, 0)
    getPreviousValorizedCellValue (cell)
Else: getPreviousValorizedCellValue = cell.Value
End If
End Function

它不起作用。 Excel给我错误

有什么想法吗?

【问题讨论】:

    标签: vba excel


    【解决方案1】:

    试试这个:

    Public Sub TestgetPrevious()
        Dim cell As Range
    
        Set cell = ActiveSheet.Range("A5")
    
        MsgBox getPreviousValorizedCellValue_v2(cell)
    End Sub
    
    
    Public Function getPreviousValorizedCellValue_v2(ByVal cell As Range) As String
        Debug.Print cell.Address
    
        If cell.Row <= 0 Then Exit Function
    
        If (Trim(cell.Value = "")) Then
            If cell.Row > 1 Then
                Set cell = cell.Offset(-1, 0)
    
                getPreviousValorizedCellValue_v2 = getPreviousValorizedCellValue_v2(cell)
            Else
                getPreviousValorizedCellValue_v2 = ""
            End If
        Else
            getPreviousValorizedCellValue_v2 = cell.Value
        End If
    
    End Function
    

    【讨论】:

    • 很高兴我能帮上忙。你几乎明白了。缺少的主要内容是:“getPreviousValorizedCellValue = getPreviousValorizedCellValue(cell)”
    • 该死!明显不见了!再次感谢。
    【解决方案2】:

    试试下面的代码。

    Function Foo(iRange As Range)
    
    For i = iRange.Row To 1 Step -1
        If Trim(Range("A" & i).Value) <> "" Then
            Foo = Range("A" & i).Value 'Put the code you want here
            Exit Function
        End If
    Next i
    
    End Function
    

    【讨论】:

    • 我想要一个函数,它返回代表第一个 valorized 前一个单元格的值的字符串。不是程序。并且该单元格应作为 ByVal 单元格传递。
    • @mastro:那你自己把这段完全够用的代码放到一个函数中吧!
    • 感谢@Jean-FrançoisCorbett。 :) 无论如何都已经编辑了代码以使 mastro 受益
    • @mastro 已将程序修改为函数 :)
    • Public Function getPreviousValorizedCellValue2(ByVal cell As Range) As String For i = cell.row To 1 Step -1 If Cells(i, cell.column).Value "" Then getPreviousValorizedCellValue2 = cell. Value End If Next i End Function Excel 现在告诉我无法应用该函数,因为我传递了一个空单元格。
    猜你喜欢
    • 1970-01-01
    • 2017-09-24
    • 2020-08-19
    • 1970-01-01
    • 2021-11-27
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多