Solved

Revise VBA code to include variables

Posted on 2015-01-29
4
97 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 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 47

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 47

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

756 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