Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Revise VBA code to include variables

Posted on 2015-01-29
4
Medium Priority
?
100 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 81

Accepted Solution

by:
byundt earned 1000 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 49

Assisted Solution

by:Martin Liss
Martin Liss earned 1000 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 49

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

705 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