Is this code the best way to do it?

jeffreymb

Board Regular
Joined
Aug 22, 2005
Messages
119
This code will hide certain columns if they are shown and show them if they are hidden.

Is this the best way to do it? Or is there a simpler way?

<font face=Courier New>
<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> Show_Hide_Notes_Click()
<SPAN style="color:#00007F">Dim</SPAN> ActCell <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">String</SPAN>
Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN>
ActCell = ActiveCell.Address
<SPAN style="color:#00007F">If</SPAN> Range("D:F").Columns.Hidden = <SPAN style="color:#00007F">False</SPAN> <SPAN style="color:#00007F">Then</SPAN>
    Columns("D:F").Select
    Selection.EntireColumn.Hidden = <SPAN style="color:#00007F">True</SPAN>
    Show_Hide_Notes.Caption = "Show Notes"
    Show_Hide_Notes.BackColor = &H8000&
    Show_Hide_Notes.ForeColor = &HFFFFFF
    
<SPAN style="color:#00007F">Else</SPAN>
    Columns("D:F").Select
    Selection.EntireColumn.Hidden = <SPAN style="color:#00007F">False</SPAN>
    Show_Hide_Notes.Caption = "Hide Notes"
    Show_Hide_Notes.BackColor = &HFFFF&
    Show_Hide_Notes.ForeColor = &H0&
    
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN>
Range(ActCell).Select
Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>
</FONT>
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
I don't see any problems with it. :)

Although, you don't have to select the columns. This works just the same, without having to lose the current selection:

Code:
Private Sub Show_Hide_Notes_Click()

Application.ScreenUpdating = False

If Range("D:F").Columns.Hidden = False Then
    Columns("D:F").EntireColumn.Hidden = True
    With Show_Hide_Notes
        .Caption = "Show Notes"
        .BackColor = &H8000&
        .ForeColor = &HFFFFFF
    End With
Else
    Columns("D:F").EntireColumn.Hidden = False
    With Show_Hide_Notes
        .Caption = "Hide Notes"
        .BackColor = &HFFFF&
        .ForeColor = &H0&
    End With
End If

Application.ScreenUpdating = True

End Sub
 
Upvote 0

Forum statistics

Threads
1,207,096
Messages
6,076,554
Members
446,213
Latest member
bettigb

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top