Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Why does code only works in Sheet1

Status
Not open for further replies.

wmbb

Technical User
Jul 17, 2005
320
NL
The code below works fine starting the macro from sheet1 but starting the macro from another sheet I get an error message.
[red]methode "range" of object "_worksheet" failed[/red]

Code:
For Each cell In Sheet1.Range(Cells(x, 2), Cells(x, 30))

How to change the code to get the macro working from any sheet in the workbook ?
 
I don't understand it, but when I change [blue]Cells(x,2)[/blue] and [blue]Cells(x,30)[/blue] into [red]Sheet1.Cells(x,2)[/red] and [red]Sheet1.Cells(x,30)[/red] it works.
 


ALL range reference ought to be fully referenced...
Code:
with Sheet 1
   For Each cell In .Range(.Cells(x, 2), .Cells(x, 30))

   next
end with


Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Again, thanks for your help.
I'm learning every time I visit this forum.
 


oops, I had a extra SPACE in Sheet1. [blush]

Skip,

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Okay, I understand.
This is the other way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top