Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Kindly suggest what is the best way to reference the following in excel formulas:
+) last row of a worksheet that has some data +) last column of a worksheet that has some data Regards, Sandeep |
#2
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Hi Sandeep
In VBA the best thing is to use this functions You can use this in your code then for example Lr = LastRow(Sheets("Sheet2")) Function LastRow(sh As Worksheet) On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByRows, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Row On Error GoTo 0 End Function Function Lastcol(sh As Worksheet) On Error Resume Next Lastcol = sh.Cells.Find(What:="*", _ After:=sh.Range("A1"), _ Lookat:=xlPart, _ LookIn:=xlFormulas, _ SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious, _ MatchCase:=False).Column On Error GoTo 0 End Function -- Regards Ron de Bruin (Win XP Pro SP-1 XL2000-2003) www.rondebruin.nl "Sandeep Sharma" wrote in message m... Kindly suggest what is the best way to reference the following in excel formulas: +) last row of a worksheet that has some data +) last column of a worksheet that has some data Regards, Sandeep |
#3
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
"Ron de Bruin" wrote...
.... In VBA the best thing is to use this functions You can use this in your code then for example Lr = LastRow(Sheets("Sheet2")) Function LastRow(sh As Worksheet) On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ .... Function Lastcol(sh As Worksheet) On Error Resume Next Lastcol = sh.Cells.Find(What:="*", _ .... "Sandeep Sharma" wrote in message Kindly suggest what is the best way to reference the following in excel formulas: +) last row of a worksheet that has some data +) last column of a worksheet that has some data Ron's suggestion only works in VBA when his functions are called from VBA procedures (Subs). As there's no way to pass worksheet objects from worksheet cells to VBA udfs, as well as the fact that the .Find *method* of the Range class always fails when called from UDFs (at least through XL2000), they won't work as udfs. Since the OP states that these would be used 'in excel formulas', it's not unreasonable to suppose that means in cell formulas. To do that, a different approach is needed. Function lr(Optional r As Range) As Variant Dim ur As Range, c As Range, i As Long, n As Long 'there arguments for & against volatility - optional to uncomment 'Application.Volatile If r Is Nothing Then Set r = Application.Caller If Not TypeOf r Is Range Then Set r = ActiveCell Set ur = r.Parent.UsedRange n = ur.Rows.Count For i = n To 1 Step -1 Set c = ur.Cells(n, 1) If Not IsEmpty(c.Value) Then Exit For If Not IsEmpty(c.End(xlToRight).Value) Then Exit For Next i lr = ur.Row + i - 1 End Function Function lc(Optional r As Range) As Variant Dim ur As Range, c As Range, i As Long, n As Long 'there arguments for & against volatility - optional to uncomment 'Application.Volatile If r Is Nothing Then Set r = Application.Caller If Not TypeOf r Is Range Then Set r = ActiveCell Set ur = r.Parent.UsedRange n = ur.Columns.Count For i = n To 1 Step -1 Set c = ur.Cells(1, n) If Not IsEmpty(c.Value) Then Exit For If Not IsEmpty(c.End(xlDown).Value) Then Exit For Next i lc = ur.Column + i - 1 End Function |
#4
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Hi Harlan
I see now he posted in microsoft.public.excel.programming,microsoft.publi c.excel.misc,microsoft.public.excel.worksheet.func tions. I read it in programming and answer there Thanks for the correction -- Regards Ron de Bruin (Win XP Pro SP-1 XL2000-2003) www.rondebruin.nl "Harlan Grove" wrote in message ... "Ron de Bruin" wrote... ... In VBA the best thing is to use this functions You can use this in your code then for example Lr = LastRow(Sheets("Sheet2")) Function LastRow(sh As Worksheet) On Error Resume Next LastRow = sh.Cells.Find(What:="*", _ ... Function Lastcol(sh As Worksheet) On Error Resume Next Lastcol = sh.Cells.Find(What:="*", _ ... "Sandeep Sharma" wrote in message Kindly suggest what is the best way to reference the following in excel formulas: +) last row of a worksheet that has some data +) last column of a worksheet that has some data Ron's suggestion only works in VBA when his functions are called from VBA procedures (Subs). As there's no way to pass worksheet objects from worksheet cells to VBA udfs, as well as the fact that the .Find *method* of the Range class always fails when called from UDFs (at least through XL2000), they won't work as udfs. Since the OP states that these would be used 'in excel formulas', it's not unreasonable to suppose that means in cell formulas. To do that, a different approach is needed. Function lr(Optional r As Range) As Variant Dim ur As Range, c As Range, i As Long, n As Long 'there arguments for & against volatility - optional to uncomment 'Application.Volatile If r Is Nothing Then Set r = Application.Caller If Not TypeOf r Is Range Then Set r = ActiveCell Set ur = r.Parent.UsedRange n = ur.Rows.Count For i = n To 1 Step -1 Set c = ur.Cells(n, 1) If Not IsEmpty(c.Value) Then Exit For If Not IsEmpty(c.End(xlToRight).Value) Then Exit For Next i lr = ur.Row + i - 1 End Function Function lc(Optional r As Range) As Variant Dim ur As Range, c As Range, i As Long, n As Long 'there arguments for & against volatility - optional to uncomment 'Application.Volatile If r Is Nothing Then Set r = Application.Caller If Not TypeOf r Is Range Then Set r = ActiveCell Set ur = r.Parent.UsedRange n = ur.Columns.Count For i = n To 1 Step -1 Set c = ur.Cells(1, n) If Not IsEmpty(c.Value) Then Exit For If Not IsEmpty(c.End(xlDown).Value) Then Exit For Next i lc = ur.Column + i - 1 End Function |
#5
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
"Harlan Grove" wrote in message ...
"Ron de Bruin" wrote... .... Since the OP states that these would be used 'in excel formulas', it's not unreasonable to suppose that means in cell formulas. To do that, a different approach is needed. .... Thanks to all of you for your tips. I am somewhat embarrassed to ask the follow up question: Q) How are user-defined functions "installed" within Excel so that they may be referenced in cell formulas? Thanks, Sandeep |
#6
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Hi Sandeep!
You can just put the functions in a module in the workbook that will call them You can put them in a module in the Personal.xls file You can put them in an Addin that you load each time that you open Excel. -- Regards Norman Harker MVP (Excel) Sydney, Australia Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "Sandeep Sharma" wrote in message om... Q) How are user-defined functions "installed" within Excel so that they may be referenced in cell formulas? Thanks, Sandeep |
#7
![]()
Posted to microsoft.public.excel.programming,microsoft.public.excel.misc,microsoft.public.excel.worksheet.functions
|
|||
|
|||
![]()
Hi Sandeep
If your usedrange is bigger then your data on the sheet the formula will not work correct(display 0). Use CTRL-End to see your Usedrange Delete the rows/ columns with no data in the usedrange -- Regards Ron de Bruin (Win XP Pro SP-1 XL2000-2003) www.rondebruin.nl "Norman Harker" wrote in message ... Hi Sandeep! You can just put the functions in a module in the workbook that will call them You can put them in a module in the Personal.xls file You can put them in an Addin that you load each time that you open Excel. -- Regards Norman Harker MVP (Excel) Sydney, Australia Excel and Word Function Lists (Classifications, Syntax and Arguments) available free to good homes. "Sandeep Sharma" wrote in message om... Q) How are user-defined functions "installed" within Excel so that they may be referenced in cell formulas? Thanks, Sandeep |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Fill column adjacent to external query (Excel 2007) | Excel Discussion (Misc queries) | |||
How do I expand formula down a column when query results change? | Excel Discussion (Misc queries) | |||
Dynamic URL for first column of data from excel query? | Excel Discussion (Misc queries) | |||
Dynamic URL for first column of data from excel query? | Excel Discussion (Misc queries) | |||
Converting an Excel formula to an Access query formula | Excel Discussion (Misc queries) |