PC Review


Reply
Thread Tools Rate Thread

Bombing on final sort command . . .

 
 
Rich
Guest
Posts: n/a
 
      7th Apr 2009
A user sent me a spreadsheet with a recorded macro. He needed it changed to
work on different worksheets within the workbook.

I changed the macro so that it used "ActiveSheet." instead of a particular
worksheet.

I also changed it so that it finds the area to sort and then assigns a Var
name to the Range.

But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
received while changing things around, the current code (below) generates a
"Run-time error '13':/Type mismatch" error msg. What the heck am I doing
wrong!?

=====================================================
Option Explicit
Sub SortCurrentSheet()
'
' Sort Macro
' Sorts Timesheet data per project number
'
' Keyboard Shortcut: Ctrl+s

Dim RngName As String

'Following code selects the data to be sorted:
Range("A1").Select
RngName = ActiveSheet.Range("A1").CurrentRegion.Address
ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName

ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
Order:=xlAscending, _
DataOption:=xlSortNormal
ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
Order:=xlAscending, _
DataOption:=xlSortNormal
With ActiveSheet.Sort
.SetRange RngName 'THIS LINE CRASHES THE MACRO
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

End Sub
=====================================================

Thank you for any assistnace.

Rich
 
Reply With Quote
 
 
 
 
Per Jessen
Guest
Posts: n/a
 
      7th Apr 2009
Hi

You miss the range statement:

..SetRange Range(RngName)

Regards,
Per

"Rich" <(E-Mail Removed)> skrev i meddelelsen
news:97AD54CB-DED8-4A76-9EB0-(E-Mail Removed)...
>A user sent me a spreadsheet with a recorded macro. He needed it changed to
> work on different worksheets within the workbook.
>
> I changed the macro so that it used "ActiveSheet." instead of a particular
> worksheet.
>
> I also changed it so that it finds the area to sort and then assigns a Var
> name to the Range.
>
> But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> received while changing things around, the current code (below) generates
> a
> "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> wrong!?
>
> =====================================================
> Option Explicit
> Sub SortCurrentSheet()
> '
> ' Sort Macro
> ' Sorts Timesheet data per project number
> '
> ' Keyboard Shortcut: Ctrl+s
>
> Dim RngName As String
>
> 'Following code selects the data to be sorted:
> Range("A1").Select
> RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
>
> ActiveSheet.Sort.SortFields.Clear
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> With ActiveSheet.Sort
> .SetRange RngName 'THIS LINE CRASHES THE MACRO
> .Header = xlYes
> .MatchCase = False
> .Orientation = xlTopToBottom
> .SortMethod = xlPinYin
> .Apply
> End With
>
> End Sub
> =====================================================
>
> Thank you for any assistnace.
>
> Rich


 
Reply With Quote
 
joel
Guest
Posts: n/a
 
      7th Apr 2009
Try this

Sub SortCurrentSheet()
'
' Sort Macro
' Sorts Timesheet data per project number
'
' Keyboard Shortcut: Ctrl+s

Dim RngName As String

'Following code selects the data to be sorted:
Range("A1").Select
RngName = ActiveSheet.Range("A1").CurrentRegion.Address
ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName

Set SortRange = ActiveSheet.Range("A1").CurrentRegion

SortRange.Sort _
key1:=Range("E1"), _
Order1:=xlAscending, _
key2:=Range("F1"), _
Order2:=x_lAscending, _
key2:=Range("G1"), _
Order2:=xlAscending, _
Header:=xlYes
End Sub

"Rich" wrote:

> A user sent me a spreadsheet with a recorded macro. He needed it changed to
> work on different worksheets within the workbook.
>
> I changed the macro so that it used "ActiveSheet." instead of a particular
> worksheet.
>
> I also changed it so that it finds the area to sort and then assigns a Var
> name to the Range.
>
> But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> received while changing things around, the current code (below) generates a
> "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> wrong!?
>
> =====================================================
> Option Explicit
> Sub SortCurrentSheet()
> '
> ' Sort Macro
> ' Sorts Timesheet data per project number
> '
> ' Keyboard Shortcut: Ctrl+s
>
> Dim RngName As String
>
> 'Following code selects the data to be sorted:
> Range("A1").Select
> RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
>
> ActiveSheet.Sort.SortFields.Clear
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> Order:=xlAscending, _
> DataOption:=xlSortNormal
> With ActiveSheet.Sort
> .SetRange RngName 'THIS LINE CRASHES THE MACRO
> .Header = xlYes
> .MatchCase = False
> .Orientation = xlTopToBottom
> .SortMethod = xlPinYin
> .Apply
> End With
>
> End Sub
> =====================================================
>
> Thank you for any assistnace.
>
> Rich

 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      7th Apr 2009
