Merge macro can't find file

allenayres83

New Member
Joined
Jan 19, 2021
Messages
4
Office Version
  1. 2016
Platform
  1. Windows
I have a macro set up to merge excel worksheets into into a single workbook from a list of file names. The macro works but I get a popup at the end saying that it cant find "mydirectory/". What I beleive is happening is that excel is looking past my last entry into an empty cell causing the macro to look for a file with no name. Below is my code. Is there a way to fix this?

Also, I can get this code to pull files from a local drive but not my network drive. I have to log in to a computer to use the network drive for work. So I am guessing that it cant access it because it is password protected. Could that be the problem and if so can that be fixed?

VBA Code:
Option Explicit
Enum ReadColumns
    rcItemCode = 3
    rcLineItem = rcItemCode - 1
    rcSupplementalDescription = rcItemCode + 2
    rcUnits = rcItemCode + 1
    rcBidQuantity = rcItemCode + 5
    rcUnitPrice = rcItemCode + 4
    
    
End Enum
Sub MergeandFillHeader()
    Dim v As Variant, i As Long
    Dim wb As Workbook, n As Long

    With Application
        .ScreenUpdating = False
        .Calculation = xlAutomatic
    End With
    
    With ThisWorkbook
        v = .Worksheets("Data").Range("C11").CurrentRegion.Value
        For i = 3 To UBound(v)
            Set wb = Workbooks.Open("C:\Users\d04m9\Desktop\test" & "\" & v(i, rcItemCode) & ".xlsm")
            wb.Sheets(1).Copy After:=.Sheets(.Sheets.Count)
            With .Sheets(.Sheets.Count)
                .Cells(1, 4).Value = Worksheets("Merge").Range("D1").Value
                .Cells(3, 4).Value = v(i, rcLineItem)
                .Cells(4, 4).Value = v(i, rcSupplementalDescription)
                .Cells(6, 4).Value = v(i, rcBidQuantity)
                .Cells(8, 4).Value = v(i, rcUnitPrice)
            End With
            n = n + 1
            wb.Close False
        Next i
    End With
    
    With Application
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
    
    MsgBox "Processed " & n & " files", Title:="Merge Excel files"
End Sub
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,214,644
Messages
6,120,709
Members
448,983
Latest member
Joaquim_Baptista

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