Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Find last used row in named range with data all around

Hi,

I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?

Someone, please help me!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Find last used row in named range with data all around

Sub CallTheBottomRowFunction()
Dim lngRow As Long
lngRow = GetBottomRow(ActiveSheet.Range("Hours"))
MsgBox lngRow & " is the last row. "
End Sub

Function GetBottomRow(ByRef TheRange As Excel.Range) As Long
On Error GoTo NoRow
GetBottomRow = TheRange.Cells.Find(what:="*", SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious).Row
Exit Function
NoRow:
GetBottomRow = 0
End Function
----------------
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware


"venus"

wrote in message
Hi,
I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?
Someone, please help me!
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,058
Default Find last used row in named range with data all around

This is specific to your range:

Sub venus()
For i = 53 To 3 Step -1
If Application.CountA(Range(Cells(i, "C"), Cells(i, "P"))) 0 Then
Exit For
End If
Next
MsgBox ("Row " & i & " has data")
End Sub

You don't need to use the name of the range.

--
Gary's Student


"venus" wrote:

Hi,

I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?

Someone, please help me!


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find last used row in named range with data all around

I'd just loop through the rows--starting at the bottom.

Option Explicit
Sub testme01()

Dim iRow As Long
Dim myRng As Range
Dim LastRow As Long

With Worksheets("Sheet1")
Set myRng = .Range("Hours") 'c6:p53
End With

LastRow = 0
With myRng
For iRow = .Rows.Count To 1 Step -1
If Application.CountA(myRng.Rows(iRow)) = 0 Then
'keep looking
Else
LastRow = .Rows(iRow).Row
Exit For
End If
Next iRow
End With

If LastRow = 0 Then
MsgBox "None used!"
Else
MsgBox "last row was row#: " & LastRow
End If

End Sub



venus wrote:

Hi,

I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?

Someone, please help me!


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Find last used row in named range with data all around

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i


'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes



venus wrote:
Hi,

I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?

Someone, please help me!




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find last used row in named range with data all around

Won't that just return the last row of the range--whether or not it's used?

LooneyTunes wrote:

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i

'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes

venus wrote:
Hi,

I'm fairly good with Excel, but new to vba (I barely know what I'm
doing). I have a named range of cells c6:p53, named Hours. There is
data all around this range. I want to find the last used row within
this range and have had no luck.

Almost every solution I've found just gets me the very last row of the
worksheet instead of the range. There is a thread with a solution from
Jim Thomlinson, but I get an "invalid qualifier" error when I use it. I
have looked for days on the net and when I find code that should work,
it doesn't. What's wrong with me and my pc?

Someone, please help me!


--

Dave Peterson
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default Find last used row in named range with data all around

Thank you all so very much!

All codes worked but for Lonney Tunes, it only gave me the last row of
the range as Dave suggested.

God bless,
Venus

Dave Peterson wrote:
Won't that just return the last row of the range--whether or not it's used?

LooneyTunes wrote:

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i

'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default Find last used row in named range with data all around

Sorry, my bad... here's another snippet to try

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer, j As Integer, k As Integer
Dim iLastRow As Integer, iLastCol As Integer
Dim sLastAddress As String


'skinny version:
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
k = j
End If
Next i

MsgBox "The last used row in the range is: row " & iLastRow


'=========== If you want more info
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
iLastCol = rng.Columns(i).Column
k = j
sAddress = Cells(iLastRow, iLastCol).Address
End If
Next i


MsgBox "The last used row in the range is: row " & iLastRow _
& vbLf & "The last used column is: Column(" & iLastCol & ") or (" &
Columns(iLastCol).Address & ")" _
& vbLf & "The last cell address is: " & sAddress

End Sub

Sorry for the boo boo

Good luck and happy programming

LooneyTunes



venus wrote:
Thank you all so very much!

All codes worked but for Lonney Tunes, it only gave me the last row of
the range as Dave suggested.

God bless,
Venus

Dave Peterson wrote:
Won't that just return the last row of the range--whether or not it's used?

