Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to reference column by name

Hi,

I want the following behaviour:

if the value of a cell changes, then I check if the column the cell is
in is a certain column, if yes then I do some calculation.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 Then
...
End If
End Sub

Question: How can I reference the queried column by name rather than by
number as above?

Thanks!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to reference column by name

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("F:F")) Is Nothing Then
....
End If
End Sub

or

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("columnF")) Is Nothing Then
....
End If
End Sub

where columnF is a range name

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Scott Steiner" wrote in message
. ..
Hi,

I want the following behaviour:

if the value of a cell changes, then I check if the column the cell is
in is a certain column, if yes then I do some calculation.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 6 Then
...
End If
End Sub

Question: How can I reference the queried column by name rather than by
number as above?

Thanks!



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default How to reference column by name

Bob Phillips schrieb:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("F:F")) Is Nothing Then
...
End If
End Sub


This worked fine, but I need something else. I would like to reference
my column by "title" or "heading" i.e. what is written in row 1. Or
maybe there is even a way to define a unique name for each column
regardless of what is written in row 1, and then reference that column
by that unique name, that would be best!


or

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("columnF")) Is Nothing Then
...
End If
End Sub

where columnF is a range name


I tried replacing "columnF" by what is written in my column in row 1,
but that didn't work. So I guess what I replaced wasn't a valid range name.
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default How to reference column by name

I feared you might mean that <vbg.

Try this version

Private Sub Worksheet_Change(ByVal Target As Range)
Dim iCol As Long
On Error Resume Next
iCol = Application.Match("column_label", Rows("1:1"), 0)
On Error GoTo 0
If iCol 0 Then
If Target.Column = iCol Then
....
End If
End If
End Sub


--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)

"Scott Steiner" wrote in message
. ..
Bob Phillips schrieb:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("F:F")) Is Nothing Then
...
End If
End Sub


This worked fine, but I need something else. I would like to reference
my column by "title" or "heading" i.e. what is written in row 1. Or
maybe there is even a way to define a unique name for each column
regardless of what is written in row 1, and then reference that column
by that unique name, that would be best!


or

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("columnF")) Is Nothing Then
...
End If
End Sub

where columnF is a range name


I tried replacing "columnF" by what is written in my column in row 1,
but that didn't work. So I guess what I replaced wasn't a valid range

name.


  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default How to reference column by name

Assume row of the target of interest contains the name/string ABCD

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Columns.count 1 then exit sub
If Cells(1,Target.Column) = "ABCD" then


--
Regards,
Tom Ogilvy

"Scott Steiner" wrote in message
. ..
Bob Phillips schrieb:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("F:F")) Is Nothing Then
...
End If
End Sub


This worked fine, but I need something else. I would like to reference
my column by "title" or "heading" i.e. what is written in row 1. Or
maybe there is even a way to define a unique name for each column
regardless of what is written in row 1, and then reference that column
by that unique name, that would be best!


or

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target,Range("columnF")) Is Nothing Then
...
End If
End Sub

where columnF is a range name


I tried replacing "columnF" by what is written in my column in row 1,
but that didn't work. So I guess what I replaced wasn't a valid range

name.


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel - changing column reference based on value of other column Dharmesh Patel Excel Discussion (Misc queries) 4 October 12th 09 02:41 PM
MS Excel - changing reference column value based on another column Dharmesh Patel[_2_] Excel Discussion (Misc queries) 2 October 12th 09 01:19 PM
Excel - changing column reference based on value of other column Dharmesh Patel Excel Discussion (Misc queries) 2 October 12th 09 01:18 PM
Row reference increment but preserve column reference Pwanda Excel Worksheet Functions 1 April 28th 05 01:12 PM
Macro to Reference Column Next to Current Reference dolphinv4 Excel Discussion (Misc queries) 2 April 11th 05 08:36 AM


All times are GMT +1. The time now is 12:31 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"