PC Review


Reply
Thread Tools Rate Thread

calling from one worksheet sheet to source from another

 
 
SteveDB1
Guest
Posts: n/a
 
      9th Apr 2008
Hello.
I'm working on a macro to link two worksheets.
I've written something that appears to work, but when I went to test it I
found that it picks the range on the primary worksheet.
I need to have it select the range on the secondary worksheet.
E.g.,

MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select

Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
input box.

How do I get the MyRngPer to select the range desired from another worksheet?

Thank you.

 
Reply With Quote
 
 
 
 
Joel
Guest
Posts: n/a
 
      9th Apr 2008
The best way to reference cell when more than one workbook is oopen is to
remember at every instruction to reference a workbook.

To reference the workbook where the macro is located use
Thisworkbook.sheets("sheet1").Range("A1").select

When you create a new workbook or open a workbook it autoimatically becomes
the active workbook so do something like this

workbooks.add
set newbk = activeworkbook

or
workbooks.open filename:="C:\temp\abc.xls"
set newbk = activeworkbook

Your code is wrong in selecting a range, you need a set statement. also you
must have a sheet reference.



newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
or
set MyRngPer = newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
MyRngPer.select



then
"SteveDB1" wrote:

> Hello.
> I'm working on a macro to link two worksheets.
> I've written something that appears to work, but when I went to test it I
> found that it picks the range on the primary worksheet.
> I need to have it select the range on the secondary worksheet.
> E.g.,
>
> MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
>
> Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> input box.
>
> How do I get the MyRngPer to select the range desired from another worksheet?
>
> Thank you.
>

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      9th Apr 2008
I just noticed something else. Excel has problem remebering that the Range
and Cell in the statement below are on the same worksheets. You need to do
something like this. Notice I have three dots (before range and cells)

with newbk.sheets("Sheet1")

set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))

end with

"SteveDB1" wrote:

> Hello.
> I'm working on a macro to link two worksheets.
> I've written something that appears to work, but when I went to test it I
> found that it picks the range on the primary worksheet.
> I need to have it select the range on the secondary worksheet.
> E.g.,
>
> MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
>
> Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> input box.
>
> How do I get the MyRngPer to select the range desired from another worksheet?
>
> Thank you.
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Apr 2008
You could even do:
set newbk = workbooks.open(filename:="C:\temp\abc.xls")

And remember if you're going to select a range, then the worksheet has to be
selected and the workbook has to be active.

with newbk.sheets("Sheet1")
.select 'newbk must be active
set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))
myrngper.select
end with


Joel wrote:
>
> The best way to reference cell when more than one workbook is oopen is to
> remember at every instruction to reference a workbook.
>
> To reference the workbook where the macro is located use
> Thisworkbook.sheets("sheet1").Range("A1").select
>
> When you create a new workbook or open a workbook it autoimatically becomes
> the active workbook so do something like this
>
> workbooks.add
> set newbk = activeworkbook
>
> or
> workbooks.open filename:="C:\temp\abc.xls"
> set newbk = activeworkbook
>
> Your code is wrong in selecting a range, you need a set statement. also you
> must have a sheet reference.
>
> newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> or
> set MyRngPer = newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
> MyRngPer.select
>
> then
> "SteveDB1" wrote:
>
> > Hello.
> > I'm working on a macro to link two worksheets.
> > I've written something that appears to work, but when I went to test it I
> > found that it picks the range on the primary worksheet.
> > I need to have it select the range on the secondary worksheet.
> > E.g.,
> >
> > MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> >
> > Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> > input box.
> >
> > How do I get the MyRngPer to select the range desired from another worksheet?
> >
> > Thank you.
> >


--

Dave Peterson
 
Reply With Quote
 
SteveDB1
Guest
Posts: n/a
 
      9th Apr 2008
Hi.
I want to link two worksheets together. These are within the same workbook.

How would I link two worksheets within the same workbook?

I tried the :

set MyRngPer = Sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
MyRngPer.select

and it did not work.
Do I need to include the workbook name?


"Joel" wrote:

> I just noticed something else. Excel has problem remebering that the Range
> and Cell in the statement below are on the same worksheets. You need to do
> something like this. Notice I have three dots (before range and cells)
>
> with newbk.sheets("Sheet1")
>
> set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))
>
> end with
>
> "SteveDB1" wrote:
>
> > Hello.
> > I'm working on a macro to link two worksheets.
> > I've written something that appears to work, but when I went to test it I
> > found that it picks the range on the primary worksheet.
> > I need to have it select the range on the secondary worksheet.
> > E.g.,
> >
> > MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> >
> > Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> > input box.
> >
> > How do I get the MyRngPer to select the range desired from another worksheet?
> >
> > Thank you.
> >

 
Reply With Quote
 
Joel
Guest
Posts: n/a
 
      9th Apr 2008
Dave: You are wrong. Workbook and Sheet do not have to be activated and
selected. The code below works

Thisworkbook.sheets("Sheet1").Range("A5:B10").copy _
destination:=newbk.sheets("Sheet2").Range("A1")


"Dave Peterson" wrote:

