3

我目前在为 Excel 2013 编程的宏中遇到关于读取隐藏列的问题。我正在尝试将 A 列用作一行唯一键,以允许我快速开发基于 A 列中的键值隐藏和显示行的逻辑。当我出于视觉目的在工作表中手动隐藏 A 列时,我就是无法从该列读取,也就是我的代码返回错误。如果我显示该列,则代码将清楚地执行。在此先感谢您的帮助!

Public Sub hideRow(findId As String, sheetName As String)
    Dim lastRow As Long
    Dim foundCell As Range
    Dim hideThisRowNum As Integer

    'Get Last Row
    lastRow = Worksheets(sheetName).Range("A" & Rows.Count).End(xlUp).Row

    'Find ID
    With Worksheets(sheetName).Range("a1:a1000") 'This needs to be A1 to AxlDown
        Set foundCell = Worksheets(sheetName).Range("A1:A" & lastRow).Find(What:=findId,     LookIn:=xlValues, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
    End With

    'Get Row # to Hide

    hideThisRowNum = Val(foundCell.Row)

    'Hide row
    Worksheets(sheetName).Rows(hideThisRowNum).Hidden = True

    'Set Add To Action Plan = No
    Worksheets(sheetName).Range("G" & hideThisRowNum).Value = "No"

End Sub
4

1 回答 1

6

问题出在 .Find() 调用中。使用LookIn:=xlValues不会找到隐藏的单元格。将其更改为LookIn:=xlFormulas,它应该可以工作。

于 2013-08-20T15:32:43.983 回答