Calling macro with Application.Run gives an error 1004 - vba

I can't find the cause of the error 1004 returned by the Application.Run command.
Sub Called()
MsgBox "Yes"
End Sub
Sub Caller()
Call Called ' works
subName = "Called"
Application.Run Application.ActiveWorkbook.Name & "!" & subName ' error 1004
End Sub
Error description (in French):
---------------------------
Erreur d'exécution '1004':
Erreur définie par l'application ou par l'objet
---------------------------
Every solution I found after googling this issue leads to the same error. The Caller and Called subs are in the same "Sheet1" module.
I'm a bit lost. I wonder if the issue is related to XL security? Or something missing in References?

Move your code out of the Sheet1 module and into a normal module.

I had the same problem.
I fixed it by moving the second macro to the same module.
Looks like a memory issue.

Remark: I had the Problem with Error 1004 "sub or function not found" error after Application.Run macro, arg
But macro without arg worked.
I found that in the target Workbook the way of declaration of the parameter arg caused a difference:
macro(arg$) -> caused error
macro(arg As String) -> worked (for a moment)
Later same errors again ...
Very strange behaviour of Excel VBA Editor: In the line with the Application.Run command VBE had a offset of 2 characters from the cursor, which means when I set the cursor after the n of Run and pressed backspace the R of Run was deleted. Same offset when I typed characters.
That issue was moving when I copy-paste that line.
I found out that the errors occured in the lines which had that issue.
I solved it by typing the line new (regarding that the new line didn't have that issue) and deleting the old line, which had that issue.

Related

How to ignore a Syntax Error without correcting the code?

This is my first question at stackoverflow and I hope you can help me.
On a big work project, I have to import some VBA codes (that are written on *.txt files), test them and run them if they're working properly.
The problem is that some of that codes have Syntax errors like the one next:
Sub ExampleScript()
Dim variable as Double
MsgBox variable +
End sub
Sure, I know that the code line MsgBox variable + is going to originate an error and pop up the warning
How do I get rid of the warning? Just by ignoring it? I tried already commands like On Error Resume Next or On Error GoTo... but simply can't fix the problem
Sub ExampleScript()
On Error Resume Next
Dim variable as Double
MsgBox variable +
End sub
or
Sub ExampleScript()
On Error GoTo Debugs
Dim variable as Double
MsgBox variable +
Debugs:
End sub
Note please that my question is about simply ignore the errors (by moving forward or ending the sub), not correct them. There are a lot of them on the *.txt files and I don't pretend to correct them all. I just want to run the codes that work properly. Thank you all for your help.

Adding Control to worksheet causes "Can't execute code in break mode"

I'm trying to add a checkbox to my worksheet using code:
Sub DropCheckboxOnSheet()
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
End Sub
When I run this code, I do get a checkbox added to the worksheet, however I also receive the message that Excel "Can't execute code in break mode". I get that my code adds an object to the object model and that this is why it breaks, but it also offeres me the option of continuing.
How do I tell VBA to continue after adding the object?
What i've tried:
Adding DisplayAlerts=False does not work.
MSDN offers no help either:https://msdn.microsoft.com/en-us/library/office/gg264133.aspx
I am not sure this is the safest solution but you will not get a error since adding "End" kills all macros. The only time i ever really use it is on progressbars. I would dig harder on the real issue using "On error resume next" is a bandaid. If you do use it make sure to restore your default error handling with "On error goto 0".
Sub DropCheckboxOnSheet()
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
End
End Sub
e.g.
Sub DropCheckboxOnSheet()
On error resume next
ActiveSheet.OLEObjects.Add ClassType:="Forms.CheckBox.1"
On error goto 0
End Sub
Changing the name of the sub might fix your issue. If you have the same sub in another open workbook it could be throwing the error.

VBA Error 457 when using Collection to create a unique list

BACKGROUND:
I am trying to identify how many unique time periods I have from a list of dates that have. Elsewhere, I have seen a method which utilizes collections and error trapping (right term? I mean "On Error Resume Next" in any case) to build the collection with unique values. I have even used this structure successfully in other code that I have written, but in my current circumstance, I am getting an "Error 457: This key is already associated with an element of this collection." Thinking I was using the collection incorrectly, I opened up some older code I wrote 6 months ago (on a different computer for a different company) which uses the same structure and was known to WORK. This older code broke on the same identical error, which it previously did not do. Here is the sample of my work-in-progress code:
Dim rng as range
Dim TimePeriod as Collection
Set TimePeriod = New Collection
For Each rng In Range("I2:I6")
On Error Resume Next
TimePeriod.Add rng.Value, CStr(rng.Value) 'This is where the code breaks
On Error GoTo 0
Next rng
QUESTION:
I'm wondering if there is a setting or a reference library that I am somehow missing that is causing both pieces of code to break, or how to determine that, since both codes are functionally identical, and the previously tested satisfactory code breaks like my work-in-progress. I expected the "On Error Resume Next" to force the loop to pass over the error. Any suggestions?
--Update--
Sample data in range("I2:I6") as follows:
1/21/15
1/21/15
1/21/15
1/23/15
1/27/15
Your code works properly on my Excel 2007, although I would rewrite it to enclose the entire loop within the on error resume next for efficiency.
I suspect you are seeing the errors now because of a mis-set macro option error break.
Check Tools/Options that you have not selected to Break on All Errors
Try getting rid of the On Error Goto 0 line. Take a look at this:
Difference between 'on error goto 0' and 'on error goto -1' -- VBA
It comes from Visual Basic 6, but works pretty much the same in VBA, it appears. Should work if you keep the On Error Resume Next line but eliminate the On Error Goto 0 line.

Run time error '1004' Unable to get the Match propertyof the WorksheetFunction class

In my macro, I have the following code :
i = Application.WorksheetFunction.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)
where 'str_accrual' is a string captured earlier to this line and the Range selected is in a single row say from "A1" to "BH1" and the result will be a number which is the position of that string in that range selected.
When I run the macro, I get the error:
Run time error '1004' Unable to get the Match propertyof the WorksheetFunction class
But when I run the macro line by line using (F8) key, I don't get this error but when I run the macro continuously I get the error. Again, if the abort the macro and run it again the error doesn't appear.
I tried several times. It seems that if there is no match, the expression will prompt this error
if you want to catch the error, use Application.Match instead
Then you can wrap it with isError
tons of posts on this error but no solution as far as I read the posts. It seems that for various worksheet functions to work, the worksheet must be active/visible. (That's at least my latest finding after my Match() was working randomly for spurious reasons.)
I hoped the mystery was solved, though activating worksheets for this kind of lookup action was a pain and costs a few CPU cycles.
So I played around with syntax variations and it turned out that the code started to work after I removed the underscore line breaks, regardless of the worksheet being displayed. <- well, for some reason I still had to activate the worksheet :-(
'does not work
'Set oCllHeader = ActiveWorkbook.Worksheets("Auswertung").Cells(oCllSpielID.Row, _
Application.Match( _
strValue, _
ActiveWorkbook.Worksheets("Auswertung").Range( _
oCllSpielID, _
ActiveWorkbook.Worksheets("Auswertung").Cells(oCllSpielID.Row, lastUsedCellInRow(oCllSpielID).Column)), _
0))
'does work (removed the line breaks with underscore for readibility) <- this syntax stopped working later, no way around activating the worksheet :-(
Set oCllHeader = ActiveWorkbook.Worksheets("Auswertung").Cells(oCllSpielID.Row, Application.Match(strValue, ActiveWorkbook.Worksheets("Auswertung").Range(oCllSpielID, ActiveWorkbook.Worksheets("Auswertung").Cells(oCllSpielID.Row, lastUsedCellInRow(oCllSpielID).Column)), 0))
In the end I am fretting running into more realizations of this mystery and spending lots of time again.
cheers
I was getting this error intermittently. Turns out, it happened when I had a different worksheet active.
As the docs for Range say,
When it's used without an object qualifier (an object to the left of the period), the Range property returns a range on the active sheet.
So, to fix the error you add a qualifier:
Sheet1.Range
I had this issue using a third-party generated xls file that the program was pulling from. When I changed the export from the third-party program to xls (data only) it resolved my issue. So for some of you, maybe there is an issue with pulling data from a cell that isn't just a clean value.
I apologize if my nomenclature isn't great, just a novice to this.
That is what you get if MATCH fails to find the value.
Try this instead:
If Not IsError(Application.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)) Then
i = Application.Match(str_accrual, Range(Selection, Selection.End(xlToRight)), 0)
Else
'do something if no match is found
End If
Update
Here is better code that does not rely on Selection except as a means of user-input for defining the range to be searched.
Sub Test()
Dim str_accrual As String
Dim rngToSearch As Range
str_accrual = InputBox("Search for?")
Set rngToSearch = Range(Selection, Selection.End(xlToRight))
If Not IsError(Application.Match(str_accrual, rngToSearch, 0)) Then
i = Application.Match(str_accrual, rngToSearch, 0)
MsgBox i
Else
MsgBox "no match is found in range(" & rngToSearch.Address & ")."
End If
End Sub
I used "If Not IsError" and the error kept showing. To prevent the error, add the following line as well:
On Local Error Resume Next
when nothing is found, Match returns data type Error, which is different from a number. You may want to try this.
dim result variant
result = Application.Match(....)
if Iserror(result)
then not found
else do your normal thing

VB Error in Excel

I am encountering an error in MS Excel when the following VB code is executed and the "Tracking Changes" feature is turned on:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 Then
If IsEmpty(Target) Then
Target.Offset(0, 1).Value = Empty
Else
Target.Offset(0, 5).Value = Now()
End If
End If
End Sub
It seems that I can enter data into the first 2 rows just fine but once I start populating data in the 3rd row and onwards, I get a error stating "run-time error '1004' application-defined or object-defined error".
I get the error from row 2 onwards with this code and track changes turned on. The code however seems to do what it is supposed to do regardless of the error, so I would advise an On Error Resume Next to work around.
This doesn't address any underlying problem with the change tracking and your changes may not be being tracked.
Change Tracking forces the workbook to become Shared, which disables many features (e.g. you can't access VBA with Change Tracking enabled).
I'm not entirely sure why this error is being thrown, but a workaround to avoid seeing this message would be to Resume Next when an error occurs:
On Error Resume Next
Simply add this line of code about your first If block (If Target.Column = 1 Then). Please note that ideally you should want to discern the source of this issue and that this solution is a workaround, which might not be good practice.