Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Hello,

I have a macro that copies data from the active sheet to a separate sheet. Although the MBC Data Backup.xlsb file will never change, the name of the active workbook where the macro resides "Iron Horse MBC Listing 0.3.0.xlsb" will change from time to time and causes this scrip to fail. How can I make this work without referencing the actual name of the active file?


Sub Backup()

' Backup Macro

Workbooks.Open Filename:="C:\NSD\Personal\MBC Data Backup.xlsb"
Range("A9:N20000").Select
Selection.ClearContents
Range("A9").Select

Windows("Iron Horse MBC Listing 0.3.0.xlsb").Activate
Sheets("Data").Select
Range("A9:N20000").Select
Selection.Copy

Windows("MBC Data Backup.xlsb").Activate
Range("A9").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False

Range("A9").Select
Application.CutCopyMode = False
ActiveWorkbook.Save
ActiveWindow.Close
Range("A9").Select

Sheets("Cover").Select

End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Have a look at...

GetOpenFilename()

...and 'Set' a fully qualified ref to it. For example...


Sub Backup()
Dim wkbSource As Workbook, wkbTarget As Workbook, sFilename$

sFilename = Get_FileToOpen("Excel Files ""*.xlsb"", (*.xls)")
If sFilename = "" Then Exit Sub '//user cancels

Set wkbSource = Workbooks("Iron Horse MBC Listing 0.3.0.xlsb")
Set wkbTarget = Workbooks.Open(Filename:=sFilename)

wkbTarget.Range("A9:N20000") = wkbSource.Range("A9:N20000")
wkbTarget.Close SaveChanges:=True

wkbSource.Sheets("Cover").Select
End Sub


Function Get_FileToOpen$(Optional FileTypes$)
If FileTypes = "" Then FileTypes = "All Files ""*.*"", (*.*)"
Get_FileToOpen = Application.GetOpenFilename(FileTypes)
End Function

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Note that the sample code assumes the activesheet in both files is
already set. Otherwise.., edit as follows:

Change

wkbTarget.Range("A9:N20000") = wkbSource.Range("A9:N20000")
TO

wkbTarget.Range("A9:N20000") = _
wkbSource.Sheets("Data").Range("A9:N20000")

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Hi Gary,

Thanks for the response. I used the codes you suggested but it didnt' work. It was failing at these 2 lines.

Keep in mind that the desination file "MBC Data Backup.xlsb" will never change name nor location. The source file where this macro resides in will change version from 3.0 to 3.1, 3.2, 3.3 etc). I think what I need is to figure how how to reference the active workbook as being dynamic instead of a static name.




Set wkbSource = Workbooks("Iron Horse MBC Listing 0.3.1.xlsb")
Set wkbTarget = Workbooks.Open(Filename:=sFilename)

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Sorry, I misunderstood! Try...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Set wkbTarget = Workbooks.Open(Filename:=sFilename)

wkbTarget.Range("A9:N20000") = _
ThisWorkbook.Sheets("Data").Range("A9:N20000")
wkbTarget.Close SaveChanges:=True

ThisWorkbook.Sheets("Cover").Select
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Garry,

Not working either. When the latest code was copied and pasted, the "wkbTarget.Range("A9:N20000") = _" turned red and errored out.

I also have a question regarding the copy and paste portion of the original code? Where would i put that ?



On Wednesday, December 10, 2014 5:07:46 PM UTC-8, GS wrote:
Sorry, I misunderstood! Try...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Set wkbTarget = Workbooks.Open(Filename:=sFilename)

wkbTarget.Range("A9:N20000") = _
ThisWorkbook.Sheets("Data").Range("A9:N20000")
wkbTarget.Close SaveChanges:=True





ThisWorkbook.Sheets("Cover").Select
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Not working either. When the latest code was copied and pasted, the
"wkbTarget.Range("A9:N20000") = _" turned red and errored out.


Make sure there's no 'line-wrapping' in the code as a result of your
copy/paste. Otherwise, this works EXACTLY as you requested in my sample
file!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

I also have a question regarding the copy and paste portion of the
original code? Where would i put that ?


Nowhere! My macro assigns the source range 'values' directly, replacing
any existing data on the target sheet.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

On Thursday, December 11, 2014 10:48:36 AM UTC-8, GS wrote:
I also have a question regarding the copy and paste portion of the

Garry,

I'm so sorry. I'm way to new to vba to make heads and tail out of this. Any possibility for me to send you the actual files?

