Colleagues get error 5 in Excel-file with VBA, but same file works fine on my computer? - vba

We have an Excel-file with a large amount of VBA behind it. The Excel-file works just fine on my computer, but so far 3 of my colleagues (non-IT'ers) have gotten this error:
Runtime error 5:
Invalid procedure call or argument
The error is located on this line, and I don't see why it is throwing an error there because it's just a simple Set (and it works perfectly fine on my computer):
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)
We all have the exact same Excel-file. I even sent them my version of the file, in which everything is running fine and no errors happen, but even when they open my version of the file they still get the above error on the above line!
What exactly could cause this? We all have Office 2013 and updates are installed automatically. The problem started about 2 weeks ago with one colleague and since this week I heard from two other colleagues that they have the same problem. One even said it suddenly worked again after he moved to a different desk (which I doubt would have an influence) but shortly after, it started getting the error again.
I have absolutely no idea why they get the error, or what might cause it. Seeing as we now all have the same version and they still get the error, I am thinking it might have something to do with Excel itself but that's just my idea.
Does this sound familiar to anyone here? Or does anyone know what might cause this, and how it can be fixed?
Edit: a while ago I checked on my colleagues their computers to see if the CommandBar was present, and it was. Even then the error still happened.

I suggest you to use a function that checks if there is a CommandBar in your Application like this:
Function IsCommandBarValid(cbName As String) As Boolean
Dim i As Long
IsCommandBarValid = True
For i = 1 To Application.CommandBars.Count
If (Application.CommandBars(i).Name = cbName) Then
Exit Function
End If
Next i
IsCommandBarValid = False
End Function
Now, You can use it to see that your user have that CommandBar in his or her Application, then make it like this:
If (Not IsCommandBarValid("Attributions")) Then
Call Application.CommandBars.Add(Name:="Attributions")
End If
' And after this add your code
Set MyButton = Application.CommandBars("Attributions").Controls.Add(Type:=msoControlButton, Before:=10)

Invalid procedure call or argument (Error 5)
Some part of the call can't be completed. This error has the following causes and solutions:
An argument probably exceeds the range of permitted values. For example, the Sin function can only accept values within a certain range. Positive arguments less than 2,147,483,648 are accepted, while 2,147,483,648 generates this error.
Check the ranges permitted for arguments.
This error can also occur if an attempt is made to call a procedure that isn't valid on the current platform. For example, some procedures may only be valid for Microsoft Windows, or for the Macintosh, and so on.
Check platform-specific information about the procedure.
For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).
MSDN Source Article

Related

MS Access Run Time Error 2465 Can't find the field 'employeeNum' referred to in your expression

Just finished tweaking a query, then started testing the change and then this issue popped up.
In a Module called GLOBALS I declare a number of public variables. One of which is this:
Public employeeNum As Long ' PO entering/modifying data
In form frmSearch I assign this variable. For some reason I am getting a run time that I catch in my Form_Load() procedure.
Here is a pic of when I stop at the breakpoint and hoover over the global variable employeeNum.
Any thoughts on this strange error?
Attempts to fix include the following:
Ran Compact and Repair Database a couple of times and no luck.
Ran /decompile to clean things up. No luck.
Compiled Database to validate no issues and no errors detected.
Changed Public to Global when declaring employeeNum. No luck.
Tried to debug.print employeeNum early in the procedure but I still would get the runtime error on the debug.print line.
Thanks
This problem is caused by two variables being named the same. A global variable and a local variable (employeeNum). The various replies gave me the clue I needed and I used the Object Browser to validate the problem. When I tried to find the object, the Object Browser stated it was hidden which is annoying but I’ll go through all the embedded code, etc. and figure out where it is just so I know.
I renamed the global variable employeeNum to employeeNumber throughout the application and everything works fine.

Explicit "WorkSheet.Range" Fails With “Method or Data Member Not Found”

