View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
turbogreg17 turbogreg17 is offline
external usenet poster
 
Posts: 6
Default Hiding columns based on value

Rick, please help me get smarter (and let's just focus on the "on run"
macro). Let's say I wanted to add a third catagory such as "NA" to the mix
(i.e., Blank, "X", and "N/A").
Would the syntax for the "If UCase(Contents)...Then" line read...If
UCase(Contents) = "XXXXXXXXXX" 0r N/AN/AN/AN/AN/AN/AN/AN/AN/AN/A Then ?
(listing "N/A" tne times). (I tried it with tthat experimental syntax, but
it didn't work). Thanks for your patience and leason. Greg

"Rick Rothstein (MVP - VB)" wrote:

You didn't say which of the two routines I posted was the one you planned to
use, so here are both of them modified to run to the last filled in column
in Row 2...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim X As Long
Dim LC As Long
Dim Contents As String
' Find last used column in Row 2
LC = Cells(2, Columns.Count).End(xlToLeft).Column
With Target
If Not Intersect(Target, Range("E3", Cells(12, LC))) Is Nothing Then
For X = 3 To 12
Contents = Contents & Cells(X, .Column).Value
Next
If UCase(Contents) = "XXXXXXXXXX" Then .EntireColumn.Hidden = True
End If
End With
End Sub

Sub HideColumns()
Dim X As Long, Z As Long
Dim LastColumn As Long
Dim Contents As String
LastColumn = Cells(2, Columns.Count).End(xlToLeft).Column
For Z = 5 To LastColumn
Contents = ""
For X = 3 To 12
Contents = Contents & Cells(X, Z).Value
Next
If UCase(Contents) = "XXXXXXXXXX" Then
Cells(1, Z).EntireColumn.Hidden = True
End If
Next
End Sub

Rick
"turbogreg17" wrote in message
...
Rick,

Is there a way we can get your code to look at each column in the sheet
and
"run until done". My plan was to tweak the code each time more columns
were
added. I'm sure there is a way to look at row 2 (my heading row) to see if
new data (in other words a new column which may contain blanks or "x's"
has
beed added. Thanks in advance. Greg