Function .Activate and .Close not working on Mac/Excel 2016 - workaround?

Kruggie

New Member
Joined
Jul 16, 2019
Messages
9
Hi team,

I have a Macro that runs like a dream on a PC, but on the Mac (which is the computer I use) it does not. I've traced it to the two lines highlighted in red. The macro runs when I remove them, but that means I have to manually close hundreds of files. Is there a workaround or fix for this? Any help is much appreciated


Code:
Sub ReadInFiles()'
' ReadInFiles Macro
'
Dim looper As Integer
Dim FileName As String


For looper = 1 To 200 
   Windows("USV_data-master_v0.1").Activate
    Sheets(1).Select
    FileName = Cells(looper, 1)


    Workbooks.Open FileName:= _
        "/Users/wilkinli/Documents/Experiments Data/USV/Raw data/" & FileName
    Columns("A:P").Select
    Selection.copy
    Windows("USV_data-master_v0.1").Activate
    Sheets.Add After:=ActiveSheet
    ActiveSheet.Paste
    ActiveSheet.Name = Left(FileName, 9)
 
[COLOR=#ff0000]    Windows(FileName).Activate[/COLOR]
[COLOR=#ff0000]    ActiveWindow.Close[/COLOR]
Next looper


End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
replace the two lines in red with
Code:
 Workbooks(FileName).Close
 
Upvote 0
replace the two lines in red with
Code:
 Workbooks(FileName).Close

That works, wohoo! However, now I get the "Grant access" request caused by Apple's sandboxing, as well as the "There is a large amount of information on the clipboard. Do you want to be able to paste this information into another program later?" query from Excel. Is there a way to have code deal with that?
 
Upvote 0
Don't know if it'll help with the 'sandbox' thing but it should stop the Excel message clipboard message.
Code:
Option Explicit

Sub ReadInFiles()
Dim wbDst As Workbook
Dim wbSrc As Workbook
Dim wsDst As Worksheet
Dim rngSrc As Range
Dim looper As Long
Dim FileName As String

' ReadInFiles Macro
'

    Set wbDst = Workbooks("USV_data-master_v0.1")
    
    For looper = 1 To 200

        FileName = wbDst.Sheets(1).Cells(looper, 1)

        Set wbSrc = Workbooks.Open(FileName:="/Users/wilkinli/Documents/Experiments Data/USV/Raw data/" & FileName)
        Set rngSrc = wbSrc.ActiveSheet.Columns("A:P")
        Set wsDst = wbDst.Sheets.Add(After:=wbDst.ActiveSheet)
        
        rngSrc.Copy
        
        wsDst.Paste
        
        Application.CutCopyMode = False
        
        wsDst.Name = Left(FileName, 9)
 
        wbSrc.Close
    
    Next looper

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

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