繁体   English   中英

在用户定义的函数中使用“查找” - VBA

[英]Using “Find” in a user defined function - VBA

工作(这可能与问题有关):

适用于 Mac 的 Microsoft Excel,版本 15.41 (171205)

macOS High Sierra,版本 10.13.2

为了避免代码重复并使其更具可读性,我尝试定义自己的函数来定位包含列名的单元格。

它有 3 个参数:

headerName - 列的名称(例如“ID”、“Price”等)

sheetName - 要在其中搜索的工作表的名称

rowNum - 列标题所在的行号

这是我写的:

Function findCell(headerName, sheetName, rowNum)

Set cell = _
Sheets(sheetName).Rows(rowNum).Find(headerName,LookIn:=xlValues, _
Lookat:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, _
MatchCase:=True)

findCell = cell.Column

End Function

确切地说 - 我希望它返回单元格而不是单元格的列,我只是使用该列作为验证正确性的一种方式。

但是,这不适用于function (返回#VALUE! ),但由于某种原因,它确实适用于sub (使用msgbox输出)...

对我来说,以下代码有效

Function findCell(ByVal headerName As String, ByVal sheetName As String, ByVal rowNum As Long) As Long

Dim cell As Range

    Set cell = _
    Sheets(sheetName).Rows(rowNum).Find(headerName, LookIn:=xlValues, _
                                        Lookat:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, _
                                        MatchCase:=True)

    If cell Is Nothing Then
        findCell = 0
    Else
        findCell = cell.Column
    End If

End Function

编辑:只是一个疯狂的猜测,因为上述功能在 Windows 环境中工作。

Function findCell(ByVal headerName As String, ByVal sheetName As String, _
                  ByVal rowNum As Long) As Long

Dim cell As Range
Dim ws As Worksheet
Dim rg As Range

    Set ws = Sheets(sheetName)
    Set rg = ws.Rows(rowNum)

    '    Set cell = _
         '    rg.Find(headerName, LookIn:=xlValues, _
         '            Lookat:=xlWhole, SearchOrder:=xlRows, SearchDirection:=xlNext, _
         '            MatchCase:=False)

    Set cell = rg.Find(headerName, , xlValues, xlWhole, xlRows, xlNext, True)

    If cell Is Nothing Then
        findCell = 0
    Else
        findCell = cell.Column
    End If

End Function

EDIT2:另一个疯狂的猜测

Set cell = rg.Find(headerName, , &HFFFFEFBD, 1, 1, 1, True)

我希望这可以帮助你。 我使用此代码在选定的单元格字段中查找值为“IB”的单元格。 一旦找到“IB”,我将单元格的值更改为“BS”。 剪切该列,并将其插入另一个位置。 然后它迭代 I 并进行下一次搜索。

我敢肯定它并不优雅,但它可能会帮助您摆脱困境:

Cells.Select
Selection.Find(What:="IB", after:=ActiveCell, LookIn:=xlFormulas, lookat _
    :=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
    False, SearchFormat:=False).Select
Selection.Value = "BS"
MyCol = ActiveCell.Column
Columns(MyCol).Cut
Columns(i).Insert Shift:=xlToRight
i = i + 1

在旧版本的 macOS 和旧版本的 Microsoft Excel for Mac 上使用另一台 Mac 对其进行测试,结果相同(即使使用 @Storax 变体)。 测试我在 PC/Windows 上发布的代码实际上会起作用(如其他人的评论中所述)。

这让我得出结论,这个问题是 Mac 的 VBA 版本中的一个奇怪的错误。

我的解决方法 - 我没有定义function而是定义了一个使用全局变量的sub sub改编自@Storax 更好的代码:

Public findColOut As Long


Sub findCol(ByVal headerName As String, ByVal sheetName As String, ByVal rowNum As Long)

Dim cell As Range

Set cell = Sheets(sheetName).Rows(rowNum).Find(headerName,_
LookIn:=xlValues, Lookat:=xlWhole, SearchOrder:=xlRows,_
SearchDirection:=xlNext, MatchCase:=True)

If cell Is Nothing Then
   findColOut = 0
Else
   findColOut = cell.Column
End If

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM