Running a macro on Excel(Mac) with an 'Invalid procedure call or argument' response

vitto

New Member
Joined
Oct 3, 2024
Messages
3
Office Version
  1. 365
Platform
  1. MacOS
Hi everyone!

I was trying to run a small macro within a spreadsheet (Microsoft® Excel for Mac).

The same error occurs every time: Invalid procedure call or argument ( see enclosed screenshots).

Probably someone here had the same issue before.

Thanks!

Best regards,
Vitto
 

Attachments

  • Screenshot 2024-10-03 at 22.11.26.png
    Screenshot 2024-10-03 at 22.11.26.png
    73.8 KB · Views: 14
  • Screenshot 2024-10-03 at 22.16.19.png
    Screenshot 2024-10-03 at 22.16.19.png
    49.7 KB · Views: 14

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
It is not possible to diagnose this with a screenshot of one line of code. The root of a problem is often not in the line of code flagged by VBA as the error.

Show us the code that calls this Sub.
Show us all the code in this Sub.

Copy from your code editor and paste directly into your post. To preserve code formatting, select the code then click the VBA button in the edit controls to add CODE tags around your code.
 
Upvote 0
Hi!

VBA Code:
Sub loadlists()
    Dim myPath As String, Wb As Workbook, nRow As Long, llName As String, numRow As Long
    Dim fromDate As Date, toDate As Date, curDate As Date, columnStart As Long, columnEnd As Long
    Dim numSku As Long, Cust_id As String, CustLocCity As String, CustDest As String, CustCont As String
    Dim qty As Long, skuN As String
  
    Application.ScreenUpdating = False
    numSku = 4  ' number of SKU
    Set Wb = ActiveWorkbook
    Set sh = Wb.ActiveSheet  'Master sheet
    Set sht = Wb.Sheets("template_load")
  
    myPath = Wb.Path  'maybe use for save lists as separate files
    sep = Application.PathSeparator
    ext = ".xlsx"
    nLists = 0  ' Number of created lists
  
    nRow = sh.Cells(Rows.Count, 4).End(xlUp).Row  'number of filled rows Customer at Master sheet
  
    fromDate = sh.Range("fromDate")
    toDate = sh.Range("toDate")
  
    columnStart = findColumn(fromDate)  'column with from date
    columnEnd = findColumn(toDate)      'column with to date
  
    If columnStart > 0 And columnEnd > 0 Then
        With sh
            ' loop for Date`s columns
            For col = columnStart To columnEnd Step numSku   ' Step = number of SKU
                curDate = .Cells(3, col)
                ' Check if this date valid and filled at least one SKU:
                ' ToDo: need recreate this chesk when number of SKU wiil increased
                If IsDate(curDate) _
                    And (.Cells(Rows.Count, col).End(xlUp).Row > 4 _
                    Or .Cells(Rows.Count, col + 1).End(xlUp).Row > 4 _
                    Or .Cells(Rows.Count, col + 2).End(xlUp).Row > 4 _
                    Or .Cells(Rows.Count, col + 3).End(xlUp).Row > 4) Then
                  
                    llName = Format(curDate, "dd.mm.yyyy")  'name for load list like "02.09.2024"
                  
                    ' Create the New Workbook
                    On Error Resume Next
                    If sht.Copy Then
                        Set Wbo = ActiveWorkbook
                        Set sho = Wbo.Sheets(1)
                        sho.Name = llName   ' Name Sheet
                        sho.Visible = True  ' Create it visible
      
                        Wbo.SaveAs (myPath & sep & llName & ext)
                    Else
                        MsgBox ("Error creating new file")
                        Exit Sub
                    End If
                    On Error GoTo 0
                                      
                    ' Clear sheet before fill it:
                    sho.Range("A2:M" & sho.Cells(Rows.Count, 9).End(xlUp).Row).ClearContents
                    numRow = 1
                  
                    sh.Activate
                  
                    ' loop for ID`s
                    For i = 5 To nRow
                        ' fill Customer info
                        Cust_id = .Cells(i, 4)
                        CustLocCity = searchID(Cust_id, 2)
                        If CustLocCity = "-1" Then   ' Not found this ID
                            MsgBox ("Customer " & Cust_id & " do not found in DICTIONARY! Please add it.")
                            Exit Sub
                        End If
                        CustDest = searchID(Cust_id, 3)
                        CustCont = searchID(Cust_id, 4)
                        ' loop for SKU
                        For s = 1 To numSku
                            qty = .Cells(i, col + s - 1)
                            skuN = .Cells(4, col + s - 1)
                            If qty > 0 Then
                                sho.Cells(numRow + 1, 2) = numRow
                                sho.Cells(numRow + 1, 3) = CustLocCity
                                sho.Cells(numRow + 1, 4) = CustDest
                                sho.Cells(numRow + 1, 9) = curDate
                                sho.Cells(numRow + 1, 10) = skuN
                                sho.Cells(numRow + 1, 11) = qty
                                sho.Cells(numRow + 1, 12) = Cust_id
                                sho.Cells(numRow + 1, 13) = CustCont
                                numRow = numRow + 1
                            End If
                        Next s  ' loop for SKU
                    Next i   ' loop for ID`s
                    Wbo.Save
                    Wbo.Close
                    nLists = nLists + 1
                End If  ' Check if this date valid...
                'Application.StatusBar = "Progress: " & col & " of " & columnEnd & ": " & Format(col / columnEnd, "Percent")
            Next col   ' loop for Date`s columns
        End With
    End If
    'Application.StatusBar = False
    sh.Activate
    MsgBox ("Created " & nLists & " list(s).")
    'Wb.Save
    Application.ScreenUpdating = True

          
