Workbooks array?

John Davis

Well-known Member
Joined
Sep 11, 2007
Messages
3,457
Hello All:

I have the following code:

Code:
Sub ReplaceZeros()
Dim i As Long
Dim lr As Long

ChDir "I:\nafta\RFP\DataK\CM\Traffic\Excel"
Workbooks.Open Filename:="I:\nafta\RFP\DataK\CM\Traffic\Excel\AGFA_HC.xls"
Workbooks.Open Filename:="I:\nafta\RFP\DataK\CM\Traffic\Excel\AgfaParts1.xls"
Workbooks.Open Filename:="I:\nafta\RFP\DataK\CM\Traffic\Excel\AgfaParts2.xls"

lr = Cells(Rows.Count, 1).End(3).Row

For i = lr To 2 Step -1

    If Len(Range("A" & i)) = 8 Then
    
        Range("A" & i).Replace What:="000", Replacement:="", LookAt:=xlPart
        
    End If
    
Next i


End Sub

What I need is the actions in the procedure to run on each of the workbooks and then close with the changes. My mind is blank at the moment, any help or advice on how too achieve this would be appreciated.
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
By simple example, you can loop thru the wb's in the folder...

Rich (BB code):
Sub exa()
Dim fil As Variant
Dim wb As Workbook
    
    For Each fil In CreateObject("Scripting.FileSystemObject").GetFolder(ThisWorkbook.Path & "\").Files
        If Mid(fil.Name, InStrRev(fil.Name, ".") + 1) = "xls" _
        And Not fil.Path = ThisWorkbook.FullName Then
            Set wb = Workbooks.Open(ThisWorkbook.Path & "\" & fil.Name)
            wb.Worksheets(1).Range("A1").Value = "Test"
            wb.Close True
        End If
    Next
End Sub
 
Upvote 0
Or maybe something like:
Code:
Sub ReplaceZeros()

    Dim aWorkbook As Variant
    Dim wrkBk As Workbook
    Dim x As Integer
    
    aWorkbook = Array("AGFA_HC.xls", "AgfaParts1.xls", "AgfaParts2.xls")
    
    For x = 0 To 2
        Set wrkBk = Workbooks.Open("I:\nafta\RFP\DataK\CM\Traffic\Excel\" And aWorkbook(x))
        With wrkBk
            .Worksheets("Sheet1").Columns(1).Replace What:="000", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByColumns
            .Save
            .Close
        End With
    Next x
    
End Sub
 
Upvote 0
Or maybe something like:
Code:
Sub ReplaceZeros()

    Dim aWorkbook As Variant
    Dim wrkBk As Workbook
    Dim x As Integer
    
    aWorkbook = Array("AGFA_HC.xls", "AgfaParts1.xls", "AgfaParts2.xls")
    
    For x = 0 To 2
        Set wrkBk = Workbooks.Open("I:\nafta\RFP\DataK\CM\Traffic\Excel\" And aWorkbook(x))
        With wrkBk
            .Worksheets("Sheet1").Columns(1).Replace What:="000", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByColumns
            .Save
            .Close
        End With
    Next x
    
End Sub

Thanks Darren and GTO for your replies. I modified Darren's version too suite my needs and it works perfectly. I needed the Len portions. Excellent, Thank You.
 
Upvote 0
Just noticed I wrote:
Code:
Set wrkBk = Workbooks.Open("I:\nafta\RFP\DataK\CM\Traffic\Excel\" And aWorkbook(x))

Should be:
Code:
Set wrkBk = Workbooks.Open("I:\nafta\RFP\DataK\CM\Traffic\Excel\" & aWorkbook(x))
 
Upvote 0

Forum statistics

Threads
1,224,602
Messages
6,179,845
Members
452,948
Latest member
UsmanAli786

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top