LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 420
Default How to activate Sheet2?

The problem is that you don't qualify your ranges.

In code like this:
Sheet2.Range(Cells(2, 1), Cells(j, 1)).Name = "filterA"

the unqualified Cells() will behave differently depending on where the code is
located.

If the code is in a General module, then the cells() will refer to the activesheet.

If the code is in a worksheet module, then the cells() will refer to the sheet
that owns the code.

You could qualify the ranges like:

Sheet2.Range(sheet2.Cells(2, 1), sheet2.Cells(j, 1)).Name = "filterA"

or use a with statement:

with Sheet2
.Range(.Cells(2, 1), .Cells(j, 1)).Name = "filterA"
end with



On 03/22/2012 07:01, Jo Stein wrote:
This VBA code works OK if I run it inside the Sheet2,
but it fails if I try to start it with a command-button
located in Sheet1.

Sheet2.Cells(2, 4) = Sheet2.Cells(2, 3)
j = j - 1
Sheet2.Cells(j, 5) = Sheet2.Cells(j, 3)
' Sheets("Sheet2").Activate
Sheet2.Range(Cells(2, 1), Cells(j, 1)).Name = "filterA"
Sheet2.Range(Cells(2, 1), Cells(j, 2)).Name = "filterAB"
End Sub

It fails at this line
Sheet2.Range(Cells(2, 1), Cells(j, 1)).Name = "filterA"
and I get the message
Run-time error '1004'
Method 'Range' of object '_Worksheet' failed

I tried to add this line (it is now a comment)
Sheets("Sheet2").Activate
and that did no good.


--
Dave Peterson
 
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
copy data from sheet2 to sheet1 when sheet2 has variable # of rows Anne Excel Discussion (Misc queries) 6 February 27th 09 09:48 PM
macro to print sheet2 without open sheet2 ramzi Excel Discussion (Misc queries) 1 January 28th 09 12:07 PM
A1 Sheet2 is linked to A1 sheet1 so that user enters value(abc123) a1 sheet1 and A1 sheet2 is updated pano[_3_] Excel Programming 2 October 28th 07 02:32 PM
how do copy "sheet1!A1+1 in sheet2 to sheet 3 and get "sheet2!A1+ Dany Excel Discussion (Misc queries) 5 April 16th 07 03:27 AM
activate macro only in sheet2 Narkom Excel Worksheet Functions 3 March 20th 06 09:21 PM


All times are GMT +1. The time now is 11:46 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"