I have the following code:
Dim newWorksheet As Worksheet
Set newWorksheet = ThisWorkbook.Sheets.add()
Set pivotCache = ThisWorkbook.PivotCaches.create(SourceType:=xlExternal, SourceData:=cnSet)
pivotCache.CreatePivotTable newWorksheet.Range("A1"), newWorksheet.Name
Works great 99% of the time, however today a customer sent me a version of the file that was not working, when I open the file and attempt to compile the code I get the following issue with the last line of code:
Method or Data Member Not Found
In fact you can even see that the property is missing in the autocomplete:
It appears as though it's recognizing the "Worksheet" object as a "chart":
Strange, so I covert the "newWorksheet" over to a variant, compile, works great. Now I change it back, compile, and... it works, issue is gone, code runs great. Very strange, you can even see it in the autocomplete:
(Now it's object type "Worksheet")
I don't understand what would cause this or how to prevent it in the future, the issue only occurs with this one file. The user was not doing anything exceptional with it as far as I can tell. This issue is documented but typically is related to other problems. I don't see any explicit reference to this occurring on a per file basis.
Does anyone have any insight on this one, this is one of those "only in VBA" type of issue?

Run Time Error 1004 When Inserting Rows/Columns

So I'm running into the lovely run time error 1004: Application defined or object defined error.
The bear of it is, half the time the code in VBA runs correctly with no problems, and the other half it falls on its face. The third line ("Mgmt.List.....") is run in a loop normally, but it has no problems messing up on me in this simple macro.
Sub whyyyy()
Dim Mgmt As Worksheet
Set Mgmt = Sheets("Mgmt MarginAnalysis")
Mgmt.ListObjects("Table4").ListRows.Add (3)
End Sub
The line
Mgmt.ListObjects("Table4").ListRows.Add (3)
is extra special, as when it's run in the loop, it will sometimes work the first time, but fail the second time. Or it will just fail outright.
Don't really know what makes it work and what doesn't. This just started happening, and the worksheet/table combination seems to only happen to tables I've just inserted recently (not by using VBA).
Are you sure, the Sheets("Mgmt Margin Analysis") has table named "Table4". If you are creating the table on runtime, chances are that, it will not be assigned the same name ("Table4") everytime it is created.
Please ensure you assign this name in your code, and do not let Excel to choose the name for you.

Strange / unstable behaviour on CreatePivotTable()

I am writing VBA code to automate some processes in Excel and I am encountering a very strange behavior for which I have not been able to find documentation / help.
I have a procedure MAJ_GF that first executes function GF.Update, checks the result, and then launches procedure GF.Build (which basically takes the data obtained by GF.Update from different worksheets and does a bunch of stuff with it).
At some point, this "bunch of stuff" requires using a pivot table, so GF.Build contains the following line:
Set pvt = ThisWorkbook.PivotCaches.Create(xlDatabase, _
"'source_GF'!R1C1:R" & j & "C" & k).CreatePivotTable("'TCD_GF'!R4C1", "GFTCD1")
The strange behavior is this:
when I run MAJ_GF, VBA properly executes GF.Update, then launches GF.Build, and stops at the line described above complaining "Bad argument or procedure call"
when I manually run GF.Update, then manually run GF.Build, everything goes smoothly and GF.Build does what it has to do from beginning to end with no errors
even stranger, when I set a break-point on the incriminated line, run MAJ_GF then VBA pauses on the line as expected, and when I say "Continue"... it just continues smoothly and with no errors !
I turned this around and around and around, double-checked the value of every variable, and this just makes no sense.
Ideas anybody?
Few ideas come to my mind:
There's still some update going on in the background. Try DoEvents and Application.Wait before the line you mentiond
Also check, if any data connections are able to update in the background - if so disable the background refresh
Very rarely (usually in older version and when involving Charts), unhiding the Excel window (in case you used Application.Visible = False and enabling ScreenUpdating helped..
Are you using any "exotic" references/add-ins? Disable them and see if the problem persists.
Try restarting your machine
Not that I'm too optimistic that either will solve your problem - but give it a try! Best of luck!

vb.net: Jump out of exceptioned function

I am just reworking my VB6 in .NET.
I have a function that is called NonNullString(byval uAny As Object) As String
In VB6 I worked with a sqlite wrapper, and a recordset's member could be accessed by using
Dim sString$
sString = r("somefield")
(without ".Value")
I have really many of these fields, and I changed most of them to ".Value", but for some I forgot it.
An exception is therefore raised in the function NoNullString, and I am looking for a way to quickly jump out of the function in order to see what the caller was and improve the code.
F5 does not do the job.
Does anybody have any ideas?
Thank you!
Press CTRL+L to see call stack. From there you can navigate through the stack.
You can then use Set Next Statement (CTRL+F9) on the End Function of your errored function. Two times F10 to complete execution of this function. Repeat this step till you are in the scope where you think the error originated. Then, if you are on x86 (so you have Edit&Continue available), fix your code, and drag your currently executed line to the moment when this fix would occur. And then try running your function again.
Unfortunately, you cannot Set Next Statement outside of the current block function/sub, which I was going to suggest originally.