Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 14th, 2004, 09:14 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Last used cell

I am trying to get the cell ref. for the last used row of a worksheet. I have copied the code from ref book Excel 2002 VBA but all that this produces is an active cell of A1 whereas the next blank row is at 54.

I do have formulas in column extending beyond this but no text.

The ref book refers to this example (Page 111,112) as catering for this.

Help
 
Old October 14th, 2004, 09:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Try this ...
Code:
Sub FindLastCell()

Dim Cell As Range
Dim MaxRow As Integer
Dim MaxAddress As String

    MaxRow = 0

    For Each Cell In ActiveSheet.UsedRange
        If Cell.Text <> "" And Cell.Row > MaxRow Then
            MaxRow = Cell.Row
            MaxAddress = Cell.Address(RowAbsolute:=False, ColumnAbsolute:=False)
        End If
    Next Cell

    MsgBox MaxAddress

End Sub
 
Old October 14th, 2004, 09:49 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

The example in the book works ok. But the code doesn't activate the last cell. Try to add this to see which values that have been found:

MsgBox (lRealLastRow & "," & lRealLastColumn) .. or

Cells(lRealLastRow, lRealLastColumn).Select

Im almost sure you'll get the real last cell.
 
Old October 15th, 2004, 02:12 AM
Authorized User
 
Join Date: Aug 2004
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try this one :-
Code:
Sub GetLastCell()
    Dim LastCell As Range
    '------------------------------
    LastRow = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
    '------------------------------
    LastCol = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByColumns).Column
    '------------------------------
    Set LastCell = ActiveSheet.Cells(LastRow, LastCol)
    LastCell.Select
End Sub

-----------------------
Regards BrianB
Most problems occur from starting at the wrong place.
Use a cup of coffee to make Windows run faster.
It is easy until you know how.
 
Old October 15th, 2004, 02:18 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

BriabB's is the only version that works OK except the only problem is that I want to disregard formulas and only find the next
blank column after the last data input.

Have tried to revise all replies but to no avail!!

MACCAS's works OK but takes nearly 5 mins (against 2 secs for the one above) to obtain the correct result I want but sometimes causes Excel to lockup.

Birger is correct but I still get the last row which contains formulas but not text in the 10000's


 
Old October 15th, 2004, 03:28 PM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What I am trying to achieve is to update the range of a pivot table to read the latest information of timesheet entries.

As I am using Groups in the pivot table I can not have blanks otherwise I loose date groups of Qrts etc because as soon as a blank date is part of the pivot table range Qrts disappear and individual dates of ever entry is the only selection.

Therefore I will add the code to a command button so that after input of time sheet details the last Row/column ref. is autoed into the pivot table range and the table is refreshed.

Hope all makes sense.

Code so far below, just the range into the pivot table now fails and last row is last row of formulas not data



Sub GetLastCell()
    Dim LastCell As Range
    Dim last As String




     Sheets("Time Sheet Information ").Select
    '------------------------------
    Lastrow = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByRows).Row
    '------------------------------
    Lastcol = ActiveSheet.Cells.Find(What:="*", _
    SearchDirection:=xlPrevious, _
    SearchOrder:=xlByColumns).Column

    '------------------------------
    Set LastCell = ActiveSheet.Cells(Lastrow, Lastcol)
    LastCell.Select

    last = Lastrow
    last = ("R3C1:" + "R" + last + ("C14"))

MsgBox (last)

Selection = last

    Sheets("Sheet1").Select
    Range("C6").Select
' [u]this next line give error 1044 ref. not valid </u>
ActiveSheet.PivotTableWizard SourceType:=xlDatabase, SourceData:= _
        "'Time Sheet Information '!last"
    ActiveSheet.PivotTables("PivotTable2").PivotCache. Refresh
End Sub
 
Old October 15th, 2004, 06:42 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not sure whether you can use this approach but it will give you the row without formulas, found from bottom up. I expect you have a fairly good idea of how the pivot structure is build, so you may change the code to fit your needs:

Sub LastRowWithoutFormula()
Dim NoF, i, r As Integer

Range("A65536").End(xlUp).Select ' Substitute A with valid column-letter
NoF = Selection.Row
Range(NoF & ":" & NoF).Select

    For i = 10 To 1 Step -1
        For Each cell In Selection
            If cell.HasFormula Then
                NoF = NoF - 1
                Range(NoF & ":" & NoF).Select
            Else
                Exit For
            End If
        Next
    Next i

r = ActiveCell.Row
Cells(r, 1).Select

End Sub
 
Old October 16th, 2004, 11:51 AM
Registered User
 
Join Date: Oct 2004
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Last thread from Birger, you misunderstood, I require the last row with data (text, dates etc) but I have to keep formulas way past the row No.I require so that future data entered is calculated.

I am trying to find the range of data entered to be able to use this for the pivot table range updated by a command button.

Hope this explains
 
Old October 16th, 2004, 12:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 150
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Where's the row of data you want, compared to the rows with formulas?
Is it something like this:

  ...
  data
  data
  formulas ??
  empty

Is it always the same pattern ?

If above is the case, you may search bottom up as shown earlier, skip the line with formulas and reach the row with data.

If you havn't got a working solution, please let me know a little more of the structure.
 
Old October 18th, 2004, 04:23 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You could do it with an Excel Array formula in the workbook.

Assuming you have formulas (blank or not) in rows 1 to 1000 and colums A to C. Write the following formula in any other blank cell

Code:
=MAX(IF((IF(A1:A1000<>"",1,0)+IF(B1:B1000<>"",1,0)+IF(C1:C1000<>"",1,0))>0,ROW(A1:A1000),0))
To set it up as an array formula you will need to enter the formula by holding Ctrl & Shift and then pressing enter whilst the cursor is blinking in the formula bar

This cell will then always report the last non-blank row in your desired range (NB you may need to run application.calulate in any macro where calculation is turned off)

To extend the tested range either change the 1's and 1000's in my formula to the desired ammounts or if you ant to add more columns you need to add another if statement in with the other columns

Code:
...+IF(D1:D1000<>"",1,0) ...
Hope this helps
Maccas

NB for more help on array formulas check this out

http://<br /> <a href="http://www.c....htm</a><br />





Similar Threads
Thread Thread Starter Forum Replies Last Post
cell is empty while using For Each cell In Range jase2007 Excel VBA 4 April 5th, 2012 10:20 PM
Separating a CSV cell into 1 value per cell bigtonyicu Excel VBA 2 March 14th, 2008 12:28 PM
I need to refer a cell within a cell like =RC[ RC2 chakravarthi_os Excel VBA 1 September 24th, 2006 08:19 AM
if the cell content is a part of another cell cont sriramus Excel VBA 1 November 15th, 2005 10:20 AM
Lose cell Text when editing cell in VSFlexGrid 6 bobcratchet VB How-To 0 July 30th, 2004 09:32 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.