LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Sort referencing incorrect worksheet

Try this code:

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim TotalRows As Long
Dim TotalCols As Long

Set wks = Worksheets.Add

With wks
.Range("x21:z30").Value = "x"

TotalRows = .UsedRange.Rows.Count
TotalCols = .UsedRange.Columns.Count
MsgBox "Test#1:" & vbLf & _
"Column: " & .UsedRange.Columns.Count & vbLf & _
"row: " & .UsedRange.Columns.Count & vbLf & _
"Address: " & .Range("A1", .Cells(TotalRows, TotalCols)).Address

With .UsedRange
TotalRows = .Rows(.Rows.Count).Row
TotalCols = .Columns(.Columns.Count).Column
End With
MsgBox "Test#2:" & vbLf & _
"Column: " & .UsedRange.Columns.Count & vbLf & _
"row: " & .UsedRange.Columns.Count & vbLf & _
"Address: " & .Range("A1", .Cells(TotalRows, TotalCols)).Address
End With

End Sub

It creates a new sheet and populates a range with some x's. But A1 doesn't get
populated.

Excel is smart enough to keep track of the usedrange--if you use A1, then clear
it, excel still thinks that the .usedrange starts in A1. But it may not always
be true in all cases.

Sometimes the .usedrange doesn't start in row 1 or column A.


ingineu wrote:

Sorry, forgot to ask ...
And if your data doesn't start in A1. your totalrows and totalcols
variables
may not be what you expect. So I changed it.

I'm not sure what the difference is between the 2 methods. When you
say "data doesn't start in A1", are you referring to blank lines?

--
ingineu
------------------------------------------------------------------------
ingineu's Profile: http://www.excelforum.com/member.php...o&userid=14860
View this thread: http://www.excelforum.com/showthread...hreadid=513944


--

Dave Peterson
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worksheet formula incorrect after copying to another worksheet Karen Excel Worksheet Functions 2 November 12th 09 01:06 AM
incorrect data unexpectly in excel worksheet 40078046 Excel Discussion (Misc queries) 2 August 7th 09 08:16 PM
Incorrect result using NPER worksheet function KG Old Wolf Excel Worksheet Functions 7 November 3rd 08 02:49 AM
Same formula referencing same cells returns incorrect results, randomly, when pasted into new worksheet [email protected] Excel Worksheet Functions 1 March 9th 06 07:55 PM
I have saved an incorrect worksheet over the right one. Emma Excel Worksheet Functions 1 August 16th 05 11:23 AM


All times are GMT +1. The time now is 10:32 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"