The goofy this is that I had tried it that way; when I did, I received an
error on the ".Apply" line, which gave me an "Run-time error '1004':/The sort
reference is not valid. Make sure that it's within the data you want to sort,
and the first Sort By box isn't the same or blank."

I'm figuring that I must be defining and/or stating something incorrectly.

I'm trying a few more things, but the reply from "Joel" worked, with (oddly)
one slight modification.

I'm still new to VBA and don't do it that often to really understand nearly
as much as I need to!

Thanks again!

Rich

"Per Jessen" wrote:

> Hi
>
> You miss the range statement:
>
> ..SetRange Range(RngName)
>
> Regards,
> Per
>
> "Rich" <(E-Mail Removed)> skrev i meddelelsen
> news:97AD54CB-DED8-4A76-9EB0-(E-Mail Removed)...
> >A user sent me a spreadsheet with a recorded macro. He needed it changed to
> > work on different worksheets within the workbook.
> >
> > I changed the macro so that it used "ActiveSheet." instead of a particular
> > worksheet.
> >
> > I also changed it so that it finds the area to sort and then assigns a Var
> > name to the Range.
> >
> > But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> > CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> > received while changing things around, the current code (below) generates
> > a
> > "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> > wrong!?
> >
> > =====================================================
> > Option Explicit
> > Sub SortCurrentSheet()
> > '
> > ' Sort Macro
> > ' Sorts Timesheet data per project number
> > '
> > ' Keyboard Shortcut: Ctrl+s
> >
> > Dim RngName As String
> >
> > 'Following code selects the data to be sorted:
> > Range("A1").Select
> > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> >
> > ActiveSheet.Sort.SortFields.Clear
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > With ActiveSheet.Sort
> > .SetRange RngName 'THIS LINE CRASHES THE MACRO
> > .Header = xlYes
> > .MatchCase = False
> > .Orientation = xlTopToBottom
> > .SortMethod = xlPinYin
> > .Apply
> > End With
> >
> > End Sub
> > =====================================================
> >
> > Thank you for any assistnace.
> >
> > Rich

>
>

 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      7th Apr 2009
It worked, Joel . . . but I had to make one modification . . .

It didn't like the variable (even with "Option Explicit" rem'd out!). So I
put replaced the var "SortRange" with the full statement. It worked find.

Thank you.

Rich

"joel" wrote:

> Try this
>
> Sub SortCurrentSheet()
> '
> ' Sort Macro
> ' Sorts Timesheet data per project number
> '
> ' Keyboard Shortcut: Ctrl+s
>
> Dim RngName As String
>
> 'Following code selects the data to be sorted:
> Range("A1").Select
> RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
>
> Set SortRange = ActiveSheet.Range("A1").CurrentRegion
>
> SortRange.Sort _
> key1:=Range("E1"), _
> Order1:=xlAscending, _
> key2:=Range("F1"), _
> Order2:=x_lAscending, _
> key2:=Range("G1"), _
> Order2:=xlAscending, _
> Header:=xlYes
> End Sub
>
> "Rich" wrote:
>
> > A user sent me a spreadsheet with a recorded macro. He needed it changed to
> > work on different worksheets within the workbook.
> >
> > I changed the macro so that it used "ActiveSheet." instead of a particular
> > worksheet.
> >
> > I also changed it so that it finds the area to sort and then assigns a Var
> > name to the Range.
> >
> > But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> > CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> > received while changing things around, the current code (below) generates a
> > "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> > wrong!?
> >
> > =====================================================
> > Option Explicit
> > Sub SortCurrentSheet()
> > '
> > ' Sort Macro
> > ' Sorts Timesheet data per project number
> > '
> > ' Keyboard Shortcut: Ctrl+s
> >
> > Dim RngName As String
> >
> > 'Following code selects the data to be sorted:
> > Range("A1").Select
> > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> >
> > ActiveSheet.Sort.SortFields.Clear
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> > Order:=xlAscending, _
> > DataOption:=xlSortNormal
> > With ActiveSheet.Sort
> > .SetRange RngName 'THIS LINE CRASHES THE MACRO
> > .Header = xlYes
> > .MatchCase = False
> > .Orientation = xlTopToBottom
> > .SortMethod = xlPinYin
> > .Apply
> > End With
> >
> > End Sub
> > =====================================================
> >
> > Thank you for any assistnace.
> >
> > Rich

 
Reply With Quote
 