original code? Where would i put that ?


Nowhere! My macro assigns the source range 'values' directly, replacing
any existing data on the target sheet.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

I'm so sorry. I'm way to new to vba to make heads and tail out of
this. Any possibility for me to send you the actual files?


Ok then, follow these instructions...

1. Delete all existing macros I gave you;
2. In the code window of a standard module:
2.1 Double-click the module in Project Explorer;
2.2 Choose InsertFile... from the menubar;
2.3 Browse to the folder where you put the file you downloaded here.

https://app.box.com/s/23yqum8auvzx17h04u4f
(Look for "BackupMacro.txt")

3. Run the macro.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Garry,

Followed your instruction. Code failed at


wkbTarget.Range("A9:N20000") = _
ThisWorkbook.Sheets("Data").Range("A9:N20000")



On Thursday, December 11, 2014 11:17:40 AM UTC-8, GS wrote:
I'm so sorry. I'm way to new to vba to make heads and tail out of
this. Any possibility for me to send you the actual files?


Ok then, follow these instructions...

1. Delete all existing macros I gave you;
2. In the code window of a standard module:
2.1 Double-click the module in Project Explorer;
2.2 Choose InsertFile... from the menubar;
2.3 Browse to the folder where you put the file you downloaded here.

https://app.box.com/s/23yqum8auvzx17h04u4f
(Look for "BackupMacro.txt")

3. Run the macro.

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #12   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Followed your instruction. Code failed at


wkbTarget.Range("A9:N20000") = _
ThisWorkbook.Sheets("Data").Range("A9:N20000")


What error description is given? Does the workbook you inserted the
code into have a sheet named "Data"?

Note that 'ThisWorkbook' refers to the file containing the code!

Note that wkbTarget is the backup file specified as 'sFilename'!

On my machine this code opens wkbTarget and replaces any existing data
in the specified range with the data from the same range on "Data".

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #13   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Revise the code as follows...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Application.ScreenUpdating = False
Set wkbTarget = Workbooks.Open(Filename:=sFilename)
wkbTarget.Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue
wkbTarget.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub

...so you don't see any screen activity. Also, note that I *explicitly*
specified the 'Value' property for both ranges. This is a range's
default property but depending on OS, Excel version, and so on it's
always better 'good practice' to use fully qualified refs!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #14   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Used you the latest code. Error wants me to debug the line below. Also highlighted in yellow


wkbTarget.Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue

Garry, I appreciate your assistance. It seems that I might have to work around and make sure to change the workbook name everytime I rename the it only.

Thank you so much for your assistance.


On Thursday, December 11, 2014 2:01:46 PM UTC-8, GS wrote:
Revise the code as follows...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Application.ScreenUpdating = False
Set wkbTarget = Workbooks.Open(Filename:=sFilename)
wkbTarget.Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue
wkbTarget.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub

..so you don't see any screen activity. Also, note that I *explicitly*
specified the 'Value' property for both ranges. This is a range's
default property but depending on OS, Excel version, and so on it's
always better 'good practice' to use fully qualified refs!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #15   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Garry, I appreciate your assistance. It seems that I might have to
work around and make sure to change the workbook name everytime I
rename the it only.


That should not be necessary *IF* the code resides in the workbook with
the source data. The error you get suggests this is not the case, *OR*
there's no sheet in that file named "Data", *OR* the sheetname is
mis-spelled!

How are you executing the code?
Where is the code located? In a worksheet module OR a standard module?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #16   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Ok.., my mistake! change that problem line to the following...

wkbTarget.ActiveSheet.Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue

...where it assumes the opened file is the active workbook and the
active sheet is whatever sheet was active when the file was last saved.

OR
you can specify the sheetname...

wkbTarget.Sheets("<name").Range("A9:N20000").Valu e = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #17   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

For clarity, my test macro uses this line

Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue

...which assumes the activesheet in the opened file. I manually
prepended the ref to the workbook before posting, but forgot to include
any ref to the sheet the range belonged to. Very bad on my part, and
you have my sincere apology for any troublesome time/energy spent!

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #18   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

Garry,

Let me take a few steps back and descript the whole file so you get a better understanding of how it's currently set up.

