View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.misc
Dave Peterson Dave Peterson is offline
external usenet poster
 
Posts: 35,218
Default Need a macro to hide certain columns

Option Explicit forces you to declare your variables. So if excel finds
something that looks like a variable and it's not declared, your code won't
compile and you'll be shown an error.

It may seem like more work, but if you've ever spent time trying to determine
why:

ctr1 = ctrll + 1
didn't work the way you hoped, you'll see the savings.
(one of those is ctr-one and the other is ctr-ELL).

Dallman Ross wrote:

In , Dave Peterson
spake thusly:

Maybe you can modify this macro to do what you want:

Option Explicit
Sub testme()

Dim iCol As Long

With Worksheets("sheet1")
For iCol = 1 To 30
If .Cells(1, iCol).Font.ColorIndex = 6 _
And .Cells(1, iCol).Interior.ColorIndex = 35 Then
.Columns(iCol).Hidden = True
Else
.Columns(iCol).Hidden = False
End If
Next iCol
End With
End Sub


That's excellent! I only had to modify it slightly.
It works perfectly! Thank you very much.

The color indexes weren't right, and I had to record a
dummy macro to see what the right ones were. (5 and 24.)
Also, my formatting starts on Row 2. So I changed the "1" cell
refs to "2". Very slick, Dave!

I saw another article here today with someone (you?)
showing how to run through the columns until a blank
is reached. Maybe I'll incorporate that in this instead
of just using my arbitrary "30" column-figure.

One question: what is the "Option Explicit" part for?

===========================
Dallman Ross wrote:

Short version: I want a macro to hide columns of a certain
cell color and font color.


[snip of "long version"]


Would someone be kind enough to help me with such a macro?

Here's some pseudo-code:

For nextcol in columns 1-30
If {data range} = Hex Val. #CCCCFF
If {data range} font color = #0000FF
Hide column
End If
End If
End of For-Loop

Dankeschoen!


--

Dave Peterson