+ Reply to Thread
Results 1 to 3 of 3

forcing excel to update last used cell?

  1. #1
    Forum Contributor
    Join Date
    02-09-2004
    Posts
    154

    forcing excel to update last used cell?

    I have some code which copies formulas from T13 down to the last used row in T13 whenever the file is saved. However I have a problem where i put something in say rows 13 to 20 but then delete rows 15-20 at some point, yet excel doesnt update the last used rows properly so when the macro in Workbook_BeforeSave runs the below code, it still thinks rows 13-20 are used and it copies the formulas down to row 20 even when 15-20 are now blank because I have deleted the ENTIRE rows (and some below that just to be sure).

    I have had this problem before where excel seemingly doesnt update the last used rows until you actually exit the file and open it again so I am wondering if there is some command to force excel to update what it thinks is the last used row, so that i can get that value updated just before the macro runs the copy down formulas code, my code for reference is

    Range("T13").Copy
    Range("T13:T" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False

    any ideas??

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello Neowok,

    You have just discovered an unsual "feature" of LastCell. It seems LastCell is loaded when the Workbook is first opened and doesn't update until it is reopened. A more reliable method is using the End Method for a range which allows you to select the last cell in a range directionally: Up, Down, Left, Right. I have made this change for you in the code line line below.

    Original Code:
    Range("T13").Copy
    Range("T13:T" & ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell) .Row).Select
    Selection.PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False


    Modified Code:
    Range("T13").Copy
    'Get the Row Number of the last Worksheet Row
    RC = Range("T:T").Rows.Count - 1

    'Get the Address of the Last Cell in Column T
    LC = Range("T:" & RC).End(xlUp).Address

    Range("T13:" & LC).PasteSpecial Paste:=xlPasteFormulas, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False

    Hope this helps,
    Leith Ross

  3. #3
    Forum Contributor
    Join Date
    02-09-2004
    Posts
    154
    couldnt get this to work, I think its because

    RC = Range("T:T").Rows.Count - 1

    This may be the problem, in that column T may be completely blank except for T13 so theres no point counting the last used row based on column T, I need the last used row based on all of columns A to U,

    the problem is doing this:

    RC = Range("A:U").Rows.Count - 1
    LC = Range("T:" & RC).End(xlUp).Address

    ends up giving me a range of 65535 for RC and T13 for LC

    T13 is currently the last used cell in column T, BUT I have typed some junk into O18 and its not picking this up as O being the last used row in the sheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1