> You could even do:
> set newbk = workbooks.open(filename:="C:\temp\abc.xls")
>
> And remember if you're going to select a range, then the worksheet has to be
> selected and the workbook has to be active.
>
> with newbk.sheets("Sheet1")
> .select 'newbk must be active
> set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))
> myrngper.select
> end with
>
>
> Joel wrote:
> >
> > The best way to reference cell when more than one workbook is oopen is to
> > remember at every instruction to reference a workbook.
> >
> > To reference the workbook where the macro is located use
> > Thisworkbook.sheets("sheet1").Range("A1").select
> >
> > When you create a new workbook or open a workbook it autoimatically becomes
> > the active workbook so do something like this
> >
> > workbooks.add
> > set newbk = activeworkbook
> >
> > or
> > workbooks.open filename:="C:\temp\abc.xls"
> > set newbk = activeworkbook
> >
> > Your code is wrong in selecting a range, you need a set statement. also you
> > must have a sheet reference.
> >
> > newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> > or
> > set MyRngPer = newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
> > MyRngPer.select
> >
> > then
> > "SteveDB1" wrote:
> >
> > > Hello.
> > > I'm working on a macro to link two worksheets.
> > > I've written something that appears to work, but when I went to test it I
> > > found that it picks the range on the primary worksheet.
> > > I need to have it select the range on the secondary worksheet.
> > > E.g.,
> > >
> > > MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> > >
> > > Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> > > input box.
> > >
> > > How do I get the MyRngPer to select the range desired from another worksheet?
> > >
> > > Thank you.
> > >

>
> --
>
> Dave Peterson
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Apr 2008
I didn't say that the worksheet and range had to be selected to do the work.

I said that if you select the range, then the worksheet had to be selected.

There's a difference.

Joel wrote:
>
> Dave: You are wrong. Workbook and Sheet do not have to be activated and
> selected. The code below works
>
> Thisworkbook.sheets("Sheet1").Range("A5:B10").copy _
> destination:=newbk.sheets("Sheet2").Range("A1")
>
> "Dave Peterson" wrote:
>
> > You could even do:
> > set newbk = workbooks.open(filename:="C:\temp\abc.xls")
> >
> > And remember if you're going to select a range, then the worksheet has to be
> > selected and the workbook has to be active.
> >
> > with newbk.sheets("Sheet1")
> > .select 'newbk must be active
> > set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))
> > myrngper.select
> > end with
> >
> >
> > Joel wrote:
> > >
> > > The best way to reference cell when more than one workbook is oopen is to
> > > remember at every instruction to reference a workbook.
> > >
> > > To reference the workbook where the macro is located use
> > > Thisworkbook.sheets("sheet1").Range("A1").select
> > >
> > > When you create a new workbook or open a workbook it autoimatically becomes
> > > the active workbook so do something like this
> > >
> > > workbooks.add
> > > set newbk = activeworkbook
> > >
> > > or
> > > workbooks.open filename:="C:\temp\abc.xls"
> > > set newbk = activeworkbook
> > >
> > > Your code is wrong in selecting a range, you need a set statement. also you
> > > must have a sheet reference.
> > >
> > > newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> > > or
> > > set MyRngPer = newbk.sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
> > > MyRngPer.select
> > >
> > > then
> > > "SteveDB1" wrote:
> > >
> > > > Hello.
> > > > I'm working on a macro to link two worksheets.
> > > > I've written something that appears to work, but when I went to test it I
> > > > found that it picks the range on the primary worksheet.
> > > > I need to have it select the range on the secondary worksheet.
> > > > E.g.,
> > > >
> > > > MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> > > >
> > > > Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> > > > input box.
> > > >
> > > > How do I get the MyRngPer to select the range desired from another worksheet?
> > > >
> > > > Thank you.
> > > >

> >
> > --
> >
> > Dave Peterson
> >


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      9th Apr 2008
Read Joel's instructions again and try it with the With/end with structure.

And it wouldn't hurt to specify the workbook if sheets("sheet1") isn't in the
activeworkbook.

SteveDB1 wrote:
>
> Hi.
> I want to link two worksheets together. These are within the same workbook.
>
> How would I link two worksheets within the same workbook?
>
> I tried the :
>
> set MyRngPer = Sheets("Sheet1").Range(Cells(NuB, NuA), Cells(NuC, NuA))
> MyRngPer.select
>
> and it did not work.
> Do I need to include the workbook name?
>
> "Joel" wrote:
>
> > I just noticed something else. Excel has problem remebering that the Range
> > and Cell in the statement below are on the same worksheets. You need to do
> > something like this. Notice I have three dots (before range and cells)
> >
> > with newbk.sheets("Sheet1")
> >
> > set MyRngPer = .Range(.Cells(NuB, NuA), .Cells(NuC, NuA))
> >
> > end with
> >
> > "SteveDB1" wrote:
> >
> > > Hello.
> > > I'm working on a macro to link two worksheets.
> > > I've written something that appears to work, but when I went to test it I
> > > found that it picks the range on the primary worksheet.
> > > I need to have it select the range on the secondary worksheet.
> > > E.g.,
> > >
> > > MyRngPer = Range(Cells(NuB, NuA), Cells(NuC, NuA)).Select
> > >
> > > Where the NuA, NuB, and NuC are variables I've dim'd, and input through an
> > > input box.
> > >
> > > How do I get the MyRngPer to select the range desired from another worksheet?
> > >
> > > Thank you.
> > >


--

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
How to ID calling worksheet if it isn't the active sheet? cate Microsoft Excel Programming 2 30th Mar 2010 01:30 AM
Auto copy cell data from source sheet to another wrkbook sheet IVLUTA Microsoft Excel Programming 2 2nd Jun 2009 05:07 PM
calling a worksheet function from another worksheet in same workbo Liz Microsoft Excel Programming 5 23rd Jun 2008 06:16 PM
copying and pasting from source sheet to destination sheet without naming source sht? Simon Lloyd Microsoft Excel Programming 5 2nd Jun 2006 06:11 PM
Why wouldn't calling the Activate sub for a sheet automatically call that sheet's Worksheet_Activate() sub? Scott Lyon Microsoft Excel Programming 3 19th Aug 2003 03:03 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:21 PM.