LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 162
Default Functions to get range properties

I wrote the following utility functions for dealing with ranges. I
often want the first column number or label; last column number or
label; next column label; first row; last row, etc.

What I want to know is whether there is a better way to get this info
with built-in functions. Any ideas?

Here are the utility functions:


' Constants
Private Const ZEE As Integer = 26
Private Const MINCOL As Integer = 1
Private Const MAXCOL As Integer = 256
Private Const ASCII64 As Integer = 64
'
'

Public Function GetFirstColumnNumber(ByVal A1Address As String) As
Integer

Dim rng As Range

Set rng = Application.ActiveWorkbook.Sheets(1).Range(A1Addre ss)
GetFirstColumnNumber = rng.Columns.Item(1).Column
Set rng = Nothing

End Function


Public Function GetLastColumnNumber(ByVal A1Address As String) As
Integer

Dim rng As Range
Dim iFirst As Integer
Dim iCount As Integer

iFirst = GetFirstColumnNumber(A1Address)

Set rng = Application.ActiveWorkbook.Sheets(1).Range(A1Addre ss)
iCount = rng.Columns.Count
Set rng = Nothing

GetLastColumnNumber = iFirst + iCount - 1

End Function


Public Function GetFirstColumnLabel(ByVal A1Address As String) As
String

Dim iCol As Integer

iCol = GetFirstColumnNumber(A1Address)
GetFirstColumnLabel = ColumnNumberToLabel(iCol)

End Function


Public Function GetLastColumnLabel(ByVal A1Address As String) As String

Dim iCol As Integer

iCol = GetLastColumnNumber(A1Address)
GetLastColumnLabel = ColumnNumberToLabel(iCol)

End Function


Public Function GetFirstRowNumber(ByVal A1Address As String) As Integer

Dim rng As Range

Set rng = Application.ActiveWorkbook.Sheets(1).Range(A1Addre ss)
GetFirstRowNumber = rng.Rows.Item(1).Row
Set rng = Nothing

End Function


Public Function GetLastRowNumber(ByVal A1Address As String) As Integer

Dim rng As Range
Dim iFirst As Integer
Dim iCount As Integer

Set rng = Application.ActiveWorkbook.Sheets(1).Range(A1Addre ss)
iCount = rng.Rows.Count
Set rng = Nothing

GetLastRowNumber = GetFirstRowNumber(A1Address) + iCount - 1

End Function


Public Function ExtractCellAddress(ByVal FullAddress As String) As
String

If InStr(FullAddress, "!") 0 Then
FullAddress = Right(FullAddress, Len(FullAddress) - pos)
End If

ExtractCellAddress = FullAddress

End Function


Public Function ColumnNumberToLabel(ByVal ColumnNumber As Integer) As
String

Dim ret As String

If ColumnNumber < MINCOL Or ColumnNumber MAXCOL Then
ret = "!OutOfRangeError"
ElseIf ColumnNumber ZEE Then
ret = Chr(Int(ColumnNumber / ZEE) + ASCII64) &
Chr((ColumnNumber Mod ZEE) + ASCII64)
Else
ret = Chr(ColumnNumber + ASCII64)
End If

ColumnNumberToLabel = ret

End Function


Public Function GetNextColumnLabel(ByVal ColumnLabel As String) As
String

Dim char1 As String
Dim char2 As String
Dim iCol As Integer
Dim ret As String

If Len(ColumnLabel) = 1 Then
If ColumnLabel < "Z" Then
ret = Chr(Asc(ColumnLabel) + 1)
Else
ret = "AA" ' special case "Z" rolls over to "AA"
End If
ElseIf Len(ColumnLabel) = 2 Then
char1 = Left(ColumnLabel, 1)
char2 = Right(ColumnLabel, 1)
iCol = (Asc(char1) - ASCII64) * ZEE + Asc(char2) - ASCII64 + 1
ret = ColumnNumberToLabel(iCol)
Else
ret = "!OutOfRangeError"
End If

GetNextColumnLabel = ret

End Function

 
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
using the address properties from range objects in a sum formula JEFFWI Excel Discussion (Misc queries) 2 September 11th 07 05:36 AM
Data range properties baz Excel Discussion (Misc queries) 1 April 1st 05 09:37 AM
data range properties baz Excel Discussion (Misc queries) 0 April 1st 05 08:53 AM
Updating range properties is so sloooow!!! Frank_Hamersley Excel Programming 9 June 19th 04 08:43 PM
Range properties Krisztian Pinter Excel Programming 0 October 16th 03 04:47 PM


All times are GMT +1. The time now is 05:51 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"