Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Revise VBA code to include variables

Posted on 2015-01-29
4
Medium Priority
?
101 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 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Windows Explorer let you handle zip folders nearly as any other folder: Copy, move, change, and delete, etc. In VBA you can also handle normal files and folders, but zip folders takes a little more - and that you'll find here.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

927 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