Different behaviour on operating systems

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
It is well known that when writing VBA to use on a Mac, things to avoid include ActiveX objects, Dictionaries and APIs.

Today I came across a problem and thankfully I was told where the problem was.

The following code returns different results depending whether it's run on Windows or Mac.

Feel free to test.

Code:
Option Explicit

    Dim Counter As Integer
    
Public Sub Start()

    Sheet1.Cells(1, 1).Resize(Counter, 1).Value = Sec()
    
End Sub

Public Function Sec() As Variant
    
    Counter = 3
    
    Dim NewArray() As Variant
    
    ReDim NewArray(1 To Counter, 1 To 1) As Variant
    
    Dim i As Integer
    
    For i = 1 To Counter
    
        NewArray(i, 1) = i
        
    Next i
    
    Sec = NewArray()

End Function

On Windows, it returns the values 1, 2 and 3.

On a Mac, it'll return blank.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
It doesn't return blank on my mac (11.4 Big Sur, Excel 16.54). It returns 1,2,3 as it does on Windows. Have you got a specific environment where this is failing?

As an aside, the code is a bit confusing though and I'd be tempted to refactor ?‍♂️
 
Upvote 0
It doesn't return blank on my mac (11.4 Big Sur, Excel 16.54). It returns 1,2,3 as it does on Windows. Have you got a specific environment where this is failing?

The code is somewhat confusing through and probably should be refactored ?‍♂️
The "real" code is much longer. I picked out the bit that was relevant and posted it here.

Will check again and update in due course.

Thanks for testing though.
 
Upvote 0
It is well known that when writing VBA to use on a Mac, things to avoid include ActiveX objects, Dictionaries and APIs.

Today I came across a problem and thankfully I was told where the problem was.

The following code returns different results depending whether it's run on Windows or Mac.

Feel free to test.

Code:
Option Explicit

    Dim Counter As Integer
   
Public Sub Start()

    Sheet1.Cells(1, 1).Resize(Counter, 1).Value = Sec()
   
End Sub

Public Function Sec() As Variant
   
    Counter = 3
   
    Dim NewArray() As Variant
   
    ReDim NewArray(1 To Counter, 1 To 1) As Variant
   
    Dim i As Integer
   
    For i = 1 To Counter
   
        NewArray(i, 1) = i
       
    Next i
   
    Sec = NewArray()

End Function

On Windows, it returns the values 1, 2 and 3.

On a Mac, it'll return blank.
Actually, second thoughts, I think it crashed on the mac, so I'm told.
 
Upvote 0
I think the problem probably lies elsewhere, that said, I think the code would be better written without the module level variable for both mac and Windows, Sec should probably accept it as an argument and then Start defines it.
 
Upvote 0
I think the problem probably lies elsewhere, that said, I think the code would be better written without the module level variable for both mac and Windows, Sec should probably accept it as an argument and then Start defines it.
Thanks for your suggestion, I'll bear that in mind going forwards.
 
Upvote 0

Forum statistics

Threads
1,214,998
Messages
6,122,639
Members
449,093
Latest member
Ahmad123098

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