SOLVED

How can I reset the “LastCell” in Excel?

%3CLINGO-SUB%20id%3D%22lingo-sub-1391606%22%20slang%3D%22en-US%22%3EHow%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391606%22%20slang%3D%22en-US%22%3E%3CP%3EHow%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20value%20in%20Excel%3F%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20need%20a%20macro%20that%20selects%20the%20correct%20print%20area%20after%20deleting%20several%20rows%20and%20columns.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EMy%20original%20spreadsheet%20has%202000%20rows%20and%2026%20columns.%20The%20%E2%80%9CLastCell%E2%80%9D%20with%20data%20is%20Z2000.%20Using%20the%20Excel%20keyboard%20shortcut%20%E2%80%9CCtrl-End%E2%80%9D%20successfully%20selects%20the%20%E2%80%9CLastCell%E2%80%9D%20which%20is%20cell%20Z2000.%20After%20deleting%20rows%20and%20columns%20the%20spreadsheet%20is%20now%201000%20rows%20and%2013%20columns.%20The%20%E2%80%9CLastCell%E2%80%9D%20should%20now%20be%20cell%20M1000%2C%20but%20Ctrl-End%20still%20selects%20cell%20Z2000%20instead%20of%20M1000%20and%20the%20print%20area%20is%20still%202000%20rows%20and%2026%20columns%2C%20printing%20a%20lot%20of%20empty%20pages.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1391606%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391672%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391672%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668666%22%20target%3D%22_blank%22%3E%40LonnieCurrier%3C%2FA%3E%26nbsp%3B%20Did%20you%20save%20the%20workbook%20after%20deleting%20the%20empty%20rows%20and%20columns%3F%20Excel%20remembers%20the%20previous%20%22Last%20cell%22%20until%20you%20do%20so.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391754%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391754%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3E%26nbsp%3B%20Thanks%20for%20the%20information!%26nbsp%3B%20No%2C%20the%20macro%20that%20deletes%20rows%20and%20columns%20was%20not%20saving%20the%20file%20before%20trying%20to%20use%20the%20%22LastCell%22%20function.%26nbsp%3B%20The%20documents%20are%20received%20by%20e-mail%2C%20so%20they%20are%20read-only%20when%20opened.%26nbsp%3B%20Now%20I%20need%20to%20figure%20out%20how%20to%20Save-As%20in%20the%20macro%20so%20that%20it%20will%20save%20to%20a%20location%20that%20the%20user%20would%20have%20permissions%20to.%26nbsp%3B%20I%20tested%20Save-As%20in%20the%20macro%2C%20and%20it%20records%20my%20specific%20username%20in%20the%20path%3A%3C%2FP%3E%3CP%3E%3CFONT%3E%3CFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3EActiveWorkbook.SAVEAS%20Filename%3A%3D%20_%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%22C%3A%5CUsers%5C%3CFONT%20color%3D%22%23ff0000%22%3ELonnieCurrier%3C%2FFONT%3E%5CDocuments%5CFILENAME.xlsx%22%2C%20FileFormat%3A%3D%20_%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20xlOpenXMLWorkbook%2C%20CreateBackup%3A%3DFalse%3C%2FFONT%3E%3C%2FP%3E%3CP%3E%3CFONT%3EIs%20there%20a%20variable%2Ffunction%20that%20I%20can%20use%20in%20place%20of%20my%20username%20so%20that%20Save-As%20will%20work%20for%20other%20users%3F%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391806%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391806%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668666%22%20target%3D%22_blank%22%3E%40LonnieCurrier%3C%2FA%3E%26nbsp%3BYou%20can%20declare%20three%20variables%20e.g.%20%3CSTRONG%3EDim%20UserName%2C%20SaveToPath%2C%20FileName%20as%20String%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3Eand%20then%20build%20the%20entire%20path%2C%20something%20like%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3ESaveToPath%3C%2FSTRONG%3E%20%3D%20%22%3CSPAN%3EC%3A%5CUsers%5C%22%20%26amp%3B%20%3CSTRONG%3EUserName%3C%2FSTRONG%3E%20%26amp%3B%20%22%5C%3C%2FSPAN%3E%3CSPAN%3EDocuments%5C%22%20%26amp%3B%20%3CSTRONG%3EFileName%3C%2FSTRONG%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThen%20you%20SaveAs%20code%20could%20become%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20%3C%2FFONT%3EActiveWorkbook.SAVEAS%20Filename%3A%3D%20%3CSTRONG%3ESaveToPath%3C%2FSTRONG%3E%2C%20FileFormat%3A%3D%20_%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%26nbsp%3B%20xlOpenXMLWorkbook%2C%20CreateBackup%3A%3DFalse%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENow%2C%20I%20didn't%20test%20this%20and%20hope%20I%20didn't%20make%20any%20typos.%20But%2C%20in%20case%20I%20did%2C%20I%20guess%20you%20can%20figure%20it%20out%20by%20yourself%20and%20make%20it%20work.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391892%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391892%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F403176%22%20target%3D%22_blank%22%3E%40Riny_van_Eekelen%3C%2FA%3EThanks%20again!%26nbsp%3B%20I%20got%20this%20to%20work%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CFONT%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20ActiveWorkbook.SAVEAS%20Filename%3A%3D%20_%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20Environ(%22UserProfile%22)%20%26amp%3B%20%22%5CDesktop%5CTemporaryFileName.xlsx%22%2C%20_%3CBR%20%2F%3E%26nbsp%3B%26nbsp%3B%26nbsp%3B%20FileFormat%3A%3DxlOpenXMLWorkbook%2C%20CreateBackup%3A%3DFalse%3C%2FFONT%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1391907%22%20slang%3D%22en-US%22%3ERe%3A%20How%20can%20I%20reset%20the%20%E2%80%9CLastCell%E2%80%9D%20in%20Excel%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1391907%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F668666%22%20target%3D%22_blank%22%3E%40LonnieCurrier%3C%2FA%3E%26nbsp%3BGreat!!%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
Occasional Contributor

