How to properly read Value2 Range ?
Hi
First you have to declare FileToOpen explicit as String, else it will
be declared as Variant. Second, only loop through column A and
remember that Value2 shift one row down at each iteration, so you do
not have to increment X.
Sub Add_Buttons()
Dim wsLogName As String
Dim wsLog As Worksheet
Dim wsLogRange As Range
Dim FileToOpen As String, WrkBookName As String
Dim RangeCell As Object
wsLogName = ActiveWorkbook.Name
Set wsLog = ActiveSheet
With wsLog
Sheets("Log").Activate
lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
Set wsLogRange = Range("A2:A" & lastrow)
End With
For Each Value2 In wsLogRange
FileToOpen = Value2 '(1, 1)
WrkBookName = Value2(1, 2)
On Error Resume Next
Debug.Print FileToOpen & " - " & WrkBookName
Workbooks.Open Filename:=FileToOpen
' Application.Run (WrkBookName & "!ADD_BUTTONS") ' this
works for manual test: "'test.xls'!ADD_BUTTONS"
' for auto
test: Application.Run (wbTarget.Name & "!MacroName")
ActiveWorkbook.Close SaveChanges:=False
Next
MsgBox "Finished Looping All workbooks from Log and running their Sub
Within "
End Sub
Best regards,
Per
On 30 Jul., 18:35, RompStar wrote:
I have two Value2(1) and Value(2) in this test..
Value2(1) is variant (1 to 2)
Value2(2) is variant (1 to 2)
in the first loop it read it right, Value2(1) 1,1 and 1,2
but in the second loop, it don't read right Value(2) 2,1 2,2
Please help if you know how to read it, thanks!
Basically the Range that it reads and sticks into Value2 array ? is
starting at A2:B last row, in this test it is A2:B3 as the range.
A2 has fullpath * filename.xls
B3 has * filename.xls *needed top execute the Sub within each workbook
that is open, as I understand it
Basically the idea of this VBA Proc is to read from my log which
contains \\network\full\path\filename.xls, open each workbook in the
range and then run a Sub within each one called ADD_BUTTONS.
This works on the first workbooks in range, but then for the Next
Value2:
* * FileToOpen = Value2(x, 1) * *- this one has a filename.xls
* * WrkBookName = Value2(x, 2) * - and this one goes blank
FileToOpen has the fullpath & FileName,xls
WrkBookName has just the FineName.xls * when I put a watch on them,
they have the right values, but I think I read it wrong
?
Please advise, maybe I am doing it wrong, not a guru :- )
Sub Add_Buttons()
Dim wsLogName As String
Dim wsLog As Worksheet
Dim wsLogRange As Range
Dim FileToOpen, WrkBookName As String
Dim RangeCell As Object
wsLogName = ActiveWorkbook.Name
Set wsLog = ActiveSheet
With wsLog
* * Sheets("Log").Activate
* * lastrow = .Range("A" & .Rows.Count).End(xlUp).Row
* * Set wsLogRange = Range("A2:B" & lastrow)
End With
x = 1
For Each Value2 In wsLogRange
* * FileToOpen = Value2(x, 1)
* * WrkBookName = Value2(x, 2)
* * On Error Resume Next
* * Workbooks.Open Filename:=FileToOpen
' * *Application.Run (WrkBookName & "!ADD_BUTTONS") * * * * *' this
works for manual test: "'test.xls'!ADD_BUTTONS"
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * ' for auto
test: * Application.Run (wbTarget.Name & "!MacroName")
ActiveWorkbook.Close SaveChanges:=False
x = 1 + 1
Next
MsgBox "Finished Looping All workbooks from Log and running their Sub
within"
End Sub
|