[RESOLVED] Excel VBA: Macro to hide columns not working
I have a macro which is supposed to provide a view of my database, however, when I run the macro, it will not work.
What I'm trying to get the macro to do is hide the following columns:
but when I run the macro it hides G through M, and O through Z. I have tried to break the macro down into several smaller macros and "call" them in a master macro for that series of macros, but I continue to get the same results.
Here is the master macro:
Code:
Sub View_Fiscal()
'
' Macro1 Macro
'
Application.ScreenUpdating = False
Call Unhide_Contra_Info
Call VF_1
Call VF_2
Call vf_3
Range("a1").Select
End Sub
Private Sub VF_1()
Columns("G:M").Select
Selection.EntireColumn.Hidden = True
Range("a1").Select
End Sub
Private Sub VF_2()
Columns("P:P").Select
Selection.EntireColumn.Hidden = True
Range("a1").Select
End Sub
Private Sub vf_3()
Columns("T:Z").Select
Selection.EntireColumn.Hidden = True
Range("a1").Select
End Sub
Re: Excel VBA: Macro to hide columns not working
if i comment out the call to "contra..." it does exactly what you are trying to do.
Re: Excel VBA: Macro to hide columns not working
But you don't need to select the columns first, so more like this:
Code:
Sub hideCols()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range("g1:m1").EntireColumn.Hidden = True
ws.Range("p1").EntireColumn.Hidden = True
ws.Range("t1:z1").EntireColumn.Hidden = True
Set ws = Nothing
End Sub
Re: Excel VBA: Macro to hide columns not working
Quote:
Originally Posted by
vbfbryce
But you don't need to select the columns first, so more like this:
Code:
Sub hideCols()
Dim ws As Worksheet
Set ws = ActiveSheet
ws.Range("g1:m1").EntireColumn.Hidden = True
ws.Range("p1").EntireColumn.Hidden = True
ws.Range("t1:z1").EntireColumn.Hidden = True
Set ws = Nothing
End Sub
Bryce you work to hard! :wave:
Code:
ActiveSheet.Range("g:m, p:p, t:z").EntireColumn.Hidden = True
Or is it that you get paid by the line? :p
Re: Excel VBA: Macro to hide columns not working
I DO get paid by the line item (when doing "project management" here at work!)!
:thumb:
Re: Excel VBA: Macro to hide columns not working
Thanks guys, I'm not sure why my original code wasn't working, I thought they were doing the same thing... do you have any idea why that was?? My original code was performing an entirely different operation than what I was calling for. Maybe it was because there was no Dim'ing?
Either way, I married the two of your codes together, ran it, and it works perfectly.
Here's what I used:
Code:
Sub hidecols()
Dim ws As Worksheet
Set ws = ActiveSheet
Application.ScreenUpdating = False
Call Unhide_Contra_Info
ActiveSheet.Range("g:m,p:p,t:z").EntireColumn.Hidden = True
Range("a1").Select
End Sub
Thanks again.
Re: [RESOLVED] Excel VBA: Macro to hide columns not working
The this useful example file for the topic can be reviewed : Excel Vba Column Hiding-Unhiding With Horizontal Form
https://www.youtube.com/watch?v=ikg8IR7Sz_4
In this template userform, module, class module was used.
Source for example file