Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / September 2007

Tip: Looking for answers? Try searching our database.

UsedRange and Ctrl-Shift-End Mystery

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
LarryP - 18 Sep 2007 15:46 GMT
PUZZLE OF THE DAY:  I have an Excel file with a worksheet where columns A:D
(only) are populated with a variable amount of data -- could be three rows,
could be a thousand.  For this discussion, let's say there are 50 rows.

When I select A1 and then hit Ctrl-Shift-End, I would expect A1:D50 to be
selected, but I get A1:IV50.  I've tried selecting columns E:IV and deleting
them entirely to supposedly reset the used range, but get the same result
when I hit Ctrl-Shift-End.

To make it even more puzzling, if I copy the data from A1:D50 into a new
worksheet in the same workbook, Ctrl-Shift-End works as expected.  

And finally, I wrote a little code snippet to show me
ActiveWorkbook.Sheets("nameofsheet").UsedRange.Columns.Count, and it returns
4 in BOTH worksheets.

Who can suggest why this is happening (and ideally, a way to fix it
programmatically)?  It's screwing me up when I try to import the weird
worksheet into MSAccess.
jhyatt - 18 Sep 2007 15:56 GMT
i just check a mock sheet trying selecting coulumns e:iv and going to format
and reseting to general.

> PUZZLE OF THE DAY:  I have an Excel file with a worksheet where columns A:D
> (only) are populated with a variable amount of data -- could be three rows,
[quoted text clipped - 15 lines]
> programmatically)?  It's screwing me up when I try to import the weird
> worksheet into MSAccess.
LarryP - 18 Sep 2007 16:08 GMT
Nope, same result.  I set E:IV cell format to General, but Ctrl-Shift-End
still selects A1:IV50.

> i just check a mock sheet trying selecting coulumns e:iv and going to format
> and reseting to general.
[quoted text clipped - 18 lines]
> > programmatically)?  It's screwing me up when I try to import the weird
> > worksheet into MSAccess.
PCLIVE - 18 Sep 2007 16:03 GMT
Select those empty rows again and go to Format-Conditional Formatting.
Select Delete and check all three condistions.  Click OK.  OK again.
Now try it.

Regards,
Paul

> PUZZLE OF THE DAY:  I have an Excel file with a worksheet where columns
> A:D
[quoted text clipped - 19 lines]
> programmatically)?  It's screwing me up when I try to import the weird
> worksheet into MSAccess.
Jim Thomlinson - 18 Sep 2007 16:24 GMT
You deleted the columns E through IV. You need to save the book however to
reset the used range. Note that the used range is not always what you would
think it is so you want to be careful using it in code... Here is some code
to find the bottom right most cell without refering to the used range...

Public Function LastCell(Optional ByVal wks As Worksheet) As Range
   Dim lngLastRow As Long
   Dim intLastColumn As Integer
   
   If wks Is Nothing Then Set wks = ActiveSheet
   On Error Resume Next
   lngLastRow = wks.Cells.Find(What:="*", _
                           After:=wks.Range("A1"), _
                           Lookat:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByRows, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Row
   intLastColumn = wks.Cells.Find(What:="*", _
                           After:=wks.Range("A1"), _
                           Lookat:=xlPart, _
                           LookIn:=xlFormulas, _
                           SearchOrder:=xlByColumns, _
                           SearchDirection:=xlPrevious, _
                           MatchCase:=False).Column
   On Error GoTo 0
   If lngLastRow = 0 Then
       lngLastRow = 1
       intLastColumn = 1
   End If
   Set LastCell = wks.Cells(lngLastRow, intLastColumn)
   
End Function
Signature

HTH...

Jim Thomlinson

> PUZZLE OF THE DAY:  I have an Excel file with a worksheet where columns A:D
> (only) are populated with a variable amount of data -- could be three rows,
[quoted text clipped - 15 lines]
> programmatically)?  It's screwing me up when I try to import the weird
> worksheet into MSAccess.
LarryP - 18 Sep 2007 16:42 GMT
Thanks, Jim (and others who responded).  My immediate problem is solved, but
Jim, your reply leads me off on a tangent.  What does your function do that
UsedRange does not do -- i.e., in what situation will your function give a
right answer where UsedRange won't?  Is this about UsedRange counting cells
that have been formatted even if they are blank?

> You deleted the columns E through IV. You need to save the book however to
> reset the used range. Note that the used range is not always what you would
[quoted text clipped - 49 lines]
> > programmatically)?  It's screwing me up when I try to import the weird
> > worksheet into MSAccess.
Jim Thomlinson - 18 Sep 2007 17:20 GMT
The difference is that my function will alway return the last cell in the
range that contains values. It looks backwards from Cell A1 to find the last
used row with something in it and the last column with something in it. It
then creates a range object from that row and column address. Used range is
correct in most cases but not always.

What you think of as cells is really just a bunch of place holders that can
hold cell objects. Most of those objects have not been created. When you
place a value (or formula or???) in them then they get created. When you
delete the value (or ???) they usually get destroyed. If they don't get
destroyed for some reason then your used range is going to be wrong. By
deleteing the range with the errant cells you delete the object but don't
destroy it. For the destructor to run you need to save the sheet (depending
on your version of XL). I am not sure that this is the best explanation but
hopefully you get the jist of it...
Signature

HTH...

Jim Thomlinson

> Thanks, Jim (and others who responded).  My immediate problem is solved, but
> Jim, your reply leads me off on a tangent.  What does your function do that
[quoted text clipped - 55 lines]
> > > programmatically)?  It's screwing me up when I try to import the weird
> > > worksheet into MSAccess.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.