Hello All,
I have searched the forums and Google and found inconclusive or merely partial implementations to hiding a range of columns (or rows) using R1C1 format in Excel VBA. The goal of this code is to have a set of rows hidden depending on the result read-in by a Case statement. The Case statement works well on its own, so for the sake of clarity and simplification it has been removed from the code below.
Using Excel 2010, the following code portion is from my original, working version that would need to be changed manually for a moving range:
Option Compare Text ' Lets: A=a, B=b, ... Z=z
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Define Variables
' ========================
Dim WS01__sheet As Worksheet
'....
'....
' Hide Columns:
'------------------
WS01__sheet.Columns("N:Y").EntireColumn.Hidden = True
WS01__sheet.Columns("Z").EntireColumn.Hidden = True
End Sub
To achieve this, I have attempted the following:
Option Compare Text ' Lets: A=a, B=b, ... Z=z
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Define Variables
' ========================
Dim Column_set_01__orig As Integer
Dim WS01__sheet As Worksheet
'Define Variables:
'--------------------
Column_set_01__orig = 10
'....
'....
' Hide Columns:
'------------------
'Attempt 1:
WS01__sheet.Range(Cells(1, (Column_set_01__orig + 4)), Cells(1, (Column_set_01__orig+ 16))).EntireColumn.Hidden = True
'Attempt 2: (non-preferred method)
' WS01__sheet.Range(Cells(1, (Column_set_01__orig + 4)), Cells(1, (Column_set_01__orig + 16))).Select
' Selection.EntireColumn.Hidden = True
End Sub
May I please get some assistance with this issue? I am not sure what is being done incorrectly, but I would also like to know why each method does not work, instead of only a quick fix so I can learn.
From what I understand, using ".select" is wasted computation time, particularly if this gets to be a rather large, actively running project.
Thanks you all in advance.
Bookmarks