Excecuting a Macro from a Sheet in a Flash Drive

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:

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()
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
To find the correct path why not record a macro and select the E drive, workbook and sheet.

Then you can put the correct path in your macro.

Cheers
 
Upvote 0
asking
When I Record the Macro it says"E:\DCR\2011\Invoices\"

The Macro works fine when I select the Files from the C:\DCR\2011\Invoices\"
but when I change it to the "E:\DCR\2011\Invoices\" it can not find it.
I copied the same directory structure to the "E:\" drive

Thanks Fernando Seminario
 
Upvote 0

Forum statistics

Threads
1,224,607
Messages
6,179,871
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