LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #10   Report Post  
Posted to microsoft.public.excel.misc
external usenet poster
 
Posts: 35,218
Default Need a macro to hide certain columns

You'll want to change this line:
iLastCol = Cells.SpecialCells(xlCellTypeLastCell).Column
to
iLastCol = .Cells.SpecialCells(xlCellTypeLastCell).Column

This extra dot means that excel will use the correct worksheet
(With Worksheets("2006 Realized Gains"))

Another way is if you can pick out a row that always has data:

With Worksheets("2006 Realized Gains")
iLastCol = .cells(1,.columns.count).end(xltoleft).column
...

I used row 1 to determine the last used column.

==
Sometimes excel's lastusedcell won't be what you expect it to be.

Or visit Debra Dalgleish's site for some techniques for resetting that
lastusedcell.
http://www.contextures.com/xlfaqApp.html#Unused




Dallman Ross wrote:

Dave Peterson suppled a nice, easy macro for me last week.
I turned it into a toggle button, which I like. I asked
a follow-up about finding the last column in the VBA code
rather than hard-coding a number. (I'd used 30 as the max
column value in my example, and Dave had gone with that.)

I'm certain this is entirely easy for the experienced VBA coders,
but because my VBA-fu is weak, it left me baffled. I
left things for a few days with the hard-coded 30. Occasionally
I would try to emend the code myself, but my efforts proved
fruitless. Well, today I succeeded. It's not hard, but
I just needed the right syntax.

Option Explicit
Private Sub ToggleButton1_Click()

Dim iCol As Long

'I just added this:
Dim iLastCol As Long

With Worksheets("2006 Realized Gains")

'and I just added this:
iLastCol = Cells.SpecialCells(xlCellTypeLastCell).Column

'and in the next line, now use "iLastCol" instead of "30":
For iCol = 1 To iLastCol

If .Cells(2, iCol).Font.ColorIndex = 5 _
And .Cells(2, iCol).Interior.ColorIndex = 24 Then
.Columns(iCol).Hidden = ToggleButton1.Value
Else
.Columns(iCol).Hidden = False
End If
Next iCol

'this is just for looks when I'm done
Range("A1").Select

End With
End Sub

(Thanks again, Dave!)

I found the needed syntax via Google, he
http://www.parry.co.nz/findlastrowor...%20a%20 Sheet

Dallman

=============================================
On 15 October 2006 in , Dallman
Ross <dman@localhost. spake thusly:

In response to my original --

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


Dave Peterson had replied as follows:


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



As I posted already, this is great and solved my problem. Now
I'm trying to take it a bit further. This is an early foray
for me into the world of VBA macros. I'm stuck on a couple of
new ideas.

Okay, I decided it would be cool to make a button (control box)
to toggle my hidden fields on and off. I'd never made a button
like that before, though I've been using Excel for years. Well,
I did a Google search and found this excellent page:

http://www.vbaexpress.com/kb/getarticle.php?kb_id=416

I converted the above code of our Dave's to a toggle button. It
now looks like so:

Option Explicit

Private Sub ToggleButton1_Click()

Dim iCol As Long

With Worksheets("2006 Realized Gains")
For iCol = 1 To 30
If .Cells(2, iCol).Font.ColorIndex = 5 _
And .Cells(2, iCol).Interior.ColorIndex = 24 Then

.Columns(iCol).Hidden = ToggleButton1.Value
Else
.Columns(iCol).Hidden = False
End If
Next iCol
End With
End Sub



Well, that works, and I think it's slick! (Setting the
.Hidden value to ToggleButton1.Value was my idea. If I
set the "Else" value to "Not ToggleButton1.Value", that's
kind of fun also: as you probably see already, the toggle
then is back and forth between hidden and unhidden columns.

I actually would find it useful (or at least loads of fun!)
to have a three-way toggle: (a) show the regular (normally
unhidden) cols; (b) show only the regularly hidden cols; and
(c) show all cols.

In order to do that, I suppose one way is to examine some
state or other at the top of the Else region. I'm stuck
for how, though. Ideas gladly entertained!

The next wish for improvement is to use something like Bob
Phillips shows in a concurrent article in this group,
Message-ID: :

Do While Activecell.Offset(0,-1).Value < ""
'do some stuff
Activecell.Offset(1,0).Select
Loop

I see what he's doing, but I am too weak on actual VBA
code to have yet been able to figure out how to incorporate
that concept into the above. In other words, lose the
"For 1-30" loop I have and replace it with a Do-While
loop.

Thanks for further suggestions!

Dallman Ross


--

Dave Peterson


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
error when running cut & paste macro Otto Moehrbach Excel Worksheet Functions 4 August 9th 06 01:49 PM
How to program a macro to hide rows with conditionnal formating Turquoise_dax Excel Discussion (Misc queries) 5 June 20th 06 04:33 PM
insert columns macro is putting 2 columns instead of 1 AGH Excel Worksheet Functions 2 February 27th 06 02:36 PM
Macro to hide blank cells in a range Dave Excel Discussion (Misc queries) 1 February 1st 06 11:55 PM
Removing columns (not hide or white-out). TrishDB Excel Worksheet Functions 8 April 28th 05 01:24 AM


All times are GMT +1. The time now is 06:52 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"