Solved

Revise VBA code to include variables

Posted on 2015-01-29
4
93 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 80

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 45

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 45

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now