PC Review


Reply
Thread Tools Rate Thread

Button Won't Work

 
 
=?Utf-8?B?RGF2aWQxMjc=?=
Guest
Posts: n/a
 
      15th Nov 2007
I created a Macro that works fine but when I try to use the code with a
command button it fails to sort - "The sort reference is not valid. Make sure
that it's within the data you want to sort, and the first Sort box isn't the
same or blank".

Private Sub cmdCopyList_MTN3_Click()
Application.Goto Reference:="List_MTN2"
Selection.Copy
Application.Goto Reference:="List_MTN3"
ActiveSheet.Paste
Application.CutCopyMode = False
Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Selection.Copy
Application.Goto Reference:="List_MTN2"
End Sub

Thanks for your help!
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      15th Nov 2007
Those unqualified ranges refer to the sheet that owns the code--not the
activesheet.

(There's a difference in behavior when the code is behind a worksheet and when
the code is in a General module.)

Private Sub cmdCopyList_MTN3_Click()
Application.Goto Reference:="List_MTN2"
Selection.Copy
Application.Goto Reference:="List_MTN3"
ActiveSheet.Paste
Application.CutCopyMode = False

Selection.Sort Key1:=activesheet.Range("A1"), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
Selection.Copy
Application.Goto Reference:="List_MTN2"
End Sub

But maybe you could do it without the .goto's and selection.

Private Sub cmdCopyList_MTN3_Click()

dim List_Mtn2_Rng as range
Dim list_mtn3_Rng as range

set list_mtn2_rng = thisworkbook.names("List_Mtn2").referstorange
set list_mtn3_rng = thisworkbook.names("List_Mtn3").referstorange

'Although, I find this syntax more self-documenting
'Set List_Mtn2_Rng = thisworkbook.worksheets("somesheetnamehere") _
' .range("list_Mtn2")
'
'Set List_Mtn3_Rng = thisworkbook.worksheets("someothersheetnamehere") _
' .range("list_Mtn3")

list_mtn2_rng.copy _
destination:=list_mtn3_rng.cells(1)

Application.CutCopyMode = False

with list_mtn3_rng
.cells.sort key1:=.columns(1), _
Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End with

End Sub

(Untested, uncompiled--watch for typos!)

I bet you know if your data has headers or not. Instead of letting excel guess,
I'd specify that (xlyes or xlno--not xlguess). Why take a chance?

David127 wrote:
>
> I created a Macro that works fine but when I try to use the code with a
> command button it fails to sort - "The sort reference is not valid. Make sure
> that it's within the data you want to sort, and the first Sort box isn't the
> same or blank".
>
> Private Sub cmdCopyList_MTN3_Click()
> Application.Goto Reference:="List_MTN2"
> Selection.Copy
> Application.Goto Reference:="List_MTN3"
> ActiveSheet.Paste
> Application.CutCopyMode = False
> Selection.Sort Key1:=Range("A1"), Order1:=xlAscending, Header:=xlGuess, _
> OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
> Selection.Copy
> Application.Goto Reference:="List_MTN2"
> End Sub
>
> Thanks for your help!


--

Dave Peterson
 
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
F9 button and send/receive click button do not work. =?Utf-8?B?R2VycnlD?= Microsoft Outlook Discussion 0 23rd May 2006 02:56 PM
The Hover Button does not work no button image =?Utf-8?B?R2VvZmZT?= Microsoft Frontpage 1 13th Mar 2006 06:18 PM
Which button event would work the best to disable the button. =?Utf-8?B?VmVybg==?= Microsoft C# .NET 5 6th May 2005 04:39 PM
HTML Reset Button Doesn't work after postback with Submit button Chris Lane Microsoft ASP .NET 4 17th Nov 2003 11:52 PM
Image Button won't work as Accept Button in IDE William Ryan Microsoft Dot NET 0 10th Aug 2003 01:46 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:47 PM.