VBA code not working on Mac

Linki

New Member
Joined
Jun 8, 2023
Messages
25
Office Version
  1. 365
Platform
  1. Windows
Hi everyone!

I've got a piece of code that @Candyman8019 kindly helped me create.
It works well on windows, but when opened on a Macbook, it does not work.
Would anyone here know how to adjust the below code to work on windows as well as mac?

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'if User logged in then don't auto-close
' NOTE:  Since this uses the Windows login name, make sure it is correct below.
If Environ("username") = "User1" Or Environ("username") = "User2" Then
    Call TimeStop
    Exit Sub
End If

'For all other users...timer is enabled.
   Call TimeStop
   Call TimeSetting
End Sub


Private Sub Workbook_Open()
Dim email As String
Dim sht As Worksheet
Dim LastRow As Long


'Check if anyone is currently using the workbook
    If Sheet2.Range("B4").Value <> "" Then
        MsgBox "Workbook is in use by " & Sheet2.Range("B4").Value & ".  Please try again later."
        ActiveWorkbook.Close Savechanges:=False
    Else
       Sheet2.Range("B4").Value = Environ("username")
       ActiveWorkbook.Save
    End If

'Start workbook auto-close timer
Call TimeSetting

'hide all sheets...at least one sheet must remain visible.
'Use VeryHidden to ensure users cannot simply unhide other sheets without going into VBA.
For Each sht In Worksheets
    If sht.CodeName <> "Sheet1" Then sht.Visible = xlSheetVeryHidden
Next

'determine email of current user
Dim objOutlook As New Outlook.Application
email = Trim(objOutlook.getnamespace("MAPI").currentuser.AddressEntry)

'Email = CreateObject("WScript.Shell").RegRead("HKEY_CURRENT_USER\Software\Microsoft\Office\16.0\Common\Identity\ADUserName")
'allows opening of app on non-domain connected computer.
On Error Resume Next


LastRow = Sheet4.Cells.Find("*", searchorder:=xlByRows, searchdirection:=xlPrevious).Row
'Display sheet applicable to department
For x = 2 To LastRow
    If email = Sheet4.Range("A" & x).Value Then
        Worksheets(Sheet4.Range("B" & x).Value).Visible = True
    End If
Next x
'Select Case Dept
'    Case Is = "DataCenter Management"
'        Sheet2.Visible = xlSheetVisible
'    Case Is = "Deskside Support"
'        Sheet3.Visible = xlSheetVisible
'End Select

Dim ws As Worksheet

For Each ws In ThisWorkbook.Sheets
    If ws.Name <> "CurrentUser" Then
              ws.Protect Password:=”iwantin”, Userinterfaceonly:=True

             ws.EnableOutlining = True
    End If
Next ws

End Sub

Private Sub workbook_beforeclose(Cancel As Boolean)

'hide all sheets except message sheet'
'Use VeryHidden to ensure users cannot simply unhide other sheets without going into VBA.

    For Each sht In Sheets
        If sht.CodeName <> "Sheet1" Then
        sht.Visible = xlSheetVeryHidden
        End If
    Next sht

'clear current user so next user can access the sheet.
    Sheet2.Range("B4").Value = ""
   
End Sub

Thanks in advance!
Have a wonderful weekend :)

Linki
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Describe what you mean by "it does not work". Does the code run or not? Does nothing happen when the code runs? Do you get an error message? If so, what is the message (not the error number, which may not be very helpful), and what line is highlighted when you click Debug in the error message?

I suspect the problem may be with Environ, but I am not at my own Mac to test this.
 
Upvote 0
that would make sense since that is looking for a windows environment variable. Is there an equivalent to get the username on a MAC?
 
Upvote 0
I don't know off the top of my head. You could search Google for 'mac excel vba get user name'.
 
Upvote 0
Thanks Jon. Did a quick google search. @Linki, try replacing your workbook_sheetchange routine with this and see how things work for the MAC.

VBA Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
'if User logged in then don't auto-close
' NOTE:  Since this uses the Windows login name, make sure it is correct below.

'MAC
If Environ("USER") = "User1" Or Environ("USER") = "User2" Then
    Call TimeStop
    Exit Sub
End If

'Windows
If Environ("username") = "User1" Or Environ("username") = "User2" Then
    Call TimeStop
    Exit Sub
End If

'For all other users...timer is enabled.
   Call TimeStop
   Call TimeSetting
End Sub
 
Upvote 0
Thank you both! Sorry for the slow reply!
And you're so right Jon! Don't know how I could've forgotten to attach that!
The error message I get is this:
 

Attachments

  • Mac VBA error.jpeg
    Mac VBA error.jpeg
    164.7 KB · Views: 25
Upvote 0
I have had absolutely no joy trying to automate one Office program from another on a Mac. What works fine in Windows, and what worked fine in Mac Office 2011, fails miserably in current Mac Office versions.
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,117
Members
453,021
Latest member
Justyna P

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