Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Referring back to the originating workbook in a macro.

I've looked through the posts and I see lots of people with similar questions
to mine, I just can't figure out how to adapt the suggestions to my
application.

I have a workbook that is public on our network and anyone can open it, save
it, rename it. It contains a macro that copies cells in that workbook and
pastes them into my own workbook that doesn't change names. This public
workbook can be renamed to anything. I need the macro to remember what the
name of the workbook is so it can go out, paste values and then come back
into that same workbook no matter what it's been saved as.

I've read about this code

Dim WB As Workbook
Set WB = activeworkbook 'Change to active workbook

but I'm having problems getting it to work in my macro. I don't know enough
about the code to get the syntax right.

Let's say the Excel file is called NewFileName.xls and it contains this
macro. I need it to go open TruckBinderyBoxReport.xls, paste the data and
then come back to NewFileName.xls. The next time I run this macro the file
may not be called NewFileName.xls so the code has to figure out the file name
so it knows what to go back to.

Here's the macro as it is now.

Sub ExportToTruckBinderyBoxReport()

Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5:S39").Select
Selection.Copy

Workbooks.Open Filename:= _
"\\fileserver\data\Bnd\Bindery
Library\Documents\B-line\TruckBinderyBoxReport.xls" _
, UpdateLinks:=3

Windows("TruckBinderyBoxReport.xls").Activate
Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Run Report").Select
Windows("TruckBinderyBoxReport UPS.xls").Activate
ActiveWindow.Close
End Sub

