Code only runs on Excel 2007 if file is opened from within Excel

liroch

I have some relatively simple code that loops through multiple files in a folder, opens each and copies a range of data into a new worksheet. I wrote the code using Excel 2013. Some users have 2007 on their box. If the 2007 users open the file from within Excel (i.e File-Open, and navigate to the directory), it runs fine. If the 2007 users open the file from Windows Explorer, all the macros run, except the file name variable "myFile" that I use in the loop is always EMPTY. It's like it won't navigate to the directory. I do not get any errors, it just executes the code and goes right to the end of the loop (which says to stop when "myFile" is empty) -- then continues on with the next macro. With Excel 2013, the program runs fine regardless of how it is opened. I thought my answer might have been contained in this thread: Excel Workbook Open Event macro doesn't always run but the Q&A doesn't seem to apply directly to my issue since the macros actually run. It seems to be just the navigation to the file location that won't work. Here is the snippet of code that runs the loop:

    Dim myfile As String
Dim wb As Workbook
Dim ws As Worksheet
Dim DataBlock As Range

'Optimize Macro Speed
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

Set wb = Workbooks("ComboFile.xlsm")
Set ws = wb.Sheets("Sheet1")    'change desired sheet

ChDir "r:\BSI\Sys9000"
myfile = Dir("*.xlsx")

Do Until myfile = ""
    Workbooks.Open Filename:=myfile
    Set DataBlock = Range("A2").CurrentRegion
    DataBlock.Copy ws.Cells(ws.Rows.Count, "A").End(xlUp).Offset(1)
    Windows(myfile).Close
    myfile = Dir
Loop

'Reset Macro Optimization Settings
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

Any suggestions welcome. Thank you.

Rory

I would recommend you replace this:

ChDir "r:\BSI\Sys9000"
myfile = Dir("*.xlsx")

with just this:

myfile = Dir("r:\BSI\Sys9000\*.xlsx")

ChDir won't change drives so if the current directory is not on the R drive, the code would fail.

You will also need to change this:

Workbooks.Open Filename:=myfile

to include the path:

Workbooks.Open Filename:="r:\BSI\Sys9000\" & myfile

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Excel VBA Code to select opened Excel file

From Dev

Run Excel VBA only if Windows task scheduler opened the file

From Dev

File.ReadAllLines() fails to read from a file that is opened by Excel

From Dev

Filter an Excel file with Excel 2007 VBA

From Dev

Pulling data from Excel file within Fortran

From Dev

Exported excel file from datagridview shows error when opened VB

From Dev

Excel 2007 COUNTIF in even rows only

From Dev

Copying only the visible cells from visible worksheets into a new workbook, excel 2007 VBA

From Dev

Remove Alphabets only from a H-Column - MS Excel 2007 (Without Plugin)

From Dev

Cannot connect to SQL Server from Excel 2007

From Dev

Run an SQL Query With a Parameter from Excel 2007

From Dev

Delete input field from Excel 2007

From Dev

VBA string search within files in directory not working in Excel 2007

From Dev

VBA Code runs in excel 2013 but not in 2010

From Dev

VBA Code runs in excel 2013 but not in 2010

From Dev

Writing to a file with QFile fails, without error code, when already opened in Excel

From Dev

Macro does not run when opening a File/Workbook from within Excel

From Dev

Excel interactions don't work after Excel file opened

From Dev

Autofilter a protected excel sheet opened from access

From Dev

Import data from opened excel files

From Dev

Autofilter a protected excel sheet opened from access

From Dev

Upgrade from Excel 2007 to Office 365: Excel very slow

From Dev

Excel 2007 - refering value from other worksheet in another function in excel

From Dev

How to change the label value on Excel 2007 ribbon programatically when Worksheet is opened

From Dev

Go to row in Excel 2007

From Dev

2007 Excel angled text

From Dev

Flatten Excel Formula (2007)

From Dev

Excel 2007 Advanced Filtering

From Dev

Column Manipulation in Excel 2007

Related Related

  1. 1

    Excel VBA Code to select opened Excel file

  2. 2

    Run Excel VBA only if Windows task scheduler opened the file

  3. 3

    File.ReadAllLines() fails to read from a file that is opened by Excel

  4. 4

    Filter an Excel file with Excel 2007 VBA

  5. 5

    Pulling data from Excel file within Fortran

  6. 6

    Exported excel file from datagridview shows error when opened VB

  7. 7

    Excel 2007 COUNTIF in even rows only

  8. 8

    Copying only the visible cells from visible worksheets into a new workbook, excel 2007 VBA

  9. 9

    Remove Alphabets only from a H-Column - MS Excel 2007 (Without Plugin)

  10. 10

    Cannot connect to SQL Server from Excel 2007

  11. 11

    Run an SQL Query With a Parameter from Excel 2007

  12. 12

    Delete input field from Excel 2007

  13. 13

    VBA string search within files in directory not working in Excel 2007

  14. 14

    VBA Code runs in excel 2013 but not in 2010

  15. 15

    VBA Code runs in excel 2013 but not in 2010

  16. 16

    Writing to a file with QFile fails, without error code, when already opened in Excel

  17. 17

    Macro does not run when opening a File/Workbook from within Excel

  18. 18

    Excel interactions don't work after Excel file opened

  19. 19

    Autofilter a protected excel sheet opened from access

  20. 20

    Import data from opened excel files

  21. 21

    Autofilter a protected excel sheet opened from access

  22. 22

    Upgrade from Excel 2007 to Office 365: Excel very slow

  23. 23

    Excel 2007 - refering value from other worksheet in another function in excel

  24. 24

    How to change the label value on Excel 2007 ribbon programatically when Worksheet is opened

  25. 25

    Go to row in Excel 2007

  26. 26

    2007 Excel angled text

  27. 27

    Flatten Excel Formula (2007)

  28. 28

    Excel 2007 Advanced Filtering

  29. 29

    Column Manipulation in Excel 2007

HotTag

Archive