passing strings

  • Thread starter Thread starter pabs
  • Start date Start date
P

pabs

I need some help with argument passing....

I have a macro (let's call it TEST()) that I will run from a Master
file. The results will go in seperate files.

The macro TEST() would be the same for each file with the exception
that the file name would change
ex : Windows("Rider.xls").Activate

the rest of the macro (TEST()) does not varie from file to file.
So from my master file I would have calls to this macro (TEST()) and
pass the name of the file I want to generate (eg. Rider1, Rider2,
Rider3, ect.)

I could then simply call Windows("STRING").Activate from the macro .
STRING being the string I would pass from the master file

What is the easiest (or pretiest) way to do this?


thanks

Pabs
 
Pabs,

Declare the Test macro with an argument like

Sub Test(WinName As String)
Windows(WinName).Activate
' rest of your code here
End Sub

Then, call the code with something like

Test WinName:="Book1.xls"


--
Cordially,
Chip Pearson
Microsoft MVP - Excel
Pearson Software Consulting, LLC
www.cpearson.com
 
Sub Main
for each bk in Application.Workbooks
Test bk.Name
Next
End Sub



Sub Test(sStr as String)

' note that sStr does not have double quotes around it.
Windows(sStr).Activate
End Sub
 
works great!

so far I've always had the file open (the one the macro write to) and I
was trying to make it so that it would open the file when the
particular macro is called.

I added this line
Workbooks.Open WindowName

WindowName being a particular file (ex: rider1.xls)

it says it can't find the file. All the file reside on the same
directory level...
am I forgetting something ...??
 
I know you already have an answer, but it parallels something I do.

My understanding is you are passing a string from one workbook to
another and for some reason you need to use multiple files to do this.

(the result will print in the immediate window after running the second
subroutine.)

Sub save_variable()


st = "string to be passed"

Workbooks.Add
Range("a1") = st

Application.DisplayAlerts = False
'prevents asking if you want to write over old file

ActiveWorkbook.SaveAs ("stringtopass")

ActiveWorkbook.Close


End Sub


Sub get_variable()

Workbooks.Open FileName:="stringtopass"
st = Range("a1")
ActiveWorkbook.Close

Debug.Print "string passed: "; st

End Sub
 
Back
Top