![]() |
usedrange
hi,
i am trying to establish the part of a worksheet that contains values. the usedrange function would seem to fit the bill (and works fine most of the time), but every now and then it returns virtually the entire sheet as a range rather than just the used part. there is clearly something confusing it but there is no data obviously in the cells it returns. does anyone have any ideas as to how to get around this? is there some other way of finding the used part of a sheet? the other thing i have noticed with the usedrange function is that when all the columns are returned (as per problem above) it then leaves the column letters out of the range's address (if you look at the address property of the range object). any ideas would be welcome. mike |
usedrange
Grüezi Mike
Mike schrieb am 17.02.2005 i am trying to establish the part of a worksheet that contains values. the usedrange function would seem to fit the bill (and works fine most of the time), but every now and then it returns virtually the entire sheet as a range rather than just the used part. there is clearly something confusing it but there is no data obviously in the cells it returns. does anyone have any ideas as to how to get around this? is there some other way of finding the used part of a sheet? Assuming your datas start in A1, then you my get the last cell with the following function: Function GetLastCell(Optional rng As Range, _ Optional sh As Worksheet) _ As Range If sh Is Nothing Then Set sh = ActiveSheet If rng Is Nothing Then Set rng = sh.Cells Else: Set rng = sh.Range(rng.Address) End If Set GetLastCell = sh.Cells(rng.Find(What:="*", _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious).Row, _ rng.Find(What:="*", _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column) End Function If you call it without any parameters, yoz'll get the lastcell of the active sheet. Regards Thomas Ramel -- - MVP for Microsoft-Excel - [Win XP Pro SP-2 / xl2000 SP-3] |
usedrange
Mike,
The following sub calls the function below and returns the "filled" range of the active sheet (you will need to adjust for line wrapping): Private Sub Get_Filled_Range() If RangeFilled = "" Then MsgBox "Sheet empty!" Else MsgBox RangeFilled End Sub Public Function RangeFilled() As String 'RETURN THE FILLED RANGE OF A SHEET Dim rngFind As Range Dim strAddressTL As String Dim strAddressBR As String Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlNext) If rngFind Is Nothing Then On Error GoTo 0: Exit Function Else strAddressTL = rngFind.Row Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlNext) strAddressTL = Cells(strAddressTL, rngFind.Column).Address Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) strAddressBR = rngFind.Row Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious) strAddressBR = Cells(strAddressBR, rngFind.Column).Address RangeFilled = Range(strAddressTL & ":" & strAddressBR).Address End Function HTH "Mike" wrote: hi, i am trying to establish the part of a worksheet that contains values. the usedrange function would seem to fit the bill (and works fine most of the time), but every now and then it returns virtually the entire sheet as a range rather than just the used part. there is clearly something confusing it but there is no data obviously in the cells it returns. does anyone have any ideas as to how to get around this? is there some other way of finding the used part of a sheet? the other thing i have noticed with the usedrange function is that when all the columns are returned (as per problem above) it then leaves the column letters out of the range's address (if you look at the address property of the range object). any ideas would be welcome. mike |
usedrange
OOOOPS!
In my first post, I accidentally gave you an unperfected version. Use the following function instead (sorry for confusion): Public Function RangeFilled() As String 'RETURN THE FILLED RANGE OF A SHEET Dim rngFind As Range Dim strAddressTL As String Dim strAddressBR As String Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious) If rngFind Is Nothing Then On Error GoTo 0: Exit Function Else strAddressBR = rngFind.Row Set rngFind = ActiveSheet.UsedRange.Find(What:="*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious) strAddressBR = Cells(strAddressBR, rngFind.Column).Address Set rngFind = ActiveSheet.UsedRange.Find(What:="*", After:=Range(strAddressBR), SearchOrder:=xlByRows, SearchDirection:=xlNext) strAddressTL = rngFind.Row Set rngFind = ActiveSheet.UsedRange.Find(What:="*", After:=Range(strAddressBR), SearchOrder:=xlByColumns, SearchDirection:=xlNext) strAddressTL = Cells(strAddressTL, rngFind.Column).Address RangeFilled = Range(strAddressTL & ":" & strAddressBR).Address End Function "Mike" wrote: hi, i am trying to establish the part of a worksheet that contains values. the usedrange function would seem to fit the bill (and works fine most of the time), but every now and then it returns virtually the entire sheet as a range rather than just the used part. there is clearly something confusing it but there is no data obviously in the cells it returns. does anyone have any ideas as to how to get around this? is there some other way of finding the used part of a sheet? the other thing i have noticed with the usedrange function is that when all the columns are returned (as per problem above) it then leaves the column letters out of the range's address (if you look at the address property of the range object). any ideas would be welcome. mike |
All times are GMT +1. The time now is 05:32 AM. |
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com