Allowing for Different Versions of Excel in VBA Code

jeffspringer

New Member
Joined
Dec 2, 2015
Messages
3
I have a macro that works for the 2011 version of Excel for Mac, but causes a Subscript out of Range error on the 2016 version of Excel.

The error occurs on the following line:

Code:
Windows("METRCMJF Reconciliation.xlsm").Activate

After some experimentation, I'm pretty sure that it's because the 2011 version of Excel for Mac requires the extension (".xlsm") but the 2016 version does not. I wish I could just remove the extension, but the macro also needs to work on the PC version of Excel. The PC version of Excel requires the extension.

Due to the differences in file structure between Mac and PC, I've had to test for the operating system with the following code:
Code:
    Dim TheOS As String

    TheOS = Application.OperatingSystem
    If Left(TheOS, 7) = "Windows" Then
        Workbooks.Open Filename:=ThisWorkbook.Path & "\RawData\PlantsInventoryReport-1.xls"


    Else
        Workbooks.Open Filename:=ThisWorkbook.Path & ":RawData:PlantsInventoryReport-1.xls"

    End If

So, my first thought was to create the same if/then statement for the line that's causing the error. However, there are 13 different files this macro needs to reference and select throughout the run. So, I thought to simply define variables for each of the files and set the variables at the beginning based upon the operating system.

But then, I thought, "What happens when the next version of Excel comes out and everything breaks again?

So, I'm hoping someone might have a more elegant way of handling this problem.

Thank you!
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
I can, Dryver14. Unfortunately, if I write: Windows("METRCMJF Reconciliation.xlsm").Activate the code will work for PC and Excel 2011 for Mac, but won't work for Excel 2016 for Mac.

If I write: Windows("METRCMJF Reconciliation").Activate, the code will work for Excel 2016 Mac, but not Excel 2011 for Mac or PC.
 
Upvote 0
Can you try this and see if following works for you?

To activate the file:
Code:
For Each w In Application.Workbooks
    If w.Name Like "METRCMJF Reconciliation*" Then w.Activate 
Next w

As for creating file path with right type of separator:
Code:
sPath = Replace(ThisWorkbook.Path & "^Folder1^Folder2^FileName.xls", "^", _
            Application.PathSeparator, 1, -1, vbTextCompare)
 
Upvote 0

Forum statistics

Threads
1,225,739
Messages
6,186,743
Members
453,370
Latest member
juliewar

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