When I get to the line with Windows("TruckBinderyBoxReport
UPS.xls").Activate I get the error because the macro doesn't know the
workbook isn't called TruckBinderyBoxReport UPS.xls anymore. I've tried to
use Windows(WB).Activate and Windows(activeworkbook).Activate but I'm sure
the syntax is wrong so it just crashes.

Don
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default Referring back to the originating workbook in a macro.

code that is executing is alwayr running in ThisWorkbook so

thisworkbook.Select

will select the workbook that is running the code.
--
HTH...

Jim Thomlinson


"Don M." wrote:

I've looked through the posts and I see lots of people with similar questions
to mine, I just can't figure out how to adapt the suggestions to my
application.

I have a workbook that is public on our network and anyone can open it, save
it, rename it. It contains a macro that copies cells in that workbook and
pastes them into my own workbook that doesn't change names. This public
workbook can be renamed to anything. I need the macro to remember what the
name of the workbook is so it can go out, paste values and then come back
into that same workbook no matter what it's been saved as.

I've read about this code

Dim WB As Workbook
Set WB = activeworkbook 'Change to active workbook

but I'm having problems getting it to work in my macro. I don't know enough
about the code to get the syntax right.

Let's say the Excel file is called NewFileName.xls and it contains this
macro. I need it to go open TruckBinderyBoxReport.xls, paste the data and
then come back to NewFileName.xls. The next time I run this macro the file
may not be called NewFileName.xls so the code has to figure out the file name
so it knows what to go back to.

Here's the macro as it is now.

Sub ExportToTruckBinderyBoxReport()

Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5:S39").Select
Selection.Copy

Workbooks.Open Filename:= _
"\\fileserver\data\Bnd\Bindery
Library\Documents\B-line\TruckBinderyBoxReport.xls" _
, UpdateLinks:=3

Windows("TruckBinderyBoxReport.xls").Activate
Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Run Report").Select
Windows("TruckBinderyBoxReport UPS.xls").Activate
ActiveWindow.Close
End Sub

When I get to the line with Windows("TruckBinderyBoxReport
UPS.xls").Activate I get the error because the macro doesn't know the
workbook isn't called TruckBinderyBoxReport UPS.xls anymore. I've tried to
use Windows(WB).Activate and Windows(activeworkbook).Activate but I'm sure
the syntax is wrong so it just crashes.

Don

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Referring back to the originating workbook in a macro.

Oops.

ThisWorkbook.Activate

Jim Thomlinson wrote:

code that is executing is alwayr running in ThisWorkbook so

thisworkbook.Select

will select the workbook that is running the code.
--
HTH...

Jim Thomlinson

"Don M." wrote:

I've looked through the posts and I see lots of people with similar questions
to mine, I just can't figure out how to adapt the suggestions to my
application.

I have a workbook that is public on our network and anyone can open it, save
it, rename it. It contains a macro that copies cells in that workbook and
pastes them into my own workbook that doesn't change names. This public
workbook can be renamed to anything. I need the macro to remember what the
name of the workbook is so it can go out, paste values and then come back
into that same workbook no matter what it's been saved as.

I've read about this code

Dim WB As Workbook
Set WB = activeworkbook 'Change to active workbook

but I'm having problems getting it to work in my macro. I don't know enough
about the code to get the syntax right.

Let's say the Excel file is called NewFileName.xls and it contains this
macro. I need it to go open TruckBinderyBoxReport.xls, paste the data and
then come back to NewFileName.xls. The next time I run this macro the file
may not be called NewFileName.xls so the code has to figure out the file name
so it knows what to go back to.

Here's the macro as it is now.

Sub ExportToTruckBinderyBoxReport()

Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5:S39").Select
Selection.Copy

Workbooks.Open Filename:= _
"\\fileserver\data\Bnd\Bindery
Library\Documents\B-line\TruckBinderyBoxReport.xls" _
, UpdateLinks:=3

Windows("TruckBinderyBoxReport.xls").Activate
Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Run Report").Select
Windows("TruckBinderyBoxReport UPS.xls").Activate
ActiveWindow.Close
End Sub

When I get to the line with Windows("TruckBinderyBoxReport
UPS.xls").Activate I get the error because the macro doesn't know the
workbook isn't called TruckBinderyBoxReport UPS.xls anymore. I've tried to
use Windows(WB).Activate and Windows(activeworkbook).Activate but I'm sure
the syntax is wrong so it just crashes.

Don


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Referring back to the originating workbook in a macro.

Since one of the workbooks that you're using contains the macro, you can use
ThisWorkbook to refer to the workbook that owns that macro.

This may give you an idea:

Option Explicit
Sub ExportToTruckBinderyBoxReport()

Dim TBBRptWkbk As Workbook
Dim RngToCopy As Range
Dim DestCell As Range

Set TBBRptWkbk = Workbooks.Open(Filename:= _
"\\fileserver\data\Bnd\Bindery Library\Documents\" _
& "B-line\TruckBinderyBoxReport.xls", _
UpdateLinks:=3)

With ThisWorkbook.Worksheets("canada box report")
Set RngToCopy = .Range("C5:s39")
End With

With TBBRptWkbk.Worksheets("canada box report")
Set DestCell = .Range("C5")
End With

RngToCopy.Copy
DestCell.PasteSpecial Paste:=xlPasteValues

TBBRptWkbk.Close savechanges:=False

Application.CutCopyMode = False

End Sub



Don M. wrote:

I've looked through the posts and I see lots of people with similar questions
to mine, I just can't figure out how to adapt the suggestions to my
application.

I have a workbook that is public on our network and anyone can open it, save
it, rename it. It contains a macro that copies cells in that workbook and
pastes them into my own workbook that doesn't change names. This public
workbook can be renamed to anything. I need the macro to remember what the
name of the workbook is so it can go out, paste values and then come back
into that same workbook no matter what it's been saved as.

I've read about this code

Dim WB As Workbook
Set WB = activeworkbook 'Change to active workbook

but I'm having problems getting it to work in my macro. I don't know enough
about the code to get the syntax right.

Let's say the Excel file is called NewFileName.xls and it contains this
macro. I need it to go open TruckBinderyBoxReport.xls, paste the data and
then come back to NewFileName.xls. The next time I run this macro the file
may not be called NewFileName.xls so the code has to figure out the file name
so it knows what to go back to.

Here's the macro as it is now.

Sub ExportToTruckBinderyBoxReport()

Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5:S39").Select
Selection.Copy

Workbooks.Open Filename:= _
"\\fileserver\data\Bnd\Bindery
Library\Documents\B-line\TruckBinderyBoxReport.xls" _
, UpdateLinks:=3

Windows("TruckBinderyBoxReport.xls").Activate
Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Run Report").Select
Windows("TruckBinderyBoxReport UPS.xls").Activate
ActiveWindow.Close
End Sub

When I get to the line with Windows("TruckBinderyBoxReport
UPS.xls").Activate I get the error because the macro doesn't know the
workbook isn't called TruckBinderyBoxReport UPS.xls anymore. I've tried to
use Windows(WB).Activate and Windows(activeworkbook).Activate but I'm sure
the syntax is wrong so it just crashes.

Don


--

Dave Peterson
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 51
Default Referring back to the originating workbook in a macro.

Thank you guys, the ThisWorkbook.Activate statement worked perfectly.

Don

"Don M." wrote:

I've looked through the posts and I see lots of people with similar questions
to mine, I just can't figure out how to adapt the suggestions to my
application.

I have a workbook that is public on our network and anyone can open it, save
it, rename it. It contains a macro that copies cells in that workbook and
pastes them into my own workbook that doesn't change names. This public
workbook can be renamed to anything. I need the macro to remember what the
name of the workbook is so it can go out, paste values and then come back
into that same workbook no matter what it's been saved as.

I've read about this code

Dim WB As Workbook
Set WB = activeworkbook 'Change to active workbook

but I'm having problems getting it to work in my macro. I don't know enough
about the code to get the syntax right.

Let's say the Excel file is called NewFileName.xls and it contains this
macro. I need it to go open TruckBinderyBoxReport.xls, paste the data and
then come back to NewFileName.xls. The next time I run this macro the file
may not be called NewFileName.xls so the code has to figure out the file name
so it knows what to go back to.

Here's the macro as it is now.

Sub ExportToTruckBinderyBoxReport()

Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5:S39").Select
Selection.Copy

Workbooks.Open Filename:= _
"\\fileserver\data\Bnd\Bindery
Library\Documents\B-line\TruckBinderyBoxReport.xls" _
, UpdateLinks:=3

Windows("TruckBinderyBoxReport.xls").Activate
Sheets(Array("Canada Box Report", "USA Box Report")).Select
Sheets("Canada Box Report").Activate
Range("C5").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Sheets("Run Report").Select
Windows("TruckBinderyBoxReport UPS.xls").Activate
ActiveWindow.Close
End Sub

When I get to the line with Windows("TruckBinderyBoxReport
UPS.xls").Activate I get the error because the macro doesn't know the
workbook isn't called TruckBinderyBoxReport UPS.xls anymore. I've tried to
use Windows(WB).Activate and Windows(activeworkbook).Activate but I'm sure
the syntax is wrong so it just crashes.

Don



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
referring to workbook Fabrizio Excel Programming 1 June 21st 06 05:49 PM
referring to workbook comparini3000 Excel Programming 0 June 21st 06 05:34 PM
referring to workbook Jim Thomlinson Excel Programming 0 June 21st 06 05:34 PM
Referring to a worsheet in another workbook Roundy Excel Programming 1 June 9th 06 04:04 AM
Referring to function in another workbook psp Excel Worksheet Functions 1 August 31st 05 10:32 PM


All times are GMT +1. The time now is 02:26 AM.

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"