Error 91 each time I run an Application.Caller Macro - excel

I am getting an error when I use Application.Caller in Excel VBA.
I have a piece of code attached to a button/shape that I want to use to print a timestamp in the cell next to the button.
I need to use this button over and over so don't want to reference actual ranges of where to put the timestamp, so I thought this code would work.
However, each time the macro gets to the 2nd line, i.e. the Application.Caller row, it gives me an error:
ERROR 91 : 'Object variable or With block variable not set'
I would greatly appreciate if someone could assist with why this is happening as I can't fix it.
Thank you
I have tried activesheet.buttons as well as activesheet.shapes but neither have worked.
Dim x As Object
Set x = ActiveSheet.Shapes(Application.CALLER)
With x.TopLeftCell
Range(Cells(x.Row, x.Column), Cells(x.Row, x.Column)).Offset(0, 1).Select
End With

Related

Excel VBA - 1004 Error code when removing picture from excel sheet

I have a made a VBA based complaints form that keeps track of a number of details about a complain. One feature that i added lately is that it is now possible to add a picture to the report/form.
When the form is submitted all the filled cells are copied to a seperate spreadsheet and the form itself needs to wiped and the pictures need to be removed so that a new report can be filled if neccessary. It is like a reset.
In order to remove the pictures I copied the below piece of script. About 90% of the time it works perfectly fine and the images are removed and the form is back to it's original form however every once in a while for unknown reason i get a Error 1004 "Application Defined or Object Defined Error". When i receive this error i am unable to remove the pictures and need to restart the excel file.
Within VBA highlighted in yellow it says that the k.TopLeftCell is the cause of it.
With Sheets("Klachtformulier")
Dim k As Shape, rng As Range
Set rng = Sheets("Klachtformulier").Range("A43:H47")
For Each k In ActiveSheet.Shapes
If Intersect(k.TopLeftCell, rng) Is Nothing Then
Else
k.Delete
End If
Next k
I tried to change activesheets to the sheet("name"), tried change the range, tried to change shape dim into a picture dim and tried to exit the for after one loop however all without succes so far. Most of the time these changes cause the pictures from not being removed anymore.
Any idea what could be the cause or the solution?
I think the problem is likely that you are deleting members of a collection (the Shapes collection) while iterating over it using 'For ... Each'. When deleting, you should use 'For ... Next' and loop from the end of the collection to the start:
Dim k As Shape, rng As Range
Dim i As Long
Set rng = Sheets("Klachtformulier").Range("A43:H47")
For i = ActiveSheet.Shapes.Count To 1 Step -1
Set k = ActiveSheet.Shapes.Item(i)
If Not Intersect(k.TopLeftCell, rng) Is Nothing Then
k.Delete
End If
Next i
I tweaked the logic of your If statement and removed the With line as (in the code you posted) it isn't doing anything useful.

VBA Run-time error '424 on simple IF statement

I have the following short VBA code, what I want is for it to check a cell, B5 for whether it is a Y or a N, and if Y - go to one macro, and if N - go to another macro.
Sub LoadSetup()
Dim Master As Workbook
Set SingleLoad = Setup.Range("B5")
If SingleLoad = "Y" Then
Call LatestLoad
Else
Call TimeSeriesLoad
End If
End Sub
I am getting a Run-rime error '424' Object Required on this line. Any ideas please what could be causing this?
Set SingleLoad = Setup.Range("B5")

Excel VBA run-time error 1004 which I can't identify

This is my code:
Sub VerticalLoop()
Dim i As Integer
Range("VerticalLoop").Activate
i = 1
Do While i <= Range("VerticalLoop").Rows.Count
ActiveCell.Value = i
ActiveCell.Offset(1, 0).Activate
i = i + 1
Loop
End Sub
For some reason I can't run this. When I press F8, the error pops up when I reach the line "i=1", so I don't know exactly what I did wrong.
This code will fail if the Sheet containing the Defined Name has not been activated first!
The error is in the line
Range("VerticalLoop").Activate
The error is probably a typo in the name of the range. Are you sure it isn't supposed to be "Vertical_Loop" (with the underscore)?
On the other hand, Gary's Student is correct that a '1004' error could be triggered by trying to activate a range in an inactive sheet. The error message itself (and not just the error number) can tell you more about the exact source of the error. Alternatively, you could break that line into two lines like thus:
Dim VLoop As Range
Set VLoop = Range("VericalLoop")
VLoop.Activate
If the Set VLoop line throws the error then the problem is with your name. If the second line throws the error then then problem is with what is active.
As an added benefit -- once you debug this error you now have a useful range variable allowing you to use expressions like VLoop.Rows.Count

Find command giving error: "Run-time Error '91': Object variable or With block variable not set"

Trying to activate the cell in my column A which says "Generator loading".
I have been trying the 'With' and 'End With' commands and other suggested formats posted on the net. However, I keep getting the same error- Run-time Error 91.
From my various trials I am very sure that there is something wrong within the "Find" command, but I cannot figure out what... I have been filling it using the format on the MSDN page.
Do you have any suggestions?
Dim findstring As String
findstring = "Generator loading"
Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1)).Activate
The error commonly arises if the result of the .Find method is Nothing, because you can't do Nothing.Activate
First, you have to check for Nothingness
Dim rng as Range
Set rng = Sheets("Summary").Columns(1).Find(What:=findstring, After:=Cells(9,1))
If rng Is Nothing Then
MsgBox findString & " not found!!", vbCritical
Exit Sub
End If
rng.Activate
'the rest of your code goes here...
I don't like to work with columns, so instead of it, I used a range.
For me to work, I just replaced the reference of the sheet
Sheets(1).Range("A:A").Find(What:=findstring, After:=Cells(9, 1)).Activate
See if this works. If not, you could send me the sheet if you want :)

'Method Range of object _Global failed'

I've seen a few posts on this problem. I know what's causing it but I'm trying to figure a way to stop it from appearing.
What it is there is one excel file called Main which I have to keep open and it updates automatically using every 20 mins using:
Application.OnTime
In the VBA formula there is a call for
Loc = Range("location").Value
So if I have another excel file open I get the range error when it auto-updates.
Is there something I can put in before to check that the value isn't right and ignore that one error? I've tried a few things and looked around but nothing really works.
Thanks for any help.
I know what the problem is, I'm trying to find a way to block or check for an empty value while in another file.... Demas 5 mins ago
I agree with Tim here. If you can fully qualify your range then do that. Else read on :)
Is this what you want?
Dim rng As Range
'
'~~> Rest of the code
'
On Error Resume Next
Set rng = Range("location")
On Error GoTo 0
If Not rng Is Nothing Then
Loc = Range("location").Value
'
'~~> Rest of the code
'
End If

Resources