Solved

EXCELL VBA cell reference

Posted on 2013-01-15
4
377 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

679 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