Hi

I need to sort a range of cells to put in numerical order. I recorded a
macro but when I transfered it to my userform button I hit problems

The following code sorts but I get a runtime error 13 type mismatch when I
exit the userform.
When I debug it has failed when it should be loading another userform

Sub Mysort1()

Sheets("Lists").Select
Range("CM38:CM42").Select
Selection.Sort Key1:=Range("CM38"), Order1:=xlAscending, Header:=xlGuess
_
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal
End Sub

I tried removing the selection part and used

Sheets("Lists").Range("CM38:CM42").Sort Key1:=Range("CM38"),
Order1:=xlAscending, Header:=xlGuess _
, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
DataOption1:=xlSortNormal

This time sorting does not take place and I get runtime 1004
The sort reference is not valid. Make sure that it's within the data you
want to sort, and the first SortBy box isn't the same or blank

I have searched the web but have not found a solution

Can anyone see what I am doing wrong.

Kenny
XP pro and office 2003

RE: VBA code sort problems - recorded macro does not work with userfor by Wigi

Wigi
Tue Jul 22 16:30:20 CDT 2008

Hello

Sheets("Lists").Range("CM38:CM42").Sort Key1:=Sheets("Lists").Range("CM38")

...



--
Wigi
http://www.wimgielis.be = Excel/VBA, soccer and music


"Forum Freak" wrote:

> Hi
>
> I need to sort a range of cells to put in numerical order. I recorded a
> macro but when I transfered it to my userform button I hit problems
>
> The following code sorts but I get a runtime error 13 type mismatch when I
> exit the userform.
> When I debug it has failed when it should be loading another userform
>
> Sub Mysort1()
>
> Sheets("Lists").Select
> Range("CM38:CM42").Select
> Selection.Sort Key1:=Range("CM38"), Order1:=xlAscending, Header:=xlGuess
> _
> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
> End Sub
>
> I tried removing the selection part and used
>
> Sheets("Lists").Range("CM38:CM42").Sort Key1:=Range("CM38"),
> Order1:=xlAscending, Header:=xlGuess _
> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
>
> This time sorting does not take place and I get runtime 1004
> The sort reference is not valid. Make sure that it's within the data you
> want to sort, and the first SortBy box isn't the same or blank
>
> I have searched the web but have not found a solution
>
> Can anyone see what I am doing wrong.
>
> Kenny
> XP pro and office 2003
>
>
>

Re: VBA code sort problems - recorded macro does not work with userfor by Forum

Forum
Tue Jul 22 16:44:30 CDT 2008

Hi Wigi

Many many thanks for your response. A quick test shows it works. I cannot
thank you enough as I have wasted 2 days searching the net and got nothing
but a headache!

Regards
Kenny
"Wigi" <Wigi@discussions.microsoft.com> wrote in message
news:06B8B960-4242-43C7-BD9C-DD85FD4263C6@microsoft.com...
> Hello
>
> Sheets("Lists").Range("CM38:CM42").Sort
> Key1:=Sheets("Lists").Range("CM38")
>
> ...
>
>
>
> --
> Wigi
> http://www.wimgielis.be = Excel/VBA, soccer and music
>
>
> "Forum Freak" wrote:
>
>> Hi
>>
>> I need to sort a range of cells to put in numerical order. I recorded a
>> macro but when I transfered it to my userform button I hit problems
>>
>> The following code sorts but I get a runtime error 13 type mismatch when
>> I
>> exit the userform.
>> When I debug it has failed when it should be loading another userform
>>
>> Sub Mysort1()
>>
>> Sheets("Lists").Select
>> Range("CM38:CM42").Select
>> Selection.Sort Key1:=Range("CM38"), Order1:=xlAscending,
>> Header:=xlGuess
>> _
>> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
>> DataOption1:=xlSortNormal
>> End Sub
>>
>> I tried removing the selection part and used
>>
>> Sheets("Lists").Range("CM38:CM42").Sort Key1:=Range("CM38"),
>> Order1:=xlAscending, Header:=xlGuess _
>> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
>> DataOption1:=xlSortNormal
>>
>> This time sorting does not take place and I get runtime 1004
>> The sort reference is not valid. Make sure that it's within the data you
>> want to sort, and the first SortBy box isn't the same or blank
>>
>> I have searched the web but have not found a solution
>>
>> Can anyone see what I am doing wrong.
>>
>> Kenny
>> XP pro and office 2003
>>
>>
>>



Re: VBA code sort problems - recorded macro does not work with userform by Dick

Dick
Tue Jul 22 22:16:52 CDT 2008

On Tue, 22 Jul 2008 19:45:57 +0100, "Forum Freak"
<forumfreak@nospamntlworld.com> wrote:

>
>I tried removing the selection part and used
>
> Sheets("Lists").Range("CM38:CM42").Sort Key1:=Range("CM38"),
>Order1:=xlAscending, Header:=xlGuess _
> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
> DataOption1:=xlSortNormal
>
>This time sorting does not take place and I get runtime 1004
>The sort reference is not valid. Make sure that it's within the data you
>want to sort, and the first SortBy box isn't the same or blank
>

If Lists is not the activesheet, then Range("CM38") will point to whatever
sheet is active and it won't be in the sort range. Use this

With ThisWorkbook.Sheets("Lists")
.Range("CM38:CM42").Sort Key1:=.Range("CM38"), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, _
Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
End With

Note the "." in front of the key1 range.
--
Dick Kusleika
Microsoft MVP-Excel
http://www.dailydoseofexcel.com

Re: VBA code sort problems - recorded macro does not work with userform by Forum

Forum
Wed Jul 23 01:20:58 CDT 2008

Hi Dick

Thanks for the post - The fact that you explained why it did not work
helped. Indeed the sheet was not the activesheet.

Any chance you could look at my post of 18/7/08 (20:30) - replace microsoft
forms messagebox with something more meaningful -

Regards
Kenny

"Dick Kusleika" <dkusleika@gmail.com> wrote in message
news:0c8d84lakv5pqan1k818stfq6v5kiug3j5@4ax.com...
> On Tue, 22 Jul 2008 19:45:57 +0100, "Forum Freak"
> <forumfreak@nospamntlworld.com> wrote:
>
>>
>>I tried removing the selection part and used
>>
>> Sheets("Lists").Range("CM38:CM42").Sort Key1:=Range("CM38"),
>>Order1:=xlAscending, Header:=xlGuess _
>> , OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
>> DataOption1:=xlSortNormal
>>
>>This time sorting does not take place and I get runtime 1004
>>The sort reference is not valid. Make sure that it's within the data you
>>want to sort, and the first SortBy box isn't the same or blank
>>
>
> If Lists is not the activesheet, then Range("CM38") will point to whatever
> sheet is active and it won't be in the sort range. Use this
>
> With ThisWorkbook.Sheets("Lists")
> .Range("CM38:CM42").Sort Key1:=.Range("CM38"), _
> Order1:=xlAscending, Header:=xlGuess, _
> OrderCustom:=1, MatchCase:=False, _
> Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
> End With
>
> Note the "." in front of the key1 range.
> --
> Dick Kusleika
> Microsoft MVP-Excel
> http://www.dailydoseofexcel.com