End Sub

Function findColumn(findDate As Date) As Long
    Set fDate = ActiveWorkbook.ActiveSheet.Rows(3).Find(findDate, LookIn:=xlValues, lookat:=xlWhole)
    If Not fDate Is Nothing Then
        findColumn = fDate.Column
    Else
        findColumn = -1
    End If

End Function
Function searchID(Cust_id As String, colmn As Integer) As String
    With ActiveWorkbook.Sheets("DICTIONARY")
        Set fid = .Columns(1).Find(Cust_id, LookIn:=xlValues, lookat:=xlWhole)
        If Not fid Is Nothing Then
            searchID = .Cells(fid.Row, colmn)
        Else
            searchID = "-1"
        End If
    End With

End Function
 
Last edited by a moderator:
Upvote 0
I only got down to the line below but this line is invalid:
VBA Code:
If sht.Copy Then

Until you figure out what is going wrong I would remove the attempt at error handling here for now and
Replace this:
VBA Code:
                    If sht.Copy Then
                        Set Wbo = ActiveWorkbook
                        Set sho = Wbo.Sheets(1)
                        sho.Name = llName   ' Name Sheet
                        sho.Visible = True  ' Create it visible
      
                        Wbo.SaveAs (myPath & sep & llName & ext)
                    Else
                        MsgBox ("Error creating new file")
                        Exit Sub
                    End If

With this:
VBA Code:
                    sht.Copy

                        Set Wbo = ActiveWorkbook
                        Set sho = Wbo.Sheets(1)
                        sho.Name = llName   ' Name Sheet
                        sho.Visible = True  ' Create it visible
      
                        Wbo.SaveAs (myPath & sep & llName & ext)
 
Upvote 0
How is loadlists being called? From another sub, or are you invoking it from the Macros user interface?

I also suggest using Option Explicit and declaring all variables. sht is not declared. That is not causing your error, but it is to your advantage to make this as clean as possible to avoid red herrings. And you set it, then copy it, but then never reference it again. It does appear to be even needed.
 
Upvote 0
The error pointed out by Alex is a compiler error and will prevent the code from running at all. I have revised that section with what I think you were trying to do. I also declared all the undeclared variables. This should at least run now, but I don't know if it does what you want.
Rich (BB code):
Option Explicit

