SQL and Last Cell

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

SQL and Last Cell

Post by jstevens »

I can run various SQL Queries to Sheet1 which return the expected results. The challenge I'm facing is that the "Last Cell" (F5>Special>Last Cell) keeps moving to the right on subsequent queries run.

Within the code I have cleared Sheet1 cells: Sheet1.Cell.Clearcontents, deleted Columns B:DZ and checked for query & existing connections (there are none).

Your thoughts are appreciated.
Regards,
John

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL and Last Cell

Post by HansV »

Sorry, no idea. Do you have a sample workbook?
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: SQL and Last Cell

Post by jstevens »

Hans,

I'm unable to post a sample workbook as there is a SQL database that the VBA code utilizes.

One thing that I observed is that the F5>Special>Last Cell returned cell address is not the same as the intersection of the last row/column used. If I save the file and run F5>Special>Last Cell again it returns the correct cell address.
Regards,
John

User avatar
HansV
Administrator
Posts: 78415
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: SQL and Last Cell

Post by HansV »

You can reset the last cell in VBA by referring to the UsedRange property of the worksheet. For example:

Dim rng As Range
Set rng = ActiveSheet.UsedRange

You can refer to a specific sheet instead of to ActiveSheet, of course.
Best wishes,
Hans

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Re: SQL and Last Cell

Post by jstevens »

Hans,

Your suggestion worked.

Thank you.
Regards,
John