Compile Error argument not optional on Mac only - excel

I have a working Excel sheet in Excel 2010 on Windows 10 which is failing on my client's side on Excel 2011 for Mac because of the "argument not optional" error when they click on the button. Additionally, it isn't working on their end on a different computer running Office 365.
It seems to be highlighting Characters in the 6th line of code:
Sub Rectangle4_Click()
Dim xSelShp As Shape, xSelLst As Variant, I As Integer
Set xSelShp = ActiveSheet.Shapes(Application.Caller)
Set xLstBox = ActiveSheet.ListBox4
If xLstBox.Visible = False Then
xLstBox.Visible = True
xSelShp.TextFrame2.TextRange.Characters.Text = "SAVE"
Else
xLstBox.Visible = False
xSelShp.TextFrame2.TextRange.Characters.Text = "SELECT"
For I = xLstBox.ListCount - 1 To 0 Step -1
If xLstBox.Selected(I) = True Then
xSelLst = xLstBox.List(I) & ";" & xSelLst
End If
Next I
If xSelLst <> "" Then
Range("R4") = Mid(xSelLst, 1, Len(xSelLst) - 1)
Else
Range("R4") = ""
End If
End If
End Sub
The ListBox it is showing/hiding is an ActiveX ListBox.
I doubt it's relevant, but there are 40 of these Rectangle buttons in the sheet, all with near identical code, just switching 4 references.
Can anyone see what I'm missing here? Or know anything about cross-platform issues with code like this?
Thank you.

In case anyone comes across this, it does seem to be that Tim was correct and ActiveX controls do not work on a Mac. But they also did not work in Office 365. I switched this to form controls and got everything working perfectly on my client's side. My advice to my past self is to work with form controls from the beginning and avoid ActiveX at all costs if you are working on something you are sending to someone else.

Related

Method "PasteSpecial" of object "TextRange2" failed when copying from Excel to PPT

I've been searching for quite a while and can't seem to find a solution. I'm trying to copy multiple cells from Excel into a PPT textbox, keeping text only. The code I have seems to work well, but it will randomly error out with the message 'Method "PasteSpecial" of object "TextRange2" failed' When I hit debug, and play, it will continue as normal.
I've tried adding a delay wait since I know the clipboard can get flaky sometimes, but that hasn't seemed to help.
I also came across this post and have tried it but it still randomly errors out: https://answers.microsoft.com/en-us/msoffice/forum/all/powerpoint-vba-flaky-clipboard/3aee03af-b0be-4664-8d17-79e0507210e7
Does anyone know why this error happens randomly and it doesn't even seem to be a real error given I can just hit "play" and it will continue normally.
Here's a snippet of the code:
Sub AddText()
Set oPPTApp = GetObject(, "PowerPoint.Application")
oPPTApp.Visible = True
For x = 1 to 3
ActiveWorkbook.Worksheets("OpenEndChartingNEW").Range("A" & x & ":A" & x * 2).Copy
Application.Wait Now + TimeValue("00:00:05")
oPPTApp.ActivePresentation.slides(6).Shapes("OpenEnd").TextFrame2.TextRange.PasteSpecial msoClipboardFormatPlainText
Next x
Set oPPTApp = Nothing
End Sub
Thank you!

Excel VBA: Can't Access Form Control?