Sub loadlists()
    Dim myPath As String, Wb As Workbook, nRow As Long, llName As String, numRow As Long
    Dim fromDate As Date, toDate As Date, curDate As Date, columnStart As Long, columnEnd As Long
    Dim numSku As Long, Cust_id As String, CustLocCity As String, CustDest As String, CustCont As String
    Dim qty As Long, skuN As String
   
    Dim sht As Worksheet, sh As Worksheet
    Dim sep As String
    Dim ext As String
    Dim nlists As Long
    Dim col As Long
    Dim WBO As Workbook
    Dim SHO As Worksheet
    Dim i As Long
    Dim S As Long
   
    Application.ScreenUpdating = False
    numSku = 4  ' number of SKU
    Set Wb = ActiveWorkbook
    Set sh = Wb.ActiveSheet  'Master sheet
    ' Set sht = Wb.Sheets("template_load")
 
    myPath = Wb.Path  'maybe use for save lists as separate files
    sep = Application.PathSeparator
    ext = ".xlsx"
    nlists = 0  ' Number of created lists
 
    nRow = sh.Cells(Rows.count, 4).End(xlUp).Row  'number of filled rows Customer at Master sheet
 
    fromDate = sh.Range("fromDate")
    toDate = sh.Range("toDate")
 
    columnStart = findColumn(fromDate)  'column with from date
    columnEnd = findColumn(toDate)      'column with to date
 
    If columnStart > 0 And columnEnd > 0 Then
        With sh
            ' loop for Date`s columns
            For col = columnStart To columnEnd Step numSku   ' Step = number of SKU
                curDate = .Cells(3, col)
                ' Check if this date valid and filled at least one SKU:
                ' ToDo: need recreate this chesk when number of SKU wiil increased
                If IsDate(curDate) _
                    And (.Cells(Rows.count, col).End(xlUp).Row > 4 _
                    Or .Cells(Rows.count, col + 1).End(xlUp).Row > 4 _
                    Or .Cells(Rows.count, col + 2).End(xlUp).Row > 4 _
                    Or .Cells(Rows.count, col + 3).End(xlUp).Row > 4) Then
                 
                    llName = Format(curDate, "dd.mm.yyyy")  'name for load list like "02.09.2024"
                 
                    ' Create the New Workbook
                    On Error Resume Next
                    sht.Copy
                    If Err.Number = 0 Then
                        Set WBO = ActiveWorkbook
                        Set SHO = WBO.Sheets(1)
                        SHO.Name = llName   ' Name Sheet
                        SHO.Visible = True  ' Create it visible
     
                        WBO.SaveAs (myPath & sep & llName & ext)
                    Else
                        MsgBox ("Error creating new file")
                        Exit Sub
                    End If
                    On Error GoTo 0
                                     
                    ' Clear sheet before fill it:
                    SHO.Range("A2:M" & SHO.Cells(Rows.count, 9).End(xlUp).Row).ClearContents
                    numRow = 1
                 
                    sh.Activate
                 
                    ' loop for ID`s
                    For i = 5 To nRow
                        ' fill Customer info
                        Cust_id = .Cells(i, 4)
                        CustLocCity = searchID(Cust_id, 2)
                        If CustLocCity = "-1" Then   ' Not found this ID
                            MsgBox ("Customer " & Cust_id & " do not found in DICTIONARY! Please add it.")
                            Exit Sub
                        End If
                        CustDest = searchID(Cust_id, 3)
                        CustCont = searchID(Cust_id, 4)
                        ' loop for SKU
                        For S = 1 To numSku
                            qty = .Cells(i, col + S - 1)
                            skuN = .Cells(4, col + S - 1)
                            If qty > 0 Then
                                SHO.Cells(numRow + 1, 2) = numRow
                                SHO.Cells(numRow + 1, 3) = CustLocCity
                                SHO.Cells(numRow + 1, 4) = CustDest
                                SHO.Cells(numRow + 1, 9) = curDate
                                SHO.Cells(numRow + 1, 10) = skuN
                                SHO.Cells(numRow + 1, 11) = qty
                                SHO.Cells(numRow + 1, 12) = Cust_id
                                SHO.Cells(numRow + 1, 13) = CustCont
                                numRow = numRow + 1
                            End If
                        Next S  ' loop for SKU
                    Next i   ' loop for ID`s
                    WBO.Save
                    WBO.Close
                    nlists = nlists + 1
                End If  ' Check if this date valid...
                'Application.StatusBar = "Progress: " & col & " of " & columnEnd & ": " & Format(col / columnEnd, "Percent")
            Next col   ' loop for Date`s columns
        End With
    End If
    'Application.StatusBar = False
    sh.Activate
    MsgBox ("Created " & nlists & " list(s).")
    'Wb.Save
    Application.ScreenUpdating = True

         
End Sub

Function findColumn(findDate As Date) As Long

   Dim fDate As Range
  
    Set fDate = ActiveWorkbook.ActiveSheet.Rows(3).Find(findDate, LookIn:=xlValues, lookat:=xlWhole)
    If Not fDate Is Nothing Then
        findColumn = fDate.column
    Else
        findColumn = -1
    End If

End Function
Function searchID(Cust_id As String, colmn As Integer) As String

   Dim fid As Range
  
    With ActiveWorkbook.Sheets("DICTIONARY")
        Set fid = .Columns(1).Find(Cust_id, LookIn:=xlValues, lookat:=xlWhole)
        If Not fid Is Nothing Then
            searchID = .Cells(fid.Row, colmn)
        Else
            searchID = "-1"
        End If
    End With

End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

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