LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 34
Default Macro Error When Sorting

We have a large macro. In one of the sub's we sort a
worksheet that has nine columns. The first three colomns
are text and the last six are numeric. For each row there
is only one numeric entry in column 4, 5, 6, 7, 8, or 9.
In the sub-routine below we are sorting column 4, then
column 5, then column 6, etc.

The problem is that the macro bombs if, for example, there
is no entry in column 5.

The error occurs on the 6th line of code, "If cells(1,
MyColumn)....." It seems like the End(xlDown) is
identifying row 65536 and therefore cannot look at Offset
(1,0).

Do we need to insert code before line 6 to test for this
condition somehow?

---------------------------

Range("A2").Select
'Sort the worksheet on column D, the third column.
Range(Selection, ActiveCell.SpecialCells _
(xlLastCell)).Select
Selection.Sort Key1:=Range("D2"), Order1:=xlAscending, _
Header:=xlGuess, OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom

'Sort the worksheet based on columns 4 through 9.
On Error Resume Next
For MyColumn = 4 To 8
If Cells(1, MyColumn).End(xlDown).Offset(1, 0) _
= "" Then
Set NewStCell = Cells(1, MyColumn).End _
(xlDown).Offset(1, -MyColumn + 1)
Else
Set NewStCell = Cells(1, MyColumn).End _
(xlDown).End(xlDown).Offset(1, -MyColumn + 1)
End If

Set RangeToSort = Range(NewStCell, _
Cells(NewStCell.End(xlDown).Row, 9))

RangeToSort.Sort Key1:=Cells(1, MyColumn + 1), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom
Next MyColumn
 
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
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Excel Worksheet Functions 1 May 3rd 08 02:35 PM
I tried to get around the problem of the pivot table field settingdefaulting to Count instead of Sum by running a macro of change the settingfrom Count to Sum. However, when I tried to run the Macro, I got error messageof run time error 1004, unable Enda80 Excel Discussion (Misc queries) 1 May 3rd 08 10:52 AM
VBA Sorting Error Scott Halper Excel Worksheet Functions 1 March 19th 08 01:27 PM
sorting error ananga Excel Discussion (Misc queries) 2 June 30th 05 04:21 PM
sorting error abstsogds Excel Worksheet Functions 2 December 6th 04 07:19 PM


All times are GMT +1. The time now is 11:38 AM.

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

About Us

"It's about Microsoft Excel"