FJSeminario
New Member
- Joined
- Mar 26, 2011
- Messages
- 12
I have a Macro that works fine when accessing files in a Directory in my C: drive but when I copy it to my USB Flash drive and change the reference to access files from my USB flash drive I get Run-time error '1004'
My Flash drive is assigned a "E:\" root in my computer but it varies depending on which computer I connect my Flash drive to.
Could it be that the "E:\" Drive if not really Drive "E:\" but a way for my computer to identify it?
This is the code:
My Flash drive is assigned a "E:\" root in my computer but it varies depending on which computer I connect my Flash drive to.
Could it be that the "E:\" Drive if not really Drive "E:\" but a way for my computer to identify it?
This is the code:
Code:
Option Explicit
'========================================================================
' The objective of this Macro is to Copy a Range (A16:J16) from all
' the Excel files in C:\DCR\2011\Invoices\ into this sheet which
' contains this Macro starting at row 5, so that I end up with a line
' for each file that exists in the Directory C:\DCR\2011\Invoices\
'========================================================================
Dim strThisExcelFile As String
Dim strPath As String
Dim strFile As String
Dim x As Integer
Sub GetInvoices()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
Sheets("Info").Select
Range("C13").Select 'Cell C13 contains the name of the
'Excel file which contains this Macro
strThisExcelFile = ActiveCell.Value
Range("C12").Select 'Cell C12 contains the Path (Directory)
'where the invoices reside
strPath = ActiveCell.Value
strFile = Dir(strPath)
Sheets("AllInvoices").Select
x = 4
ChDir strPath
Do While strFile <> ""
x = x + 1
Workbooks.Open Filename:=strFile '<== This is where I get the Error
Sheets("Info").Select
Range("A16:J16").Select: Selection.Copy
Windows(strThisExcelFile).Activate
Sheets("AllInvoices").Select
Range("A" & x).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Windows(strFile).Activate: ActiveWindow.Close ' Close Current Excel Invoice
strFile = Dir ' Get next entry.
Loop
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub ' End of Sub GetInvoices()