Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Accessing Columns by name in a Row loop in VBA

I have a sheet with columns named that I want to access in code. And the
area I want to acees is also named.

I want to be able to write code like this (which is pseudo code of course):

Dim table As Range
table = Sheets("Books").Range("OutOfPrint")

Dim row As Range
For Each row In table
Dim title As String
Set title = row.Column("Title")

Dim cost As Integer
Set cost = row.Column("Cost")
Next row

The bit I don't get is how I can do the Column("<Name") magic. Any ideas?

Thanks,
Tyler
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Accessing Columns by name in a Row loop in VBA

Here is a general purpose function you can use:
Public Function FindCell(FindRange As Range, FindRow As Variant, FindColumn
As Variant, _
Optional UseColumn1 As Boolean = True, Optional UseColumnHeader As
Boolean = True) As Variant
' Returns a cell's contents by specifying row and column
' Row and column can "name" (value in 1st column/top row) or number
' UseColumn1 and UseColumnHeader determine if search is by matching the name
(true) or number (false)
Dim FRow As Integer, FColumn As Integer

On Error GoTo NotFound

With FindRange

If UseColumn1 Then FRow = WorksheetFunction.Match(FindRow, .Columns(1),
0) _
Else FRow = FindRow
If UseColumnHeader Then FColumn = WorksheetFunction.Match(FindColumn,
..Rows(1), 0) _
Else FColumn = FindColumn

FindCell = .Cells(FRow, FColumn)

End With

Exit Function

NotFound:
FindCell = CVErr(9001)
End Function

So, for example, FindCell(ActiveSheet.Cells, 5, "NAME", False, True) will
return the value in row 5 of the range that is in the column with the header
"NAME"

By the way, it is good practice to avoid using words like "row" and "column"
as variable names: since there are object properties with these names you are
bound to confuse yourself - or, worse, confuse VBA.
--
- K Dales


"Tyler W. Wilson" wrote:

I have a sheet with columns named that I want to access in code. And the
area I want to acees is also named.

I want to be able to write code like this (which is pseudo code of course):

Dim table As Range
table = Sheets("Books").Range("OutOfPrint")

Dim row As Range
For Each row In table
Dim title As String
Set title = row.Column("Title")

Dim cost As Integer
Set cost = row.Column("Cost")
Next row

The bit I don't get is how I can do the Column("<Name") magic. Any ideas?

Thanks,
Tyler

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Accessing Columns by name in a Row loop in VBA

Mmm, I guess I was hoping for a slightly simpler solution. There is no
way, given a range which represents a row, to get a cell based on its
column name?

Thanks,
Tyler

K Dales wrote:
Here is a general purpose function you can use:
Public Function FindCell(FindRange As Range, FindRow As Variant, FindColumn
As Variant, _
Optional UseColumn1 As Boolean = True, Optional UseColumnHeader As
Boolean = True) As Variant
' Returns a cell's contents by specifying row and column
' Row and column can "name" (value in 1st column/top row) or number
' UseColumn1 and UseColumnHeader determine if search is by matching the name
(true) or number (false)
Dim FRow As Integer, FColumn As Integer

On Error GoTo NotFound

With FindRange

If UseColumn1 Then FRow = WorksheetFunction.Match(FindRow, .Columns(1),
0) _
Else FRow = FindRow
If UseColumnHeader Then FColumn = WorksheetFunction.Match(FindColumn,
.Rows(1), 0) _
Else FColumn = FindColumn

FindCell = .Cells(FRow, FColumn)

End With

Exit Function

NotFound:
FindCell = CVErr(9001)
End Function

So, for example, FindCell(ActiveSheet.Cells, 5, "NAME", False, True) will
return the value in row 5 of the range that is in the column with the header
"NAME"

By the way, it is good practice to avoid using words like "row" and "column"
as variable names: since there are object properties with these names you are
bound to confuse yourself - or, worse, confuse VBA.

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
loop over columns kizzie Excel Discussion (Misc queries) 4 August 10th 05 01:31 PM
incrementing columns in a loop Mitch Excel Programming 1 June 9th 05 03:40 PM
Loop through columns and then rows Sunryzz Excel Programming 8 December 23rd 04 03:13 PM
loop through columns hotherps[_78_] Excel Programming 6 July 23rd 04 11:40 AM
Loop 20 columns Help! Michael168[_106_] Excel Programming 2 July 2nd 04 12:26 PM


All times are GMT +1. The time now is 05:51 AM.

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

About Us

"It's about Microsoft Excel"