Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Reference dynamic filename

I have the following part code included in macro 1:

Dim spath As String
spath = Range("B3").Value
ActiveWorkbook.SaveAs spath & ".xls"

(The text in range B3 is:
C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team 1)


In macros 2, 3, 4, etc. I repeatedly copy and paste a dynamic sheet into
Daily Withdrawal Team 3.xls by referencing with hard code:
Sheets("Output").Select
Sheets("Output").Copy After:=Workbooks("07_11_14 Daily Withdrawal Team
1.xls").Sheets(1)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet
.Name = Range("D1").Value
.Move After:=Sheets(Sheets.Count)
End With

Problem:
As the date component of the filename (07_11_14 ) will be dynamic, is there
any way to reference this in macros 2, 3, 4, etc without the need to
manually <Edit <Replace in VB?

Any assistance greatly appreciated

Cheers

Ozbobeee





  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Reference dynamic filename


Declare a module level variable to reference the workbook...
At the top of the module, just below Option Explicit add
"Private wbNew as Excel.Workbook" ' no quote marks

In your code set the variable to the saved workbook...
ActiveWorkbook.SaveAs spath & ".xls"
Set wbNew = ActiveWorkbook

You can use the reference and not have to use the workbook name...
Sheets("Output").Copy After:=wbNew.Sheets(1)
-or- possibly eliminate moving the sheet...
Sheets("Output").Copy After:=wbNew.Sheets(wbNew.Sheets.Count)

If the macros are not all in the same module then change "Private" to "Public".
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware
(Excel Add-ins / Excel Programming)



"Bob Maloney"
wrote in message
I have the following part code included in macro 1:
Dim spath As String
spath = Range("B3").Value
ActiveWorkbook.SaveAs spath & ".xls"
(The text in range B3 is:
C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team 1)

In macros 2, 3, 4, etc. I repeatedly copy and paste a dynamic sheet into
Daily Withdrawal Team 3.xls by referencing with hard code:
Sheets("Output").Select
Sheets("Output").Copy After:=Workbooks("07_11_14 Daily Withdrawal Team
1.xls").Sheets(1)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet
.Name = Range("D1").Value
.Move After:=Sheets(Sheets.Count)
End With

Problem:
As the date component of the filename (07_11_14 ) will be dynamic, is there
any way to reference this in macros 2, 3, 4, etc without the need to
manually <Edit <Replace in VB?
Any assistance greatly appreciated
Cheers
Ozbobeee





  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default Reference dynamic filename

First, I'm not sure I understood the problem.

I thought that you wanted to change the name of the file in code. That may not
be what you want.

This first section offers some ways to change the name of the file. The second
section is a way to refer to that workbook without using its (except when you
open it). (I'm kind of thinking that the second option is what you really
wanted--so don't give up on me!!!)

First option...

Maybe you could have the value in B3 obtained through a formula:

="C:\Documents and Settings\Agents\"
&TEXT(TODAY(),"yy\_mm\_dd")
&" Daily Withdrawal Team 1)"

or you could put the path (drive and folder) in one cell and the "suffix" in
another:

with worksheets("Somesheetnamehere")
spath = .range("b3").value & format(date,"yy_mm_dd") _
& .range("C3").value & ".xls"
end with

Or you could inspect the string, look for the last backslash and replace the 8
characters following it with the formatted date.

Dim sPath As String
Dim LastBackSlashPos As Long

sPath = "C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team 1" &
".xls"
LastBackSlashPos = InStrRev(sPath, "\", -1, vbTextCompare)

sPath = Left(sPath, LastBackSlashPos) _
& Format(Date, "yy_mm_dd") & Mid(sPath, LastBackSlashPos + 9)

Debug.Print sPath

But all this depends on the fact that I replaced the date in that string with
today's date.

========
Second Option...

Dim wkbk as workbook
dim sPath as string

'your code to retrieve the name of the file.

'then open the file using that name:

Set wkbk = workbooks.open(filename:=spath)

'now you can use that workbook variable in your code later on:

Sheets("Output").Copy _
After:=wkbk.Sheets(1)



Bob Maloney wrote:

I have the following part code included in macro 1:

Dim spath As String
spath = Range("B3").Value
ActiveWorkbook.SaveAs spath & ".xls"

(The text in range B3 is:
C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team 1)

In macros 2, 3, 4, etc. I repeatedly copy and paste a dynamic sheet into
Daily Withdrawal Team 3.xls by referencing with hard code:
Sheets("Output").Select
Sheets("Output").Copy After:=Workbooks("07_11_14 Daily Withdrawal Team
1.xls").Sheets(1)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet
.Name = Range("D1").Value
.Move After:=Sheets(Sheets.Count)
End With

Problem:
As the date component of the filename (07_11_14 ) will be dynamic, is there
any way to reference this in macros 2, 3, 4, etc without the need to
manually <Edit <Replace in VB?

Any assistance greatly appreciated

Cheers

Ozbobeee


--

Dave Peterson
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9
Default Reference dynamic filename

Jim and Dave,

Setting the variable to the saved workbook and referencing that in the other
macros was what I was after.

Thanks to both of you for your assistance.

Cheers

Ozbobeee


"Dave Peterson" wrote in message
...
First, I'm not sure I understood the problem.

I thought that you wanted to change the name of the file in code. That
may not
be what you want.

This first section offers some ways to change the name of the file. The
second
section is a way to refer to that workbook without using its (except when
you
open it). (I'm kind of thinking that the second option is what you really
wanted--so don't give up on me!!!)

First option...

Maybe you could have the value in B3 obtained through a formula:

="C:\Documents and Settings\Agents\"
&TEXT(TODAY(),"yy\_mm\_dd")
&" Daily Withdrawal Team 1)"

or you could put the path (drive and folder) in one cell and the "suffix"
in
another:

with worksheets("Somesheetnamehere")
spath = .range("b3").value & format(date,"yy_mm_dd") _
& .range("C3").value & ".xls"
end with

Or you could inspect the string, look for the last backslash and replace
the 8
characters following it with the formatted date.

Dim sPath As String
Dim LastBackSlashPos As Long

sPath = "C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team
1" &
".xls"
LastBackSlashPos = InStrRev(sPath, "\", -1, vbTextCompare)

sPath = Left(sPath, LastBackSlashPos) _
& Format(Date, "yy_mm_dd") & Mid(sPath, LastBackSlashPos + 9)

Debug.Print sPath

But all this depends on the fact that I replaced the date in that string
with
today's date.

========
Second Option...

Dim wkbk as workbook
dim sPath as string

'your code to retrieve the name of the file.

'then open the file using that name:

Set wkbk = workbooks.open(filename:=spath)

'now you can use that workbook variable in your code later on:

Sheets("Output").Copy _
After:=wkbk.Sheets(1)



Bob Maloney wrote:

I have the following part code included in macro 1:

Dim spath As String
spath = Range("B3").Value
ActiveWorkbook.SaveAs spath & ".xls"

(The text in range B3 is:
C:\Documents and Settings\Agents\07_11_14 Daily Withdrawal Team 1)

In macros 2, 3, 4, etc. I repeatedly copy and paste a dynamic sheet into
Daily Withdrawal Team 3.xls by referencing with hard code:
Sheets("Output").Select
Sheets("Output").Copy After:=Workbooks("07_11_14 Daily Withdrawal
Team
1.xls").Sheets(1)
Cells.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone,
SkipBlanks _
:=False, Transpose:=False
Range("A1").Select
Application.CutCopyMode = False
With ActiveSheet
.Name = Range("D1").Value
.Move After:=Sheets(Sheets.Count)
End With

Problem:
As the date component of the filename (07_11_14 ) will be dynamic, is
there
any way to reference this in macros 2, 3, 4, etc without the need to
manually <Edit <Replace in VB?

Any assistance greatly appreciated

Cheers

Ozbobeee


--

Dave Peterson



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
Dynamic Hyperlinks - Changing Folders and Filename Philip J Smith Excel Worksheet Functions 0 April 18th 07 12:24 PM
Reference in a filename.. Frode Hjoennevaag Excel Worksheet Functions 3 April 14th 05 02:47 PM
Toolbar & dynamic filename Steven Excel Programming 1 June 3rd 04 04:01 PM
Dynamic filename JC Excel Programming 8 March 5th 04 09:56 PM
Dynamic Filename From Macro Craig[_9_] Excel Programming 1 November 6th 03 06:36 PM


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