Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Run-time error '1004': Select method of Range class failed

This simple code breaks on line 7 with the above error message. I cannot for
the life of me figure out why. Can anyone help? Thanx in advance

1 Private Sub Worksheet_Change(ByVal Target As Range)
2 If Target.Address = "$E$10" Then
3 Application.ScreenUpdating = False
4 Select Case Target.Value
5 Case "N/A"
6 Sheets("QBQuery1_1Criteria").Select
7 Range("O1:O530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Case "All Projects Actual"
Sheets("QBQuery1_1Criteria").Select
Range("P1:P530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Select
Sheets("Residential_Estimator").Select
End If
End Sub

--
paul
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Run-time error '1004': Select method of Range class failed

When code is in a General module, then the unqualified range will refer to the
activesheet. But in code that is behind a worksheet, any unqualified ranges
will refer to the sheet with the code.

So say this code is behind "sheet1"

Sheets("QBQuery1_1Criteria").Select
Range("O1:O530").Select

This is equivalent to:

Sheets("QBQuery1_1Criteria").Select
sheets("sheet1").Range("O1:O530").Select

And you can only select a range on a sheet that is active.

You could use this:

Sheets("QBQuery1_1Criteria").Select
sheets("QBQuery1_1Criteria").Range("O1:O530").Sele ct

But in general, there are better ways to do things than by selecting them:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$10" Then
'since we're not selecting, no reason to hide the changes
'Application.ScreenUpdating = False
Select Case Target.Value
Case "N/A"
with Sheets("QBQuery1_1Criteria")
.Range("O1:O530").Copy _
destination:=.range("K1")
end with
Case "All Projects Actual"
with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=.range("K1")
end with
End Select
'Application.ScreenUpdating = True
'and then there's no reason to have to select the original sheet
'Sheets("Residential_Estimator").Select
End If
End Sub

I assumed that K1 was on the same QBQuery1_1Criteria worksheet. If K1 was
supposed to be on the sheet that owned the code, change this:

with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=me.range("K1")
end with

(in both places. Me is the object that owns the code--in this case, the
worksheet.)

pglufkin wrote:

This simple code breaks on line 7 with the above error message. I cannot for
the life of me figure out why. Can anyone help? Thanx in advance

1 Private Sub Worksheet_Change(ByVal Target As Range)
2 If Target.Address = "$E$10" Then
3 Application.ScreenUpdating = False
4 Select Case Target.Value
5 Case "N/A"
6 Sheets("QBQuery1_1Criteria").Select
7 Range("O1:O530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Case "All Projects Actual"
Sheets("QBQuery1_1Criteria").Select
Range("P1:P530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Select
Sheets("Residential_Estimator").Select
End If
End Sub

--
paul


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default Run-time error '1004': Select method of Range class failed

Dave, you nailed it! Thank you.

I need to spend some time and brush up on my theory . . . . can you
recommend a good VBA soup to nuts book which can take me from upper level
beginner to upper level intermediate?

Thanks again!

Paul
--
paul


"Dave Peterson" wrote:

When code is in a General module, then the unqualified range will refer to the
activesheet. But in code that is behind a worksheet, any unqualified ranges
will refer to the sheet with the code.

So say this code is behind "sheet1"

Sheets("QBQuery1_1Criteria").Select
Range("O1:O530").Select

This is equivalent to:

Sheets("QBQuery1_1Criteria").Select
sheets("sheet1").Range("O1:O530").Select

And you can only select a range on a sheet that is active.

You could use this:

Sheets("QBQuery1_1Criteria").Select
sheets("QBQuery1_1Criteria").Range("O1:O530").Sele ct

But in general, there are better ways to do things than by selecting them:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$10" Then
'since we're not selecting, no reason to hide the changes
'Application.ScreenUpdating = False
Select Case Target.Value
Case "N/A"
with Sheets("QBQuery1_1Criteria")
.Range("O1:O530").Copy _
destination:=.range("K1")
end with
Case "All Projects Actual"
with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=.range("K1")
end with
End Select
'Application.ScreenUpdating = True
'and then there's no reason to have to select the original sheet
'Sheets("Residential_Estimator").Select
End If
End Sub

I assumed that K1 was on the same QBQuery1_1Criteria worksheet. If K1 was
supposed to be on the sheet that owned the code, change this:

with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=me.range("K1")
end with

(in both places. Me is the object that owns the code--in this case, the
worksheet.)

pglufkin wrote:

This simple code breaks on line 7 with the above error message. I cannot for
the life of me figure out why. Can anyone help? Thanx in advance

1 Private Sub Worksheet_Change(ByVal Target As Range)
2 If Target.Address = "$E$10" Then
3 Application.ScreenUpdating = False
4 Select Case Target.Value
5 Case "N/A"
6 Sheets("QBQuery1_1Criteria").Select
7 Range("O1:O530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Case "All Projects Actual"
Sheets("QBQuery1_1Criteria").Select
Range("P1:P530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Select
Sheets("Residential_Estimator").Select
End If
End Sub

--
paul


--

Dave Peterson

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Run-time error '1004': Select method of Range class failed

Debra Dalgleish has a list of books at her site:
http://www.contextures.com/xlbooks.html

John Walkenbach's is a nice one to start with.

See if you can find them in your local bookstore/internet site and you can
choose what one you like best.

pglufkin wrote:

Dave, you nailed it! Thank you.

I need to spend some time and brush up on my theory . . . . can you
recommend a good VBA soup to nuts book which can take me from upper level
beginner to upper level intermediate?

Thanks again!

Paul
--
paul

"Dave Peterson" wrote:

When code is in a General module, then the unqualified range will refer to the
activesheet. But in code that is behind a worksheet, any unqualified ranges
will refer to the sheet with the code.

So say this code is behind "sheet1"

Sheets("QBQuery1_1Criteria").Select
Range("O1:O530").Select

This is equivalent to:

Sheets("QBQuery1_1Criteria").Select
sheets("sheet1").Range("O1:O530").Select

And you can only select a range on a sheet that is active.

You could use this:

Sheets("QBQuery1_1Criteria").Select
sheets("QBQuery1_1Criteria").Range("O1:O530").Sele ct

But in general, there are better ways to do things than by selecting them:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$E$10" Then
'since we're not selecting, no reason to hide the changes
'Application.ScreenUpdating = False
Select Case Target.Value
Case "N/A"
with Sheets("QBQuery1_1Criteria")
.Range("O1:O530").Copy _
destination:=.range("K1")
end with
Case "All Projects Actual"
with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=.range("K1")
end with
End Select
'Application.ScreenUpdating = True
'and then there's no reason to have to select the original sheet
'Sheets("Residential_Estimator").Select
End If
End Sub

I assumed that K1 was on the same QBQuery1_1Criteria worksheet. If K1 was
supposed to be on the sheet that owned the code, change this:

with Sheets("QBQuery1_1Criteria")
.Range("P1:P530").Copy _
destination:=me.range("K1")
end with

(in both places. Me is the object that owns the code--in this case, the
worksheet.)

pglufkin wrote:

This simple code breaks on line 7 with the above error message. I cannot for
the life of me figure out why. Can anyone help? Thanx in advance

1 Private Sub Worksheet_Change(ByVal Target As Range)
2 If Target.Address = "$E$10" Then
3 Application.ScreenUpdating = False
4 Select Case Target.Value
5 Case "N/A"
6 Sheets("QBQuery1_1Criteria").Select
7 Range("O1:O530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Case "All Projects Actual"
Sheets("QBQuery1_1Criteria").Select
Range("P1:P530").Select
Selection.Copy
Range("K1").Select
ActiveSheet.Paste
Application.ScreenUpdating = True
End Select
Sheets("Residential_Estimator").Select
End If
End Sub

--
paul


--

Dave Peterson


--

Dave Peterson
Reply
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
Run-Time error '1004' : Select method of Range class failed [email protected] Excel Discussion (Misc queries) 3 March 9th 07 01:36 PM
Run-time error "1004" Select method of range class failed Tallan Excel Discussion (Misc queries) 3 March 7th 07 05:22 PM
Select method of range class failed, Run time error 1004 smit127 Excel Programming 1 September 1st 05 12:43 PM
error 1004 Select method of Range class failed J.E. McGimpsey Excel Programming 1 September 12th 03 07:42 PM


All times are GMT +1. The time now is 06:08 PM.

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

About Us

"It's about Microsoft Excel"