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

问题描述 投票:1回答:3

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

Microsoft Excel for Mac,版本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输出)...

excel vba excel-vba find excel-vba-mac
3个回答
1
投票

对我来说,以下代码有效

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)

0
投票

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

我确信它不优雅,但它可能会帮助你解开:

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

0
投票

在较旧版本的macOS上使用另一台Mac进行测试,使用较早版本的Microsoft Excel for Mac进行测试(即使使用@Storax变体)也是如此。测试我在PC / Windows上发布的代码实际上是可行的(如其他人的评论中所述)。

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

我的解决方法 - 我没有定义function,而是定义了一个使用全局变量的subsub改编自@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
© www.soinside.com 2019 - 2024. All rights reserved.