Solved

Revise VBA code to include variables

Posted on 2015-01-29
4
94 Views
Last Modified: 2015-01-29
Hello Experts,

I am currently using this code.

Option Explicit

Sub EmpHideCol()
    Columns("J:W").EntireColumn.Hidden = True
End Sub
Sub EmpShowCol()
    Columns("J:W").EntireColumn.Hidden = False
End Sub

Open in new window


While I've been working on my workbook, I've needed to change the column references above several times because I keep changing the layout of my design.

I am hoping someone could revise both of my macro's, so that where is says "J:W" - the code then looks to a variable up ?above? that holds the value (IE: J:W)

Does that make sense?

Thank you in advance for your help!
0
Comment
Question by:Geekamo
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 250 total points
ID: 40578934
How about storing the columns you are working with in a Constant:
Option Explicit

Const HiddenColumns As String = "J:W"

Sub EmpHideCol()
    Columns(HiddenColumns).EntireColumn.Hidden = True
End Sub
Sub EmpShowCol()
    Columns(HiddenColumns).EntireColumn.Hidden = False
End Sub

Open in new window

0
 
LVL 46

Assisted Solution

by:Martin Liss
Martin Liss earned 250 total points
ID: 40578961
And you could do with one less macro. This way it's a toggle; if they're visible it hides them and if they're hidden it unhides them.

Option Explicit

Const HiddenColumns As String = "J:W"

Sub EmpHideCol()
    Columns(HiddenColumns).EntireColumn.Hidden =  Not Columns(HiddenColumns).EntireColumn.Hidden
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:Geekamo
ID: 40578982
@ byundt -

When you say put them in a Constant, does that mean a variable?  It appears to be doing what a variable would do, I'm just unsure what "Constant" means (other then the obvious meaning) - is it any different then a normal variable?

(Sorry for the stupid question!)

@ Martin Liss -

The toggle, it's beautiful! :)

I will be using this code, which is 'slightly' different than both of your solutions.
Option Explicit

Const HiddenColumns As String = "J:W"

Sub EmployeesShowAndHideColumns()
Columns(HiddenColumns).Hidden = Columns(HiddenColumns).Hidden = False
End Sub

Open in new window


Thank you both for your solutions! :)
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40578995
Internally in Excel, variables and constants are just addresses to a value in memory. A variable can be changed in code and a constant, as you might guess from it's name, is constant and can not be changed in code.

You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http:/Q_27311462.html), I created an alternative function to the Excel Concatenate() function that you might find useful.  I tested several solutions and share the results in this article as well as t…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now