Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Table column in VBA

Hi, all,

I am writing some VBA code and want to control an Excel 2010
table-object and a column or headers to a column from it with set
abstraction names rather than just using the real name. I'm having
trouble getting it to work.

So I have a table called "Table2". I have a column called "Key" as
the left column of that.

Dim tblTgt As Range

Set tblTgt = [table2] 'workbook scope

Well, that actually works. But then I can't, later,
select a column such as "Key" or a headder row or cell.
It won't work:

Range(TblTgt[[#Headers],["Key"]]).Select
Range(TblTgt[[#Headers],[Key]]).Select
TblTgt[[#Headers],[Key]].Select
TblTgt[[#Headers],["Key"]].Select

and various other variations on that kind of thing won't
work. I have to go back to:

Range("Table2[[#Headers],[Key]]").Select

That works, but it defeats the point of the abstraction
in the code.

Ideas?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Table column in VBA

hi,

the first column address:
firtColumn = Range("Table2").Columns(1).Address

the last column address:
lastColumn = Range("Table2").Columns(Range("Table1").Columns.Co unt).Address

isabelle

Le 2015-04-14 19:19, Sarah H. a écrit :
Hi, all,

I am writing some VBA code and want to control an Excel 2010
table-object and a column or headers to a column from it with set
abstraction names rather than just using the real name. I'm having
trouble getting it to work.

So I have a table called "Table2". I have a column called "Key" as
the left column of that.

Dim tblTgt As Range

Set tblTgt = [table2] 'workbook scope

Well, that actually works. But then I can't, later,
select a column such as "Key" or a headder row or cell.
It won't work:

Range(TblTgt[[#Headers],["Key"]]).Select
Range(TblTgt[[#Headers],[Key]]).Select
TblTgt[[#Headers],[Key]].Select
TblTgt[[#Headers],["Key"]].Select

and various other variations on that kind of thing won't
work. I have to go back to:

Range("Table2[[#Headers],[Key]]").Select

That works, but it defeats the point of the abstraction
in the code.

Ideas?

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Table column in VBA

oops, correction:

the last column address:
lastColumn = Range("Table2").Columns(Range("Table2").Columns.Co unt).Address

isabelle

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 587
Default Table column in VBA

anotherway,

x = Application.Match("Key", [table2].Rows(1), 0)
rangeKey = Range("Table2").Columns(x).Address

isabelle

Le 2015-04-14 23:15, isabelle a écrit :
oops, correction:

the last column address:
lastColumn = Range("Table2").Columns(Range("Table2").Columns.Co unt).Address

isabelle

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Table column in VBA

isabelle wrote:
anotherway,

x = Application.Match("Key", [table2].Rows(1), 0)
rangeKey = Range("Table2").Columns(x).Address

isabelle

Le 2015-04-14 23:15, isabelle a écrit :
oops, correction:

the last column address:
lastColumn = Range("Table2").Columns(Range("Table2").Columns.Co unt).Address


Interesting ideas. But I wasn't clear about what I am trying to do.

Basically, I have a long set of VBA procedures. I want to set all
the real names to variable names near the top, so if I change my sheets
later I won't have to search all through all the code to find individual
instances of, e.g., "Table2". What If I rename Table2 next year and
call it "MyUpdatedTable"? Then, on lines 435 and 654 of my procedure(s)
somewhere it says "Table2", and the code won't work.

So at the top I wanted

set foo = Range("Table2")

If I change my tables later, I know to just go to the definitions
area of my macros and change the set variables there.

This seems to me to be careful and correct programming technique
for me and my possible successors who won't want to look at
spaghetti code to figure out what I was doing in Spring 2015.

But I can't figure out how to do it for individual columns in a table.

Well, let me amend that. I can do it, but only if I set each column
manually. I don't want that. I want the same flexibility to find
subranges using the abstraction name "foo" that I have with the
actual (current) name "Table2".

Isabelle, I appreciate your kind efforts of help.

May I mention, as an aside, that "v.org" is an actual domain? If
you are not connected with it, I would suggest you use a
legitimate fake address if you want to munge your address in
the group. That's why I choose "example.com": it was created
and reserved for such purposes and is sanctioned for that.
http://en.wikipedia.org/wiki/Example.com

If you do actually work at v.org, then I apologize. :-)

--
/s


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default Table column in VBA

Sarah H. wrote:
isabelle wrote:
anotherway,

x = Application.Match("Key", [table2].Rows(1), 0)
rangeKey = Range("Table2").Columns(x).Address

isabelle

Le 2015-04-14 23:15, isabelle a écrit :
oops, correction:

the last column address:
lastColumn = Range("Table2").Columns(Range("Table2").Columns.Co unt).Address


Interesting ideas. But I wasn't clear about what I am trying to do.


Isabelle, you were closer to right than I gave you credit for. Apologies!

This works (tested in the "Immediate" window):

set foo=[table2]
x=Application.Match("Key", foo.Rows(0), 0)
debug.Print x
1
foo.columns(x).select

So that's pretty much what I want to be able to do. Thanks!
The trick is, Row(0) is the table header. I wonder if this
is documented anywhere.

--
/s
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
Sorting a Pivot Table Column that is not the first column... [email protected] Excel Worksheet Functions 1 October 10th 07 09:02 PM
Pivot table: showing COLUMN summaries for some, not all, COLUMN fields. AndyCotgreave Excel Programming 0 October 2nd 07 11:18 AM
pivot table, How to add 1 column Daniel Charts and Charting in Excel 0 July 24th 07 08:00 PM
Can a column be % of another column in a pivot table joel Excel Discussion (Misc queries) 1 January 11th 07 01:36 PM
"Cumulative" column based on a pivot table sum column? Robert Day Excel Worksheet Functions 2 December 15th 05 09:20 PM


All times are GMT +1. The time now is 04:31 PM.

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"