I'm using the latest Office 365 Excel on OS X. I've created a List Box Form Control (ActiveX controls don't seem to be available in OS X), called wb_from:
I'm trying to access this List Box from VBA using this code:
Sub my_Import()
Dim MailStr As String
MailStr = ""
If wb_from.SelectedItems.Count = 0 Then
MsgBox "No User Selected"
Exit Sub
End If
For i = 0 To (wb_from.Items.Count - 1)
If wb_from.Selected(i) Then
MailStr = MailStr & wb_from.Items.Item(i) & "; "
End If
Next i
a = 100
End Sub
Excel is giving me an object required error:
How can I correct this?
What cell did you link that control to via Format Control? That cell address should be used in your code.

"Run-time error '-2147221040 (800401d0)' happen sometimes after excel upgrade from 2010 to 2016

I used VBA to generate the pictures before and the code is shown below, it never happened any error. But when I upgraded the Micro-Soft office version from 2010 to 2016, it happens
"Run-time error '-2147221040 (800401d0)'
occasionally, and it will ok if i try to re-run the same macro. Is there a way to fix this? Thanks in advance~~
Sub Chart_generate()
Dim j As Byte
Dim Max As Byte
Dim TOOL As String
Dim WCpath As String
Sheets("pivot").Select
ActiveSheet.PivotTables("PivotTable1").PivotCache.Refresh
ActiveSheet.PivotTables("PivotTable2").PivotCache.Refresh
ActiveWindow.Zoom = 130
Max = Range("AO1")
For j = 2 To Max + 1
TOOL = Range("AN" & j)
ActiveSheet.PivotTables("PivotTable1").PivotFields("TOOL").CurrentPage = TOOL
ActiveSheet.ChartObjects("Chart1").Activate
WCpath = "D:\users\Picture\" & TOOL & "_Pressure.png"
ActiveChart.ChartArea.Select
ActiveChart.Export (WCpath)
Next j
ActiveWindow.Zoom = 100
Range("A2").Select
End Sub
I have a similar issue when moving graphs generated on hidden worksheets over to a visible worksheet, and then resizing them.
My code is repeated multiple times in a large loop (not seen in code below), and it works more often than not but will periodically fail. If I use the debugger and single step it always works perfectly; it's only a problem when running full speed.
I've tried solutions from the web including DoEvents and the On Error tricks. I added a While loop and some verbal feedback to help identify the code causing the problem.
I hear "runtime 2" more often than "runtime 1". It always works fault-free single stepping in the debugger, but fails occasionally on some computers. During a fail, if I click on the pictures, the name of the object doesn't appear in the upper left box of the worksheet where it normally would.
Of the eleven machines tested, I've seen problems on three of them. Mine works better than most, which is a problem for a code developer!
histogram second
Sheets("Histogram").Activate
ActiveSheet.Shapes.Range(Array("pi_Histogram")).Select
DoEvents
On Error Resume Next
errflag = 0
Selection.Copy
While Err.Number = 1004 And errflag < 10
Err.Number = 0
Application.Speech.Speak "runtime 1"
Selection.Copy
errflag = errflag + 1
Wend
Sheets("Raw Data").Select
Range("V23").Select 'start insertion point for coefficients table
ActiveCell.Offset(1, 10 + segment).Select
DoEvents
errflag = 0
ActiveSheet.Pictures.Paste.Select
While Err.Number <> 0 And errflag < 10
Err.Number = 0
Application.Speech.Speak "runtime 2"
ActiveSheet.Pictures.Paste.Select
errflag = errflag + 1
Wend
On Error GoTo 0
Finally! The answer has been found by my companies IT professional. This issue has nothing to do with the actual VBA code chosen, the problem comes when Windows is managing the virtual memory. Office 2016 is a memory hog and the allocated virtual memory was not high enough. The problem completely goes away when on a machine with 16GB of memory the VM settings are manually changed to Custom size, Initial size=24000 and Maximum size=48000. Apparently when moving/resizing graphs and/or pictures there is a lot of memory being used, and the default setting of "Automatically manage paging file size for all drives" is just NOT the correct setting.
Turning off the clipboard function will help to resolve the problem:

Object doesn't support this property/method in an IF statement within a loop

I have a workbook that is used to organize daily orders for many customers into one sheet. The information that is entered into this sheet is then used to generate invoices for the customers.
This workbook has worked without any bugs for several months. However, recently it has started throwing up an error. I do not recall changing any of the code that could have caused this bug.
I have tried to figure this out by myself however, I am running out of ideas, here are a few things that I have found out:
The error ALWAYS pops up on the 7th iteration of the loop below (i.e when x=27).
The error ONLY has a problem with the (CheckBox " & x + 10) checkbox. I have tried running the loop avoiding checking the value of that particular checkbox and it works for all other checkboxes.
I read on another post that it might be a problem with using the dreaded .Select. So I got rid of it from everywhere that it might have been used.
My code
With ThisWorkbook.Worksheets("Invoice")
For x = 21 To 35
If .Shapes("Check Box " & x + 10).ControlFormat.Value = 1 And .Shapes("Check Box " & x + 25).ControlFormat.Value = 1 Then
MsgBox "Please only select one from the options (Return OR Discount)"
Exit Sub
Else
End If
Next x
End With
From what I understand, this error comes up when the object that I am working with does not support a property that I am trying to work with. What I do not understand is why the same lines of code, work "for 90%" of the loop (i.e the same type of object does support that property for 90% of the loop) but then it does not for one object of the same type.
For completeness, the code below is used to re-insert the checkboxes every time the workbook is used. This is done as another process might delete entire rows in the workbook, messing up the functionality of the check-boxes. So this makes sure the checkbox with the right name is in the correct cell:
Sub CheckBoxes()
ThisWorkbook.Worksheets("Invoice").CheckBoxes.Delete
With ThisWorkbook.Worksheets("Invoice")
For x = 5 To 19
Set cb = .CheckBoxes.Add(.Cells(x, "J").Left, .Cells(x, "J").Top, 4, 10)
With cb
.Name = "Check Box " & x + 26
.Caption = ""
.Display3DShading = False
End With
Set cb2 = .CheckBoxes.Add(.Cells(x, "K").Left, .Cells(x, "K").Top, 4, 10)
With cb2
.Name = "Check Box " & x + 41
.Caption = ""
.Display3DShading = False
End With
Next x
End With
End Sub
Any help or guidance in the right direction would be greatly appreciated!

VBA - How do I remove a file from the recent documents list in excel 2007?

The recent documents feature in Office is really useful, but I moved a file to a new directory and now I can't get Excel to stop hitting me with a "can't find this file" notification whenever I open a workbook. The Excel options seem only to control how many of these "recent documents" are displayed and not how many are actually saved. So I;'m wondering if there's a way in VBA to get at the list and remove the offending file.
Try this...
Public Function TestIt()
For i = 1 To Application.RecentFiles.Count - 1
Dim answer As String
answer = MsgBox("Delete " & Application.RecentFiles(i).Name, vbYesNo)
If answer = vbYes Then
answer = MsgBox("Are you sure?", vbYesNo)
If answer = vbYes Then
Application.RecentFiles(i).Delete
End If
End If
Next i
End Function
Not a VBA solution, but open up Regedit and you can remove files from the list at will.
The "File MRU" list is what you're after; for Excel 2007 it's under
HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\File MRU
Adjust the version number accordingly.
Close Excel, delete the offending file's entry from the list found there, and restart.
Facing the same issue I wrote this litte macro, removing all files from the recent file list, which are not accessible:
Public Function CheckRecentFiles() As Integer
Dim i As Integer, c As Integer
For i = Application.RecentFiles.count To 1 Step -1
'Debug.Print Application.RecentFiles(i).name
If Dir(Application.RecentFiles(i).name) = "" Then
Debug.Print "Delete from recent file list: " & Application.RecentFiles(i).name
Application.RecentFiles(i).Delete
c = c + 1
End If
Next i
Debug.Print c & " files removed."
CheckRecentFiles = c
End Function
Try the routine above not as function but as SUB.
And in the second line remove "-1" at its end, because the last entry will not be handled else.
Then the routine will work properly.
Based on #GunnarBernsteinI 's answer,I just added this to my Personal Macro Book. This is going to be super handy to clean up the temp files that I create to answer questions on SO.
Public Sub CleanRecentFiles()
Const ReviewEntry As Boolean = False
Dim f As RecentFile
For Each f In Application.RecentFiles
If Len(Dir(f.Name)) = 0 Then
f.Delete
ElseIf ReviewEntry Then
Debug.Print f.Name
Stop
End If
Next
End Sub
Demo
Open the Recent Workbooks List. Right click quickly and firmly between the icon and text for the document you wish to remove from the list. A dropdown list appears. It is the list which allows you to pin an item to the list. Choose Remove from List. It does work but it can be a bit tricky to time it correctly. If you are too slow it will just try to open the file.

Resources