joel
Guest
Posts: n/a
 
      7th Apr 2009
You need to change the last sort order and key from 2 to 3. I copied the
line and forgot to make the change. I don't like the SORT fields method in
VBA. The sort command the way I did it is much simplier.

"Rich" wrote:

> It worked, Joel . . . but I had to make one modification . . .
>
> It didn't like the variable (even with "Option Explicit" rem'd out!). So I
> put replaced the var "SortRange" with the full statement. It worked find.
>
> Thank you.
>
> Rich
>
> "joel" wrote:
>
> > Try this
> >
> > Sub SortCurrentSheet()
> > '
> > ' Sort Macro
> > ' Sorts Timesheet data per project number
> > '
> > ' Keyboard Shortcut: Ctrl+s
> >
> > Dim RngName As String
> >
> > 'Following code selects the data to be sorted:
> > Range("A1").Select
> > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> >
> > Set SortRange = ActiveSheet.Range("A1").CurrentRegion
> >
> > SortRange.Sort _
> > key1:=Range("E1"), _
> > Order1:=xlAscending, _
> > key2:=Range("F1"), _
> > Order2:=x_lAscending, _
> > key2:=Range("G1"), _
> > Order2:=xlAscending, _
> > Header:=xlYes
> > End Sub
> >
> > "Rich" wrote:
> >
> > > A user sent me a spreadsheet with a recorded macro. He needed it changed to
> > > work on different worksheets within the workbook.
> > >
> > > I changed the macro so that it used "ActiveSheet." instead of a particular
> > > worksheet.
> > >
> > > I also changed it so that it finds the area to sort and then assigns a Var
> > > name to the Range.
> > >
> > > But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> > > CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> > > received while changing things around, the current code (below) generates a
> > > "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> > > wrong!?
> > >
> > > =====================================================
> > > Option Explicit
> > > Sub SortCurrentSheet()
> > > '
> > > ' Sort Macro
> > > ' Sorts Timesheet data per project number
> > > '
> > > ' Keyboard Shortcut: Ctrl+s
> > >
> > > Dim RngName As String
> > >
> > > 'Following code selects the data to be sorted:
> > > Range("A1").Select
> > > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> > >
> > > ActiveSheet.Sort.SortFields.Clear
> > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> > > Order:=xlAscending, _
> > > DataOption:=xlSortNormal
> > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> > > Order:=xlAscending, _
> > > DataOption:=xlSortNormal
> > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> > > Order:=xlAscending, _
> > > DataOption:=xlSortNormal
> > > With ActiveSheet.Sort
> > > .SetRange RngName 'THIS LINE CRASHES THE MACRO
> > > .Header = xlYes
> > > .MatchCase = False
> > > .Orientation = xlTopToBottom
> > > .SortMethod = xlPinYin
> > > .Apply
> > > End With
> > >
> > > End Sub
> > > =====================================================
> > >
> > > Thank you for any assistnace.
> > >
> > > Rich

 
Reply With Quote
 
gimme_this_gimme_that@yahoo.com
Guest
Posts: n/a
 
      7th Apr 2009
With ActiveSheet.Sort
.SetRange "TestRange" ' you must use a range name not an
address
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With

 
Reply With Quote
 
Rich
Guest
Posts: n/a
 
      7th Apr 2009
Thank you . . . good eye for your own work. Will do!

"joel" wrote:

> You need to change the last sort order and key from 2 to 3. I copied the
> line and forgot to make the change. I don't like the SORT fields method in
> VBA. The sort command the way I did it is much simplier.
>
> "Rich" wrote:
>
> > It worked, Joel . . . but I had to make one modification . . .
> >
> > It didn't like the variable (even with "Option Explicit" rem'd out!). So I
> > put replaced the var "SortRange" with the full statement. It worked find.
> >
> > Thank you.
> >
> > Rich
> >
> > "joel" wrote:
> >
> > > Try this
> > >
> > > Sub SortCurrentSheet()
> > > '
> > > ' Sort Macro
> > > ' Sorts Timesheet data per project number
> > > '
> > > ' Keyboard Shortcut: Ctrl+s
> > >
> > > Dim RngName As String
> > >
> > > 'Following code selects the data to be sorted:
> > > Range("A1").Select
> > > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> > >
> > > Set SortRange = ActiveSheet.Range("A1").CurrentRegion
> > >
> > > SortRange.Sort _
> > > key1:=Range("E1"), _
> > > Order1:=xlAscending, _
> > > key2:=Range("F1"), _
> > > Order2:=x_lAscending, _
> > > key2:=Range("G1"), _
> > > Order2:=xlAscending, _
> > > Header:=xlYes
> > > End Sub
> > >
> > > "Rich" wrote:
> > >
> > > > A user sent me a spreadsheet with a recorded macro. He needed it changed to
> > > > work on different worksheets within the workbook.
> > > >
> > > > I changed the macro so that it used "ActiveSheet." instead of a particular
> > > > worksheet.
> > > >
> > > > I also changed it so that it finds the area to sort and then assigns a Var
> > > > name to the Range.
> > > >
> > > > But, now it bombs on a particular line, which I have rem'd as, "THIS LINE
> > > > CRASHES THE MACRO" in the below code. Amongst the various error msgs I've
> > > > received while changing things around, the current code (below) generates a
> > > > "Run-time error '13':/Type mismatch" error msg. What the heck am I doing
> > > > wrong!?
> > > >
> > > > =====================================================
> > > > Option Explicit
> > > > Sub SortCurrentSheet()
> > > > '
> > > > ' Sort Macro
> > > > ' Sorts Timesheet data per project number
> > > > '
> > > > ' Keyboard Shortcut: Ctrl+s
> > > >
> > > > Dim RngName As String
> > > >
> > > > 'Following code selects the data to be sorted:
> > > > Range("A1").Select
> > > > RngName = ActiveSheet.Range("A1").CurrentRegion.Address
> > > > ActiveWorkbook.Names.Add Name:="TestName", RefersToR1C1:=RngName
> > > >
> > > > ActiveSheet.Sort.SortFields.Clear
> > > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > > Offset(0, 4).Range(RngName), SortOn:=xlSortOnValues,
> > > > Order:=xlAscending, _
> > > > DataOption:=xlSortNormal
> > > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > > Offset(0, 5).Range(RngName), SortOn:=xlSortOnValues,
> > > > Order:=xlAscending, _
> > > > DataOption:=xlSortNormal
> > > > ActiveSheet.Sort.SortFields.Add Key:=ActiveCell. _
> > > > Offset(0, 6).Range(RngName), SortOn:=xlSortOnValues,
> > > > Order:=xlAscending, _
> > > > DataOption:=xlSortNormal
> > > > With ActiveSheet.Sort
> > > > .SetRange RngName 'THIS LINE CRASHES THE MACRO
> > > > .Header = xlYes
> > > > .MatchCase = False
> > > > .Orientation = xlTopToBottom
> > > > .SortMethod = xlPinYin
> > > > .Apply
> > > > End With
> > > >
> > > > End Sub
> > > > =====================================================
> > > >
> > > > Thank you for any assistnace.
> > > >
> > > > Rich

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
I have a Table that I can't Sort--Final attempt for help Mike Microsoft Excel Misc 9 6th Jan 2009 04:06 AM
Problem sync'g Outlook 2007 final with ActiveSync v.4.2 final =?Utf-8?B?VGhlIFN1aXRl?= Microsoft Outlook Installation 0 6th Dec 2006 04:39 AM
Outlook 2007 (final), Vista (final) and Exchange 2003 =?Utf-8?B?QWxleEw=?= Microsoft Outlook Installation 1 24th Nov 2006 04:15 PM
Sort command does not sort some columns? =?Utf-8?B?QmlsbHlCb2I=?= Microsoft Excel New Users 5 20th Jun 2006 01:36 PM
Word 2003: track changes opens in final view instead of final sho. =?Utf-8?B?SmVubmlmZXJMZWU=?= Microsoft Word Document Management 2 19th Apr 2005 06:25 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:20 AM.