modify multiple excel files on mac - no errors but also no results

dantesp

New Member
Joined
Mar 11, 2020
Messages
9
Office Version
  1. 2011
Platform
  1. MacOS
Let's see who is strong.

A code that does not give errors but which does not modify not even one of the series of excel files (xlsx, Mac 2011) present in the same folder.

What is the problem? :)
VBA Code:
Option Explicit

    Sub Macro2()

    Dim MyFiles As String
    Dim MyPath As String
    Dim FilesInPath As String
    Dim Filename As String
    Dim Fnum As Long

        On Error Resume Next
        MyFiles = MacScript("choose folder as string")
        MyPath = MacScript("return posix path of (choose folder with prompt ""Select the folder"") as string")
        If MyPath = "" Then Exit Sub
        On Error GoTo 0

        If Right(MyPath, 1) <> Application.PathSeparator Then
            MyPath = MyPath & Application.PathSeparator
        End If

        FilesInPath = Dir(MyPath & "XLS#")
        If FilesInPath = "" Then
                 MsgBox "No files found"
                Exit Sub
            End If

            If Fnum > 0 Then

            Filename = Dir(MyPath & "XLS#")

            Do While Filename <> ""

        Workbooks.Open ":" & MyFiles

        MsgBox ActiveWorkbook.Name

            insertc wb:=ActiveWorkbook

        ActiveWorkbook.Close SaveChanges:=True

        Loop

        End If
        End Sub

about insertc
Code:
Sub insertc(ByVal wb As Workbook)
        wb.Range("A5").Value = "ca1"
        wb.Range("A6").Value = "ca2"
End Sub

Many thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Why not step through the code with F8 and test that each line is doing what it should?
 
Upvote 0
Done. "Sub or function not defined". I don't understand why, the code seem ok.
 
Upvote 0
What is MacScript? Is that a real function?
 
Upvote 0
Dermot, MacScript is a function usable on Mac system, of course.
 
Upvote 0
I have updated the script as follows but nothing changes. No mistakes, no results.

Sub ModifyAllFiles()

On Error Resume Next
MyPath = "Macintosh HD:Users:Danespola:Desktop:test"
If MyPath = "" Then Exit Sub
On Error GoTo 0

If Right(MyPath, 1) <> Application.PathSeparator Then
MyPath = MyPath & Application.PathSeparator
End If

FilesInPath = Dir(MyPath, MacID("XLSX"))
If FilesInPath = "" Then
MsgBox "No files found"
Exit Sub
End If

If Fnum > 0 Then

Do While Filename <> ""
Application.ScreenUpdating = False
Workbooks(FilesInPath).Open
Range("A5").Value = "ca1"
Range("A6").Value = "ca2"
Workbooks(FilesInPath).Save
Workbooks(FilesInPath).Close
Filename = Dir()
Loop
Application.ScreenUpdating = True

End If

End Sub

Thanks
 
Upvote 0
You need to step through line by line (F8) and see how far it is getting
 
Upvote 0
Nothing happens that is not a highlight of the simple Sub ModifyAllFiles () without any explanation.
 
Upvote 0
This is the solution. Solved.


Option Explicit

Sub ModifyAllFiles()

Dim Filename As String, MyPath As String, count As Integer
Dim wb As Workbook, t0 As Single
t0 = Timer

MyPath = "Macintosh HD:Users:User:Desktop:test"
If Right(MyPath, 1) <> Application.PathSeparator Then
MyPath = MyPath & Application.PathSeparator
End If

Filename = Dir(MyPath, MacID("XLSX"))
If Filename = "" Then
MsgBox "No files found"
Exit Sub
Else
Application.ScreenUpdating = False

Do While Filename <> ""
count = count + 1
Set wb = Workbooks.Open(MyPath & Filename)
With wb.Sheets(1)
Call insertlinea
End With
wb.Save
wb.Close
Filename = Dir()
Loop
Application.ScreenUpdating = True
End If
MsgBox count & " files updated", vbInformation, "Finished in " & Int(Timer - t0) & " secs"

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,999
Messages
6,122,645
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