Solved

CodeName reference

Posted on 2013-12-10
6
167 Views
Last Modified: 2013-12-13
I need to reference the code name of  a sheet instead of the name.

Right now I am using
Set Mysheet = mybook.sheets("ABD")

How do I change to reference Sheet1 which is the code name and use in a range?
0
Comment
Question by:leezac
6 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 39710094
What is mybook?

How do you want to use the codename?

Where is the code located?
0
 

Author Comment

by:leezac
ID: 39710105
mybook = application.activeworkbook

code is located in a module

The name of the sheet is going to change every month so I need to be able to code using the code name because the sheet name will change.

To be able to select range starting at B5 on Sheet1 using codename not tab name.

__________________________________________________________

or something like this, but be able to use code name not "Data"

Set ActWks = ActiveSheet

Sheets("Data").Select

ActWks.Select


I have not created code yet, but trying to figure out name part first.
0
 
LVL 33

Expert Comment

by:Norie
ID: 39710135
if the code is in the same workbook as the sheet you want to refer to then you can just use this.
Set Mysheet = Sheet1

Open in new window


You can then use Mysheet just as you normally would.
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 81

Accepted Solution

by:
byundt earned 500 total points
ID: 39710411
You might use code like this:
Dim wsData As Worksheet
Set wsData = Sheet1        'or perhaps ActiveWorkbook.Sheet1

Open in new window


The worksheet codename is good to use when the user might change the name of the worksheet, but the same worksheet will be present in the workbook at both design time (when you write the code) and run time (when the user runs the macro).

Based on your description ("the name of the sheet is going to change every month"), I am guessing that a new worksheet is being created or is being imported.  If so, you won't be able to count on the worksheet having the same codename every month.

As an alternative, if the worksheet is always in the same position (first sheet, second sheet, etc.) in the workbook, then you can identify it that way.
Dim wsData As Worksheet
Set wsData = ActiveWorkbook.Worksheets(1)            'First worksheet in workbook

Open in new window

0
 
LVL 4

Expert Comment

by:yuppydu
ID: 39710701
When you say that the name of the sheet changes, are you referring to the name appearing in the tab at the bottom?
0
 
LVL 33

Expert Comment

by:Rob Henson
ID: 39710785
How about creating a named range in a cell on that sheet, eg "DATA_TAB"

Then use:

Application.Goto Reference:="DATA_TAB"

As the Sheet name changes so will the reference for named range but macro doesn't need to change.

Thanks
Rob H
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

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…
This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
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…
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

808 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