How can I reset the “LastCell” value in Excel?

 

I need a macro that selects the correct print area after deleting several rows and columns.

 

My original spreadsheet has 2000 rows and 26 columns. The “LastCell” with data is Z2000. Using the Excel keyboard shortcut “Ctrl-End” successfully selects the “LastCell” which is cell Z2000. After deleting rows and columns the spreadsheet is now 1000 rows and 13 columns. The “LastCell” should now be cell M1000, but Ctrl-End still selects cell Z2000 instead of M1000 and the print area is still 2000 rows and 26 columns, printing a lot of empty pages.

5 Replies
Highlighted
Best Response confirmed by LonnieCurrier (Occasional Contributor)
Solution

@LonnieCurrier  Did you save the workbook after deleting the empty rows and columns? Excel remembers the previous "Last cell" until you do so.

Highlighted

@Riny_van_Eekelen  Thanks for the information!  No, the macro that deletes rows and columns was not saving the file before trying to use the "LastCell" function.  The documents are received by e-mail, so they are read-only when opened.  Now I need to figure out how to Save-As in the macro so that it will save to a location that the user would have permissions to.  I tested Save-As in the macro, and it records my specific username in the path:

        ActiveWorkbook.SAVEAS Filename:= _
        "C:\Users\LonnieCurrier\Documents\FILENAME.xlsx", FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False

Is there a variable/function that I can use in place of my username so that Save-As will work for other users?

Highlighted

@LonnieCurrier You can declare three variables e.g. Dim UserName, SaveToPath, FileName as String

and then build the entire path, something like:

SaveToPath = "C:\Users\" & UserName & "\Documents\" & FileName

 

Then you SaveAs code could become:

 

        ActiveWorkbook.SAVEAS Filename:= SaveToPath, FileFormat:= _
        xlOpenXMLWorkbook, CreateBackup:=False

 

Now, I didn't test this and hope I didn't make any typos. But, in case I did, I guess you can figure it out by yourself and make it work.

Highlighted

@Riny_van_EekelenThanks again!  I got this to work:

 

    ActiveWorkbook.SAVEAS Filename:= _
    Environ("UserProfile") & "\Desktop\TemporaryFileName.xlsx", _
    FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False

Highlighted