LooneyTunes wrote:

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i

'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Find last used row in named range with data all around

Hi Looney,

Well this code works better, but instead of the last row, it gives me
2-rows before the last. I'm not sure why...

Venus

LooneyTunes wrote:
Sorry, my bad... here's another snippet to try

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer, j As Integer, k As Integer
Dim iLastRow As Integer, iLastCol As Integer
Dim sLastAddress As String


'skinny version:
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
k = j
End If
Next i

MsgBox "The last used row in the range is: row " & iLastRow


'=========== If you want more info
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
iLastCol = rng.Columns(i).Column
k = j
sAddress = Cells(iLastRow, iLastCol).Address
End If
Next i


MsgBox "The last used row in the range is: row " & iLastRow _
& vbLf & "The last used column is: Column(" & iLastCol & ") or (" &
Columns(iLastCol).Address & ")" _
& vbLf & "The last cell address is: " & sAddress

End Sub

Sorry for the boo boo

Good luck and happy programming

LooneyTunes



venus wrote:
Thank you all so very much!

All codes worked but for Lonney Tunes, it only gave me the last row of
the range as Dave suggested.

God bless,
Venus

Dave Peterson wrote:
Won't that just return the last row of the range--whether or not it's used?

LooneyTunes wrote:

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i

'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Find last used row in named range with data all around

LooneyTunes used application.counta() to count the number of rows used.

If your data is nice and contiguous (no gaps), you'll be fine. But as soon as
the data gets gaps, all bets are off.



venus wrote:

Hi Looney,

Well this code works better, but instead of the last row, it gives me
2-rows before the last. I'm not sure why...

Venus

LooneyTunes wrote:
Sorry, my bad... here's another snippet to try

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer, j As Integer, k As Integer
Dim iLastRow As Integer, iLastCol As Integer
Dim sLastAddress As String


'skinny version:
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
k = j
End If
Next i

MsgBox "The last used row in the range is: row " & iLastRow


'=========== If you want more info
k = 0
Set rng = Range("hours")
For i = 1 To rng.Columns.Count
j = Application.CountA(rng.Columns(i))
If j k Then
iLastRow = rng.Rows(j).Row
iLastCol = rng.Columns(i).Column
k = j
sAddress = Cells(iLastRow, iLastCol).Address
End If
Next i


MsgBox "The last used row in the range is: row " & iLastRow _
& vbLf & "The last used column is: Column(" & iLastCol & ") or (" &
Columns(iLastCol).Address & ")" _
& vbLf & "The last cell address is: " & sAddress

End Sub

Sorry for the boo boo

Good luck and happy programming

LooneyTunes



venus wrote:
Thank you all so very much!

All codes worked but for Lonney Tunes, it only gave me the last row of
the range as Dave suggested.

God bless,
Venus

Dave Peterson wrote:
Won't that just return the last row of the range--whether or not it's used?

LooneyTunes wrote:

Try this little snippet of code:

Sub FindLastRowInMyRange()

Dim rng As Range
Dim i As Integer

'to find the last used row in your named range _
regardless of where it is
Set rng = Range("hours")
i = rng.Rows(rng.Rows.Count).Row

'proof
MsgBox "The last used row within your named range is row: " & i

'to find the next row after your named range
i = i + 1
'proof
MsgBox "Start your next row he row " & i

End Sub

Good luck and happy programming

LooneyTunes


--

Dave Peterson
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
Find in Named Range problem (2nd Try) G.R. New Users to Excel 2 February 27th 08 08:17 PM
Find Row in a named range Raul Excel Worksheet Functions 3 November 24th 07 12:41 AM
Find Cell in Named Range David Excel Worksheet Functions 3 May 8th 07 06:00 AM
find within a named range, then deselect the range Bob Mouldy Excel Programming 4 August 23rd 06 02:48 PM
How to find all formulas that used a certain named range Laurence Lombard Excel Discussion (Misc queries) 2 October 25th 05 08:15 AM


All times are GMT +1. The time now is 02:01 PM.

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"