我目前正在为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
最佳答案
问题出在.Find()调用中。使用LookIn:=xlValues
不会找到隐藏的单元格。将其更改为LookIn:=xlFormulas
,它应该可以工作。