Tuesday, 20 August 2013

Issue reading hidden column using VBA in Excel 2013

Issue reading hidden column using VBA in Excel 2013

I am currently having issues with a Macro I am programming for Excel 2013
regarding reading hidden columns. I am trying to utilize Column A as a row
of unique keys to allow me to quickly develop logic that hides and shows a
row based on the key value in column A. When I hide column A manually in
the sheet for visual purposes I am then unable to read from that column,
aka my code returns an error. If I show the column the code executes
clearly. Thanks in advance for the help!
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

No comments:

Post a Comment