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

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?

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.

Answer this and you're a true genius: Error removing then re-adding missing reference

Dim brokenRef As VBIDE.Reference
Dim refrnc As VBIDE.Reference
For Each refrnc In vbProj.References
If refrnc.GUID = "{C94A4194-E621-404A-8E20-447E4D415ABD}" Then
found = True
If refrnc.IsBroken Then
found = False
Set brokenRef = refrnc
End If
End If
Next
If Not found Then
If Not brokenRef Is Nothing Then vbProj.References.Remove brokenRef 'THISLINE'
vbProj.References.AddFromFile ThisWorkbook.Path & "NuancePDF.tlb"
End If
I am looking for a missing reference and then attempting to add it. I got some error when adding it, so I figured it must be because the reference is there, it is just broken, so I try to remove the reference and I get the error on that line (this line). The error description is given in the image. As background, I want to automate the process of adding references since some machines cannot find them. This runs in the workbook_open sub so it seems to run this sub before compiling the rest, which relies on the references and so for some reason I am not getting a compile error because of a missing reference, but if it works idc.
I read online about running regedit in the cmd, and then deleting some stuff. However I do not fully understand what they were doing and I am working on company computers and so I do not want to risk messing anything up.
Sort of solved, see the comments. The link posted by Tim Williams gives a good idea which is to start without the reference and then add it, which I guess is maybe similar to late binding in a way. As for getting what I wanted to explicitly work it seems that you have to register it via regsvr32 in the cmd, which is not working for me.

Second CopyPicture in VBA fails with "Automation error"

So I am copying and pasting a bunch of data to another spreadsheet. I thought this last bit was going to be easy since I've done this a bunch of times in this script.
Except it fails. Here's part of my code:
ProdCK.Worksheets("CK week").Range("A11:AY28").CopyPicture Appearance:=xlScreen
Worksheets("Prod.CK").Paste Destination:=Worksheets("Prod.CK").Range("A1")
ProdCK.Worksheets("CK week").Range("I131:BO148").CopyPicture Appearance:=xlScreen
Worksheets("Prod.CK").Paste Destination:=Worksheets("Prod.CK").Range("A22")
The first one works fine, but then the second one crashes on the CopyPicture operation. I've checked that a range is actually present by first storing that in a range, checking if the data is there in the "watch" screen and then calling CopyPicture on that, but nonetheless is still gives a
424 error Object required
Can anyone shine a light on this for me?
UPDATE:
The error is actually
-2147417851 Automation error The server threw an exception
My error handling code had a bug which turned the automation error into the Object required error. What is also interesting is that the error does not happen when I use a visible Excel.Application in this Sub. The only difference between my visible and invisible settings are Visible = True and ScreenUpdating = True.
I haven't been able to figure out why but currently I just set App.Visible = True before these two statements and set it back to App.Visible = False afterwards. This gets things working.
I really dislike it though.

Error -2147417848 (&H80010108): The object invoked has disconnected from its clients

I have a huge vba project with 10 complex userforms and with a lot of other functions and subs.
Then I have debug window open everything works perfectly and the next time I open the file it works also well, but if I open it 2nd or 3rd time (without opening debug window) I receive the following error:
Error -2147417848 (&H80010108): The object invoked has disconnected from its clients.
After few hours I found the source of this error, once I exclude the following line it works perfectly:
ws.Cells(nr + 5, 7) = Me.tb_COT_MP_SM.Value
This code transfers value from one of the textboxes of main big userform to cell. I read the article related to this error and tried to Dim this textbox as an MSForms object but it didn't help..
Thank you in advance for any support.
Something somewhere in your code has either unloaded or destroyed or not created either ws or me (probably ws)
I think this will work (but I don't know Why):
ws.Cells(nr + 5, 7).Value = CStr(Me.tb_COT_MP_SM.Value)

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

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