Solved

EXCELL VBA cell reference

Posted on 2013-01-15
4
373 Views
Last Modified: 2013-01-24
I am sure that this is an easy question, so here goes.

Currently in VBA i can reference a cell as (useing cell H8 as an example)

[H8]

Open in new window


or as
ActiveSheet.Range("H8").Text

Open in new window


However if i insert a column before column H then all the VBA's go out of whack.

Is there a way that i can name the columns and then refer to them by their name so that a column add/remove isnt breaking all the scripts? if it helps each column in row 1 holds the Unique identifyer for that column (Name, Address, Phone, etc....)

Cheers for any solutions.
0
Comment
Question by:Andrew Davis
4 Comments
 
LVL 18

Accepted Solution

by:
krishnakrkc earned 200 total points
ID: 38781510
You can use

MsgBox Range("Name").Column >> returns Column Number
MsgBox Range("Name").Address >> returns Range Address

where "Name" is a name range.

Kris
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 150 total points
ID: 38781591
try this

Sheets("Sheet1").Range("Employees").Cells(4, 1).Text

refer
http://stackoverflow.com/questions/11966342/vba-excel-rename-columns-for-easy-referencing
0
 
LVL 24

Assisted Solution

by:Steve
Steve earned 150 total points
ID: 38781612
You can find the column in which the word "Name" first appears and use that in the range/cell refs:

NameColumn = ActiveSheet.Cells.Find(What:="Name", After:=ActiveSheet.Range("A1"), SearchOrder:=xlByColumns, SearchDirection:=xlNext).Column

ActiveSheet.Cells(8,NameColumn).Text ' will reference row 8 in the name Column

Open in new window

0
 
LVL 18

Author Closing Comment

by:Andrew Davis
ID: 38817100
Hey guys thanks for your help. All answers were good and correct, got something from each of them, Split points, with slightly more for first answer, for no other reason than Kris was the quickest to post ;)

Cheers.
Andrew
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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.
The viewer will learn how to simulate a series of coin tosses with the rand() function and learn how to make these “tosses” depend on a predetermined probability. Flipping Coins in Excel: Enter =RAND() into cell A2: Recalculate the random variable…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

758 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

19 Experts available now in Live!

Get 1:1 Help Now