PDA

View Full Version : Solved: Trouble with VBA Code



jo15765
11-15-2011, 02:25 PM
I am running the following Array which works great. Problem I have now is that I need to add a pre-assigned unique identifier
to each varBook. For example, Test1 needs to be 123ABC_Test1, Test2 needs to be DOGcatTest2 etc... I know that I can assign ID1 = 123ABC
and ID2 = DOGcat and I know that I would just need to cycle through the pre-assigned id's just like I would the varBooks. Problem is...
I have no clue how to add 2 loops together. How would I code it to cycle BOTH the unique ID's as well as the varBooks, and 1st verify they match up
then make sure that it appends the unique ID to the varbook?

Dim Varbooks
Dim varBook
Dim wb As Excel.Workbook

Varbooks = Array(Test1", "Test2", "Test3")
For Each varBook In Varbooks
Set wb = Workbooks.Open(Filename:="R:\workbooks\Daily\" & varBook)
Run "RefreshOnOpen"
With wb
With Range("I4")
.Formula = "=TODAY() + 1"
Run "PrintToPDF_MultiSheetToOne_RunEarly"
.Formula = "=TODAY()"
ActiveWorkbook.SaveAs Filename:="R:\Workbooks\Completed\" & VBA.Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1) & "_" & VBA.Format(Date + 1, "mmddyyyy") & ".xls"
End With
End With
Next varBook

mdmackillop
11-15-2011, 04:51 PM
Something like
Prefix = Array("Pre1", "Pre2", "Pre3")
Varbooks = Array("Test1", "Test2", "Test3")

For Each Pre In Prefix
For Each varBook In Varbooks
Set wb = Workbooks.Open(Filename:="R:\workbooks\Daily\" & Pre & varBook
If Not wb is Nothing Then

jo15765
11-15-2011, 06:25 PM
I actually don't need the prefix assigned until the save portion. Would this code accomplish the same feat? And the correct prefix will go with teh correct varBook as long as they are both in the same order in the Array correct? So in the example below, Pre1 would go with Test1 and Pre2 with Test2 etc., right?

Prefix = Array("Pre1", "Pre2", "Pre3")
Varbooks = Array("Test1", "Test2", "Test3")

For Each Pre In Prefix
For Each varBook In Varbooks
Set wb = Workbooks.Open(Filename:="R:\workbooks\Daily\" & varBook
Run "RefreshOnOpen"
With wb
With Range("I4")
.Formula = "=TODAY() + 1"
Run "PrintToPDF_MultiSheetToOne_RunEarly"
.Formula = "=TODAY()"
ActiveWorkbook.SaveAs Filename:="R:\Workbooks\Completed\" & Pre &VBA.Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1) & "_" & VBA.Format(Date + 1, "mmddyyyy") & ".xls"
End With
End With
Next varBook
next Pre

jo15765
11-21-2011, 02:38 PM
I tried the code that I posted above and it did'nt function as needed. What this does, is assign the first Pre in Prefix to each Var in Varbooks, then it assigns the 2nd Pre in Prefix to each var in Varbooks. I was needing it to assign Pre1 with Var1, Pre2 with Var2 etc.

nilem
11-21-2011, 11:34 PM
Maybe so:
Dim i As Long
Prefix = Array("Pre1", "Pre2", "Pre3")
Varbooks = Array("Test1", "Test2", "Test3")

For i = 0 To UBound(Varbooks) ' note UBound(Varbooks)=UBound(Prefix)
Set wb = Workbooks.Open(Filename:="R:\workbooks\Daily\" & Varbooks(i))
Run "RefreshOnOpen"
With wb
With Range("I4")
.Formula = "=TODAY() + 1"
Run "PrintToPDF_MultiSheetToOne_RunEarly"
.Formula = "=TODAY()"
ActiveWorkbook.SaveAs Filename:="R:\Workbooks\Completed\" & Prefix(i) & Left(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1) & "_" & Format(Date + 1, "mmddyyyy") & ".xls"
End With
End With
Next i

jo15765
11-29-2011, 03:24 PM
That got it, thank you for the assistance.