Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default run a macro on an in-active workbook

Is there any way to get a macro to execute on a workbook
which is open, but not active?

I have a macro in a certain workbook (call it "A")which
repeats automatically but if I I open another workbook,
say "B", then the macro produces an error because it
doesn't find the data it needs in the active worksheet if
I'm working in "B".

I've added the following code to the start of my macro

Workbooks("hilowwork book.xls").Activate
Worksheets("UpdateInfo").Activate

and the macro runs fine but it causes the sheet to become
active and it interrupts the work I may be doing in
workbook "B".

Is there some way I can feed the macro the information of
the workbook name and sheet name that it is to operate on
so that it does this in the background and still allows
me to continue working in workbook "B" without
interruption?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default run a macro on an in-active workbook

You can refer to cells in non-active workbooks by fully qualifying the references. Something like:

x = Workbooks("A.xls").Worksheets("Sheet1").Range("A1" ).Value

If you need to make lots of references to the same object, use the following:

With Workbooks("A.xls").Worksheets("Sheet1")
x = .Range("A1").Value
.Range("A2").Value = x

End With

--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message ...
Is there any way to get a macro to execute on a workbook
which is open, but not active?

I have a macro in a certain workbook (call it "A")which
repeats automatically but if I I open another workbook,
say "B", then the macro produces an error because it
doesn't find the data it needs in the active worksheet if
I'm working in "B".

I've added the following code to the start of my macro

Workbooks("hilowwork book.xls").Activate
Worksheets("UpdateInfo").Activate

and the macro runs fine but it causes the sheet to become
active and it interrupts the work I may be doing in
workbook "B".

Is there some way I can feed the macro the information of
the workbook name and sheet name that it is to operate on
so that it does this in the background and still allows
me to continue working in workbook "B" without
interruption?



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5
Default run a macro on an in-active workbook

John,

I've tried what you suggest and it produces an error
right off ... here's what I'm doing (below) What is
wrong?

Sub upDateHis1()

booknm = Workbooks("hilowwork book.xls").Worksheets
("UpdateInfo")
lwrLimit = Range("lwr").Row + 1
uprLimit = Range("eend").Row - 1
lgOpnHi = Range("OpnHi").Column
lgStokNow = Range("StokNow").Column
lgStokhi = lgStokNow + 1


For rwIndex = lwrLimit To uprLimit

'This sets High Value of Option
With Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi)
If .Value _
< Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi - 1).Value _
Then .Value = Worksheets("UpdateInfo").Cells
(rwIndex, lgOpnHi - 1).Value
If .Value * 0.75 _
Worksheets("UpdateInfo").Cells(rwIndex,

lgOpnHi - 1).Value _
Then Worksheets("UpdateInfo").Cells
(rwIndex, 4).Value _
= "SELL"
End With
'This sets high value of Stock
If Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value _
Worksheets("UpdateInfo").Cells(rwIndex,

lgStokhi).Value _
Then
Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi).Value _
= Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value

Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi + 1).Value _
= Now
End If

Next rwIndex

rePeater

End Sub



-----Original Message-----
You can refer to cells in non-active workbooks by fully

qualifying the references. Something like:

x = Workbooks("A.xls").Worksheets("Sheet1").Range

("A1").Value

If you need to make lots of references to the same

object, use the following:

With Workbooks("A.xls").Worksheets("Sheet1")
x = .Range("A1").Value
.Range("A2").Value = x

End With

--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message

...
Is there any way to get a macro to execute on a

workbook
which is open, but not active?

I have a macro in a certain workbook (call it "A")which
repeats automatically but if I I open another workbook,
say "B", then the macro produces an error because it
doesn't find the data it needs in the active worksheet

if
I'm working in "B".

I've added the following code to the start of my macro

Workbooks("hilowwork book.xls").Activate
Worksheets("UpdateInfo").Activate

and the macro runs fine but it causes the sheet to

become
active and it interrupts the work I may be doing in
workbook "B".

Is there some way I can feed the macro the information

of
the workbook name and sheet name that it is to operate

on
so that it does this in the background and still allows
me to continue working in workbook "B" without
interruption?



.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 58
Default run a macro on an in-active workbook

Your code will not work if the workbook is not active as you are not including a reference to the workbook.

Also, if you are trying to create an object variable referring to the worksheet, use Set

Set booknm = Workbooks("hilowwork book.xls").Worksheets("UpdateInfo")

For rwIndex = lwrLimit To uprLimit

'This sets High Value of Option
With booknm.Cells(rwIndex, lgOpnHi)
If .Value _
< booknm.Cells(rwIndex, lgOpnHi - 1).Value _
Then .Value = booknm.Cells(rwIndex, lgOpnHi - 1).Value


--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message ...
John,

I've tried what you suggest and it produces an error
right off ... here's what I'm doing (below) What is
wrong?

Sub upDateHis1()

booknm = Workbooks("hilowwork book.xls").Worksheets
("UpdateInfo")
lwrLimit = Range("lwr").Row + 1
uprLimit = Range("eend").Row - 1
lgOpnHi = Range("OpnHi").Column
lgStokNow = Range("StokNow").Column
lgStokhi = lgStokNow + 1


For rwIndex = lwrLimit To uprLimit

'This sets High Value of Option
With Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi)
If .Value _
< Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi - 1).Value _
Then .Value = Worksheets("UpdateInfo").Cells
(rwIndex, lgOpnHi - 1).Value
If .Value * 0.75 _
Worksheets("UpdateInfo").Cells(rwIndex,

lgOpnHi - 1).Value _
Then Worksheets("UpdateInfo").Cells
(rwIndex, 4).Value _
= "SELL"
End With
'This sets high value of Stock
If Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value _
Worksheets("UpdateInfo").Cells(rwIndex,

lgStokhi).Value _
Then
Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi).Value _
= Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value

Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi + 1).Value _
= Now
End If

