Strange error with simple VBA function - vba

I have the following function:
Sub numbers2()
barrier1 = Sheets("screen_3_TE_NONSCLIENT").Cells(8, 5)
MsgBox (barrier1)
End sub
When I try to run it I get the following error however (translated from Dutch).
Error 13: types do not match
Any thoughts what goes wrong here?

Rory suggested rightly, it could be due to error value in the cell such as #N/A.
To handle error value in cells, try convert them to string
msgbox (Cstr(barrier1))
You will receive the error in text message such as below for #N/A error.

barrier1 is a Range object, you should use:
MsgBox (barrier1.Value)

Related

Picture.Insert returns Run-time error '1004'

I am trying to show pictures in excel with the use of a macro.
I have a list of web URLs and I want to see the pictures contained in those links.
Online I found some solutions that suggest to use the Picture.Insert method, but I am not able to make it work.
So far this is what I got:
Sub InstallPictures()
Dim i As Long, v As String
For i = 2 To 1322
v = Cells(i, "F").Value
Worksheets("Sheet1").Pictures.Insert (v)
Next i
End Sub
The URL I am using as test is the folllowing: http://cache.lego.com/media/bricks/5/1/4667591.jpg
The error message I receive is the
Run-time error '1004' Application-defined or object-defined error
Any advice on what I did wrong?
Thanks
Try to make your code as simpler as possible. Then test from there:
Sub TestMe()
Dim v As String
v = "http://cache.lego.com/media/bricks/5/1/4667591.jpg"
Worksheets(1).Pictures.Insert (v)
End Sub
Does this work for you? If yes, then try to write the v in the corresponding cell.
Try again. Does it work? If yes, then try to make the loop!
Try again. Does it work? Insert the Sheet1 instead of Worksheets(1) and so on.

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.

Calling macro with Application.Run gives an error 1004

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.

VBA : Error using Range.Formula

I'm using this formula
ThisWorkbook.Sheets("Overview").Range(formrange).Formula = "=IF(OR(ISBLANK(B2);WEEKDAY(DATE($B$38;$B$37;B2);2)>5;DAY(EOMONTH(DATE($B$38;$B$37;B$3);0))<B2);0;IF(C2=""Y"";0,5;1))"
And am having the following error
Run-time error "1004"
Application-denied or object-defined error
Would you guys have an idea what that is?
The Range.Formula property needs a formula string in the same format it would be entered into a cell on a computer with US regional settings. So for this example you need:
ThisWorkbook.Sheets("Overview").Range(formrange).Formula = "=IF(OR(ISBLANK(B2),WEEKDAY(DATE($B$38,$B$37,B2),2)>5,DAY(EOMONTH(DATE($B$38,$B$37,B$3),0))<B2),0,IF(C2=""Y"",0.5,1))"

Keep getting "Run-time error '1004': Select method of Worksheet class failed when trying to select a worksheet

What I really need is a way to select multiple worksheets in a workbook based on index and/or location in the tab lineup. However, even the simplest select-by-index code is returning this frustrating error.
Here's the simplest code that still returns the error:
Sub testselect()
Worksheets(1).Select
End Sub
And here's the gist of what I'd like to make work, but probably won't even after I solve this frustrating error:
Sub testselect2()
Worksheets(Array(Worksheets(1), Worksheets(2), Worksheets(3), Worksheets(4))).Select
End Sub
You're entering the Array wrong:
Sub testselect2()
Sheets(Array(1, 2, 3, 4)).Select
End Sub