LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default VBA and Excell - really need you help

In looking over my code again, I guess it would be more Excel-like if I used
the Hidden properties of the Columns and Rows in my tests rather than test
for their being "equal to zero" (even though there is no practical
difference between doing either)...

Function MaxRowInUse(Optional WS As Worksheet, Optional _
FactorInHiddenRows As Boolean = False) As Long
Dim X As Long
Dim LastRow As Long
If WS Is Nothing Then Set WS = ActiveSheet
With WS
For X = 1 To .UsedRange.Columns.Count
If Not (Not FactorInHiddenRows And Columns(X).Hidden) Then
LastRow = .Cells(.Rows.Count, X).End(xlUp).Row
If LastRow MaxRowInUse Then MaxRowInUse = LastRow
End If
Next
End With
End Function

Function MaxColumnInUse(Optional WS As Worksheet, Optional _
FactorInHiddenColumns As Boolean = False) As Long
Dim X As Long
Dim LastColumn As Long
If WS Is Nothing Then Set WS = ActiveSheet
With WS
For X = 1 To .UsedRange.Rows.Count
If Not (Not FactorInHiddenColumns And Rows(X).Hidden) Then
LastColumn = .Cells(X, .Columns.Count).End(xlToLeft).Column
If LastColumn MaxColumnInUse Then MaxColumnInUse = LastColumn
End If
Next
End With
End Function

Rick


"Rick Rothstein (MVP - VB)" wrote in
message ...
That reminded me of these two functions that I dummied up awhile ago (one
for determining the maximum row in use and the other for determining the
maximum column in use), but I don't think I ever posted them to any
newsgroups before. If you don't specify a worksheet in the first
(optional) argument, then the active sheet is used. The second optional
argument is the interesting one... it lets you determine whether to
include hidden rows or columns when determining the maximum row and column
that are in use; that is, if a hidden row or column contains the maximum
row or column, it will be ignored unless the second argument is set to
True. This allows you to get the maximum row or column for what you see on
the worksheet rather than for what what any hidden data would return. I
wasn't sure which would be the most logical default for this second
argument, so I chose not factor in hidden rows or columns (that is, the
functions return the maximum row and column for only the visible data); if
desired, this can be easily changed in the declaration headers for each
function. I hope you and others will find this of some use...

Function MaxRowInUse(Optional WS As Worksheet, Optional _
FactorInHiddenRows As Boolean = False) As Long
Dim X As Long
Dim LastRow As Long
If WS Is Nothing Then Set WS = ActiveSheet
With WS
For X = 1 To .UsedRange.Columns.Count
If Not (Not FactorInHiddenRows And Columns(X).Width = 0) Then
LastRow = .Cells(.Rows.Count, X).End(xlUp).Row
If LastRow MaxRowInUse Then MaxRowInUse = LastRow
End If
Next
End With
End Function

Function MaxColumnInUse(Optional WS As Worksheet, Optional _
FactorInHiddenColumns As Boolean = False) As Long
Dim X As Long
Dim LastColumn As Long
If WS Is Nothing Then Set WS = ActiveSheet
With WS
For X = 1 To .UsedRange.Rows.Count
If Not (Not FactorInHiddenColumns And Rows(X).Height = 0) Then
LastColumn = .Cells(X, .Columns.Count).End(xlToLeft).Column
If LastColumn MaxColumnInUse Then MaxColumnInUse = LastColumn
End If
Next
End With
End Function

Rick



"Gord Dibben" <gorddibbATshawDOTca wrote in message
...
Rick

I stumbled across this UDF from Bob Flanagan which ignores those once
filled
then cleared cells.

Function RangeToUse(anySheet As Worksheet) As Range

'this function returns the range from cells A1 to cell which is the
'intersection of the last row with an entry and the last column with an
'entry.

Dim I As Integer, c As Integer, r As Integer

With anySheet.UsedRange
I = .Cells(.Cells.Count).Column + 1
For c = I To 1 Step -1
If Application.CountA(anySheet.Columns(c)) 0 _
Then Exit For
Next
I = .Cells(.Cells.Count).Row + 1
For r = I To 1 Step -1
If Application.CountA(anySheet.Rows(r)) 0 Then _
Exit For
Next
End With

With anySheet
Set RangeToUse = .Range(.Cells(1, 1), .Cells(r, c))
End With
End Function

Then a statement like Set rng = RangeToUse(ActiveSheet)


Gord


On Sat, 2 Aug 2008 17:28:02 -0700, Xaraam

wrote:

As for what controls the setting/resetting of the UsedRange, it looks
like
deleting entire columns and/or rows is the key. See the "To
programmatically
reset the used range" section on this webpage...




 
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
Is it possible to change the normal excell icon on an excell file? Romileyrunner1 Excel Worksheet Functions 7 September 8th 09 08:38 PM
auto locking of excell workbook (excell 2003) cheekymonkey Excel Discussion (Misc queries) 2 November 14th 08 11:50 PM
How to open MS Excell 2007 Sheet in MS Excell 2000??? Alek Luchnikov New Users to Excel 1 March 22nd 07 04:40 PM
create a slides show with excell spreadsheets using excell wantabepas Charts and Charting in Excel 0 June 16th 06 07:46 PM
how do you open an excell email attacment, if I dont have excell Gary Excel Discussion (Misc queries) 1 February 22nd 06 10:26 PM


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