Next rwIndex

rePeater

End Sub



-----Original Message-----
You can refer to cells in non-active workbooks by fully

qualifying the references. Something like:

x = Workbooks("A.xls").Worksheets("Sheet1").Range

("A1").Value

If you need to make lots of references to the same

object, use the following:

With Workbooks("A.xls").Worksheets("Sheet1")
x = .Range("A1").Value
.Range("A2").Value = x

End With

--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message

...
Is there any way to get a macro to execute on a

workbook
which is open, but not active?

I have a macro in a certain workbook (call it "A")which
repeats automatically but if I I open another workbook,
say "B", then the macro produces an error because it
doesn't find the data it needs in the active worksheet

if
I'm working in "B".

I've added the following code to the start of my macro

Workbooks("hilowwork book.xls").Activate
Worksheets("UpdateInfo").Activate

and the macro runs fine but it causes the sheet to

become
active and it interrupts the work I may be doing in
workbook "B".

Is there some way I can feed the macro the information

of
the workbook name and sheet name that it is to operate

on
so that it does this in the background and still allows
me to continue working in workbook "B" without
interruption?



.



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default run a macro on an in-active workbook

Thanks John,

Your comment about creating an object referring the the
worksheet worked like a charm.

I had tried that before, but without using "SET" and it
wouldn't work and I had forgotten about the difference.

Anyhow, this has also solved the problem I had with the
stopping of a running macro, because now I can let it go
on in the background and don't need to pause it.

The other answer will be tucked awa for future
reference...it still may come in useful.

Jack

-----Original Message-----
Your code will not work if the workbook is not active as

you are not including a reference to the workbook.

Also, if you are trying to create an object variable

referring to the worksheet, use Set

Set booknm = Workbooks("hilowwork book.xls").Worksheets

("UpdateInfo")

For rwIndex = lwrLimit To uprLimit

'This sets High Value of Option
With booknm.Cells(rwIndex, lgOpnHi)
If .Value _
< booknm.Cells(rwIndex, lgOpnHi - 1).Value _
Then .Value = booknm.Cells(rwIndex, lgOpnHi -

1).Value


--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message

...
John,

I've tried what you suggest and it produces an error
right off ... here's what I'm doing (below) What is
wrong?

Sub upDateHis1()

booknm = Workbooks("hilowwork book.xls").Worksheets
("UpdateInfo")
lwrLimit = Range("lwr").Row + 1
uprLimit = Range("eend").Row - 1
lgOpnHi = Range("OpnHi").Column
lgStokNow = Range("StokNow").Column
lgStokhi = lgStokNow + 1


For rwIndex = lwrLimit To uprLimit

'This sets High Value of Option
With Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi)
If .Value _
< Worksheets("UpdateInfo").Cells(rwIndex,
lgOpnHi - 1).Value _
Then .Value = Worksheets

("UpdateInfo").Cells
(rwIndex, lgOpnHi - 1).Value
If .Value * 0.75 _
Worksheets("UpdateInfo").Cells(rwIndex,

lgOpnHi - 1).Value _
Then Worksheets("UpdateInfo").Cells
(rwIndex, 4).Value _
= "SELL"
End With
'This sets high value of Stock
If Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value _
Worksheets("UpdateInfo").Cells(rwIndex,

lgStokhi).Value _
Then
Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi).Value _
= Worksheets("UpdateInfo").Cells(rwIndex,
lgStokNow).Value

Worksheets("UpdateInfo").Cells(rwIndex,
lgStokhi + 1).Value _
= Now
End If

Next rwIndex

rePeater

End Sub



-----Original Message-----
You can refer to cells in non-active workbooks by

fully
qualifying the references. Something like:

x = Workbooks("A.xls").Worksheets("Sheet1").Range

("A1").Value

If you need to make lots of references to the same

object, use the following:

With Workbooks("A.xls").Worksheets("Sheet1")
x = .Range("A1").Value
.Range("A2").Value = x

End With

--

John Green - Excel MVP
Sydney
Australia


"jfeka" wrote in message

...
Is there any way to get a macro to execute on a

workbook
which is open, but not active?

I have a macro in a certain workbook (call it "A")

which
repeats automatically but if I I open another

workbook,
say "B", then the macro produces an error because it
doesn't find the data it needs in the active

worksheet
if
I'm working in "B".

I've added the following code to the start of my

macro

Workbooks("hilowwork book.xls").Activate
Worksheets("UpdateInfo").Activate

and the macro runs fine but it causes the sheet to

become
active and it interrupts the work I may be doing in
workbook "B".

Is there some way I can feed the macro the

information
of
the workbook name and sheet name that it is to

operate
on
so that it does this in the background and still

allows
me to continue working in workbook "B" without
interruption?



.



.

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
Active Workbook Kim Excel Discussion (Misc queries) 8 June 1st 10 11:33 PM
Macro to update active workbook in one folder Mike @ GD Excel Worksheet Functions 8 January 15th 09 02:19 PM
Macro to copy active worksheet to new workbook Macca Excel Discussion (Misc queries) 1 May 25th 08 02:07 PM
Macro: Exit active workbook without save? Don Excel Worksheet Functions 0 May 20th 05 06:47 AM
copy worksheet from closed workbook to active workbook using vba mango Excel Worksheet Functions 6 December 9th 04 07:55 AM


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

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

About Us

"It's about Microsoft Excel"