The main file is a directory listing of people. This list is about 7,000 lines long and growing. It's in a tab called "Data" inside the main file called "Iron Horse MBC Listing 0.3.0.xlsb". From time to time, I will need to update this file to include new features as requested. When I do update this file, I will name it to Iron Horse MBC Listing 0.3.1, 0.3.2, 0.3.3 and etc. as part of version control. Since this file will be maintain by a person who isn't very computer savvy, I want to preserve the data in the Data tab of this file.

To make this happen, I got a Before Save script which will automatically copy the data from the Iron Horse MBC Listing 0.3.0.xls file to a separate excel file called "MBC Data Backup.xlsb". This file is located on the local C drive C:\NSD\Personal\MBC Data Backup.xlsb. This destination file will never change location nor name. The des


Here is the before save script and it's in the ThisWorkbook. Using the Callup command, everytime the file is saved, it will automatically call up the Backup macro which will copy the data from the main file's Data tab to MBC Data Backup.xlsb file. Keep in mind that the data is also being copied to the destination file tab called "Data" as well. Perhaps that is the confusion.

Source file: Iron Horse MBC Listing 0.3.0.xlsb - Data tab A9:N20000
Destination file: MBC Data Backup.xlsb - Data tab A9:N20000

-----------------
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)


Call Backup

End Sub


The Backup macro is on Module 8. Using the original code that posted, this works perfectly EXCEPT when the main files' name changed from 0.3.0 to 0.3..1 as part of version control processes.

Hopefully this helps you understand the situation. Anything you can do to assist is greatly appreciated.



On Thursday, December 11, 2014 3:50:19 PM UTC-8, GS wrote:
Garry, I appreciate your assistance. It seems that I might have to
work around and make sure to change the workbook name everytime I
rename the it only.


That should not be necessary *IF* the code resides in the workbook with
the source data. The error you get suggests this is not the case, *OR*
there's no sheet in that file named "Data", *OR* the sheetname is
mis-spelled!

How are you executing the code?
Where is the code located? In a worksheet module OR a standard module?

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #19   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

Ok, the only thing unclear was the name of the target sheet in the
backup file. As per my most recent post, I realize the troubles you've
been having are due to my neglecting to ref the target sheet as I
explained. Again I apologize and also thank you for your persistence!
Here's the complete final version...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Application.ScreenUpdating = False
Set wkbTarget = Workbooks.Open(Filename:=sFilename)
wkbTarget.Sheets("Data").Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue
wkbTarget.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


  #20   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default Run macro from active workbook whose name keeps changing

On Thursday, December 11, 2014 4:29:28 PM UTC-8, GS wrote:
Ok, the only thing unclear was the name of the target sheet in the
backup file. As per my most recent post, I realize the troubles you've
been having are due to my neglecting to ref the target sheet as I
explained. Again I apologize and also thank you for your persistence!
Here's the complete final version...

Sub Backup()
Dim wkbTarget As Workbook
Const sFilename$ = "C:\NSD\Personal\MBC Data Backup.xlsb"

Application.ScreenUpdating = False
Set wkbTarget = Workbooks.Open(Filename:=sFilename)
wkbTarget.Sheets("Data").Range("A9:N20000").Value = _
ThisWorkbook.Sheets("Data").Range("A9:N20000").Val ue

Garry,

IT WORKED !!! You are a genius my friend and have saved me countless hours in the long run. Thank you for everything and no, you don't need apologize for anything. I'm grateful. Thank you again.




wkbTarget.Close SaveChanges:=True
Application.ScreenUpdating = True
End Sub

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion




  #21   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,514
Default Run macro from active workbook whose name keeps changing

IT WORKED !!! You are a genius my friend and have saved me countless
hours in the long run. Thank you for everything and no, you don't
need apologize for anything. I'm grateful. Thank you again.


I'm very glad for you! Thanks for the feedback...

--
Garry

Free usenet access at http://www.eternal-september.org
Classic VB Users Regroup!
comp.lang.basic.visual.misc
microsoft.public.vb.general.discussion


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
Macro that opens a separate workbook to a specified worksheet whenthe active workbook is right mouse clicked Michael Lanier Excel Programming 8 December 20th 11 09:51 PM
Call macro from active workbook John[_140_] Excel Programming 4 October 3rd 09 04:07 AM
Run Macro on all WS in active workbook.. jeremiah Excel Programming 6 May 6th 08 05:56 PM
Changing the active workbook SP[_5_] Excel Programming 6 October 16th 05 10:36 PM
run a macro on an in-active workbook jfeka[_2_] Excel Programming 4 July 19th 03 03:49 PM


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

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"