ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Discussion (Misc queries) (https://www.excelbanter.com/excel-discussion-misc-queries/)
-   -   Help with code: (https://www.excelbanter.com/excel-discussion-misc-queries/106188-help-code.html)

pinmaster

Help with code:
 
Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy

Don Guillett

Help with code:
 
Your code worked as written. Perhaps you have a problem with the sheet name.
Spaces???
You may like this if you don't need to select. Notice the dot ( . ) before
range in BOTH cases.

Sub sortit()
With ActiveWorkbook.Sheets("1st div")
..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

--
Don Guillett
SalesAid Software

"pinmaster" wrote in message
...
Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy




pinmaster

Help with code:
 
HI Don, tried your code but now I get an "compile error syntax error". Maybe
I should have mentioned that the code I gave works fine on its own, its when
I put the code inside another macro that I get the error. Here's the macro
with the bit of code you gave me.

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Sheets("data").Range("f7").Value = "FALSE"
Sheets("secret partner").EnableCalculation = True
Sheets("entry").Select
Range("b15:b115").ClearContents
Range("d15:d115").ClearContents
Range("g15:h115").ClearContents
With ActiveWorkbook.Sheets("1st div")..Range("B5:B54") _
...Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
Sheets("Entry").Select
Application.ScreenUpdating = True

End Sub

I've beent getting the "select method of range class failed" error a lot
lately, even on something as simple as this.
Private Sub CommandButton3_Click()
Sheets("data").Select
Range("a1:b10").Select
End Sub

Is it me or is there something wrong with excel?

Hope you can help
Jean-Guy

"Don Guillett" wrote:

Your code worked as written. Perhaps you have a problem with the sheet name.
Spaces???
You may like this if you don't need to select. Notice the dot ( . ) before
range in BOTH cases.

Sub sortit()
With ActiveWorkbook.Sheets("1st div")
..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

--
Don Guillett
SalesAid Software

"pinmaster" wrote in message
...
Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy





Dave Peterson

Help with code:
 
Just to add to Don's response...

If your code was in a worksheet module, then Range("b4:B54") is unqualified--and
unqualified ranges in a worksheet module will refer to the sheet that owns the
code.

And since you can't select a range on a sheet that isn't active, your code blows
up.

This is different than how things work in a General module. The unqualified
ranges will refer to the activesheet.



pinmaster wrote:

Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy


--

Dave Peterson

Don Guillett

Help with code:
 
Put in a sub in a REGULAR module (as Dave said)

Application.ScreenUpdating = False
Sheets("data").Range("f7").Value = "FALSE"
Sheets("secret partner").EnableCalculation = True ' ??
Sheets("entry").Range("b15:b115,d15:d115,g15:h115" ).ClearContents
'Range("d15:d115").ClearContents 'combined above
'Range("g15:h115").ClearContents

'too many dots .. 2 instead of one
with sheets("1st div").Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With

'Sheets("Entry").Select 'not needed
Application.ScreenUpdating = True


--
Don Guillett
SalesAid Software

"pinmaster" wrote in message
...
HI Don, tried your code but now I get an "compile error syntax error".
Maybe
I should have mentioned that the code I gave works fine on its own, its
when
I put the code inside another macro that I get the error. Here's the macro
with the bit of code you gave me.

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Sheets("data").Range("f7").Value = "FALSE"
Sheets("secret partner").EnableCalculation = True
Sheets("entry").Select
Range("b15:b115").ClearContents
Range("d15:d115").ClearContents
Range("g15:h115").ClearContents
With ActiveWorkbook.Sheets("1st div")..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
Sheets("Entry").Select
Application.ScreenUpdating = True

End Sub

I've beent getting the "select method of range class failed" error a lot
lately, even on something as simple as this.
Private Sub CommandButton3_Click()
Sheets("data").Select
Range("a1:b10").Select
End Sub

Is it me or is there something wrong with excel?

Hope you can help
Jean-Guy

"Don Guillett" wrote:

Your code worked as written. Perhaps you have a problem with the sheet
name.
Spaces???
You may like this if you don't need to select. Notice the dot ( . )
before
range in BOTH cases.

Sub sortit()
With ActiveWorkbook.Sheets("1st div")
..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

--
Don Guillett
SalesAid Software

"pinmaster" wrote in message
...
Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending,
Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy







Dave Peterson

Help with code:
 
Try not to do any selections:

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Sheets("data").Range("f7").Value = "FALSE"
'Sheets("secret partner").EnableCalculation = True
with Sheets("entry")
.Range("b15:b115").ClearContents
.Range("d15:d115").ClearContents
.Range("g15:h115").ClearContents
end with
With ActiveWorkbook.Sheets("1st div")
.Range("B5:B54") _
.Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
Application.ScreenUpdating = True

End Sub

pinmaster wrote:

HI Don, tried your code but now I get an "compile error syntax error". Maybe
I should have mentioned that the code I gave works fine on its own, its when
I put the code inside another macro that I get the error. Here's the macro
with the bit of code you gave me.

Private Sub CommandButton2_Click()
Application.ScreenUpdating = False
Sheets("data").Range("f7").Value = "FALSE"
Sheets("secret partner").EnableCalculation = True
Sheets("entry").Select
Range("b15:b115").ClearContents
Range("d15:d115").ClearContents
Range("g15:h115").ClearContents
With ActiveWorkbook.Sheets("1st div")..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
Sheets("Entry").Select
Application.ScreenUpdating = True

End Sub

I've beent getting the "select method of range class failed" error a lot
lately, even on something as simple as this.
Private Sub CommandButton3_Click()
Sheets("data").Select
Range("a1:b10").Select
End Sub

Is it me or is there something wrong with excel?

Hope you can help
Jean-Guy

"Don Guillett" wrote:

Your code worked as written. Perhaps you have a problem with the sheet name.
Spaces???
You may like this if you don't need to select. Notice the dot ( . ) before
range in BOTH cases.

Sub sortit()
With ActiveWorkbook.Sheets("1st div")
..Range("B5:B54") _
..Sort Key1:=.Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom
End With
End Sub

--
Don Guillett
SalesAid Software

"pinmaster" wrote in message
...
Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess,
_
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy





--

Dave Peterson

pinmaster

Help with code:
 
Hi guys, Don your code worked after all, my mistake was to delete a carriage
return where there wasn't suppose to be one, after correcting that it worked
fine. So thanks for the help.

Dave, I'm still a bit confused about the worksheet vs. the general module
stuff but I'll try to figure that out later, thanks for the info.

Regards
Jean-Guy

"Dave Peterson" wrote:

Just to add to Don's response...

If your code was in a worksheet module, then Range("b4:B54") is unqualified--and
unqualified ranges in a worksheet module will refer to the sheet that owns the
code.

And since you can't select a range on a sheet that isn't active, your code blows
up.

This is different than how things work in a General module. The unqualified
ranges will refer to the activesheet.



pinmaster wrote:

Hi, does anyone know why this would cause an error?

ActiveWorkbook.Sheets("1st div").Select
Range("B5:B54").Select
Selection.Sort Key1:=Range("B5"), Order1:=xlAscending, Header:=xlGuess, _
OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom

error - Select method of range class failed

TIA
Jean-Guy


--

Dave Peterson



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

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com