Run-time error VBA upon changing ":" to ";" - vba

Im trying to dynamically add a formula in an Excel sheet using VBA. Something really odd happens. When dynamically creating a formula by using "&" to link together the various components of a string, its gives a Run-time error '1004': Application-defined or object defined error.
This is working (but produces the wrong formula):
Worksheets("Sheet1").Cells(row, 7).Value = "=BDP(f" & row & ":Security Name)"
This is not working (produces the above mentioned error):
Worksheets("Sheet1").Cells(row, 7).Value = "=BDP(f" & row & ";Security Name)"
Note that the ONLY difference is the ":" in front of Security Name became a ";".
Any idea why this is producing this error?
Also, "Security Name" should also be between quotation marks, but when I double up the quotation marks, or use & Chr(34) I get the same error again.
What I am looking for is a formula to be added to the cell which looks like this =BDP(F4:"Security Name")
Your help is appreciated!

If you want a ; in the actual formula you need to use a , in the String you are using.
Also If you write this "" inside the string it will result in this in your string "
So this in you VBA:
.Formula = "=BDP(f" & Row & ",""Security Name"")"
will result in this in you actual cell:
=BDP(F5;"Security Name") (For me the Row was 5)
(You also can set the .Value property instead, but since you´re setting a formula i´d suggest using the .Formula)
Edit:
The method I used, mentioned in the comments:
Sub test()
BBCode = "XS0357495513 Corp"
Sheets(1).Range("A1").Formula = "=BDP(""" & BBCode & """,""Security Name"")"
'Range("A1") is like Cells(1, 1)
End Sub

Related

.range.Formula of a Concatenate() to a server path

I'm trying to fill in a cell with the =CONCATENATE() formula which gave me errors,
strformula = "=CONCATENATE("\\SERVER\PATH\DIR\";$A5;".pdf")"
I tried to fix this with the incode concatenate & method
but I somehow have an error with the Sub
"Method or data member not found"
Any ideas?
Sub Fillcells()
Dim pthstr As String
Dim strformula As String
With sheetNAME
pthstr = "\\SERVER\PATH\DIR"
strformula = "=CONCATENATE(" & pthstr & ";$A5;\" \ .pdf \ ")"
.Range("N5:N5").Formula = strformula
'.Range("N5", "N" & GetLastRow(sheetNAME)).FillDown
End With
End Sub
You have a couple issues with how you're handling your string text. I think this will work as a formula for you or at least allow you to avoid errors.
strformula = "=CONCATENATE(""" & pthstr & """,$A5,""\ .pdf \ "")"
Note that for quotes within the formula, you have to use 2x double quotes. And if this is the end of your quote, you;ll have THREE sets, followed by an & sign.
However, I'm not sure concatenate is really what you want. You might just consider typing ="\\SERVER\PATH\DIR\"&A5&".PDF"
There's almost no reason one should ever use Concatenate when compared to alternative functions such as textjoin or Concat. This article explains. Good luck.

Not able to use a variable inside Find command in VBA

I have a string s which contains "subham$"
Now using excel's built in command find i like to know the position of the dollar symbol,there might be other ways but i like to use find in vba code and use a variable inside it
Sub testfind()
Dim s As String
s = "subham$"
Sheets("Sheet1").Select
Range("A1").Select
ActiveCell.FormulaR1C1 = "=FIND(""$""," & s & ")"
End Sub
I am getting the error Application defined or object defined error,
what am I doing wrong?
I can't actually fathom what you're trying to do with the code, or why you'd be doing it, but in order to solve your problem:
You're not wrapping the string of s in quotes. Try:
Range("A1").FormulaR1C1 = "=FIND(""$"",""" & s & """)"

Extracting hyperlink from a range and writing them on another range

I'm new to this site, but I have already found some nice advice on how to solve problems in VBA. Now I'm here to ask help on a sub that gives me problems with Hyperlinks.
In particular, my problem is similar to the one described in this topic:
Excel VBA Get hyperlink address of specific cell
I have a worksheet full of hyperlink, but I need to extract only the addresses present in the "H" column, starting from "H6" and writing them into the "N" column, starting from "N6".
I put down this code:
Sub EstraiIndirizzoPut()
Dim IndirizzoInternet As Hyperlink
Dim ISINs As String
i = 6
For Each IndirizzoInternet In Sheets("XXX").Range("H" & i).Hyperlinks
IndirizzoInternet.Range.Offset(0, 6).Value = IndirizzoInternet.Address
ISINs = Mid(IndirizzoInternet.Address, 78, 12)
Range("N" & i).Value = ISINs
i = i + 1
Next
End Sub
It works fine only for the first "H6" cell, but at the "Next" point, when it should read the "H7" cell, it goes instead to "End Sub", terminating the routine, altough the "H7" cell, as well many others down the column, are filled with hyperlinks (it gives me "Nothing" value).
Could you please suggest me where I get this wrong? Many thanks.
Your loop isnt set up correctly. Try it like this instead:
For i = 6 to 100
Set IndirizzoInternet = Sheets("XXX").Range("H" & i).Hyperlinks
IndirizzoInternet.Range.Offset(0, 6).Value = IndirizzoInternet.Address
ISINs = Mid(IndirizzoInternet.Address, 78, 12)
Range("N" & i).Value = ISINs
Next
How do you know when to stop the loop? Is it a preset number of rows? If it not, you will want to have something determine the last row to process and replace the 100 with that variable.

Why is cell "A1" being used in this GetValue function in VBA?

I'm using this function to retrieve a value from a closed workbook. In this 8th line of this code, I don't understand why "A1" is being used. What exactly is happening in that entire 8th line? I'm confused by the xlR1C1 argument as well.
Private Function GetValue(path, file, sheet, ref)
Dim arg As String
If Right(path, 1) <> "\" Then path = path & "\"
If Dir(path & file) = "" Then
GetValue = "File Not Found"
Exit Function
End If
arg = "'" & path & "[" & file & "]" & sheet & "'!" & _
Range(ref).Range("A1").Address(, , xlR1C1)
GetValue = ExecuteExcel4Macro(arg)
End Function
Range().Range() Documentation here:
When applied to a Range object, the property is relative to the Range object. For example, if the selection is cell C3, then Selection.Range("B1") returns cell D3 because it’s relative to the Range object returned by the Selection property. On the other hand, the code ActiveSheet.Range("B1") always returns cell B1.
The code is using that second Range("A1") to ensure that if you have a ref range larger than one cell, it only returns the top left cell of that range. Also it would appear your other Sub called ExecuteExcel4Macro() requires an R1C1 type cell reference so the address is being converted into that type for passing the arg string into the Sub.
xlR1C1 is a reference style which is used to specify how formulas work. When using this style, your formulas will work and look very differently than you expect. The R1C1 specification, basically means the cells are referred to differently using row & column ordinals instead of letter names. For example, when using xlR1C1, you would access cell B2 by using =R2C2 (row2, column 2). Another Example, cell C10 could be referred to as =R10C3
As to whats happening on line 8... you are constructing a cell reference that looks like this: (Note that your cell reference will be different because it has a file path in it)
='[Myfilename.xlsx]Sheet1'!R1C1
You can use the debugger to view the string contained in the arg variable.
Adding Range("A1") to the code doesn't appear to do anything at all. Typing this into the immediate window results in some... unexpected results.
?Range("B3").Range("A1").Address
$B$3
Now, I would have expected that to return $A$1, but it seems that this part of the function will return the address of Range(ref).
Now, calling Range.Address with the ReferenceStyle argument would produce these results.
?Range("B3").Range("A1").Address(,,xlR1C1)
R3C2

Excel 2010 - Error: Cannot run the macro SelectCell using .onAction

I have been looking all over the internet for a solution to this problem but for some reason I can never find anything directly related to using .onAction with selecting a specific cell.
I am using an answer to another question as a reference:
https://stackoverflow.com/a/18199035
In the section where it is looping through shapes, the script assigns an .onAction event to each shape. Whenever this is run in Excel 2010 I get the error:
Cannot run the macro "SelectCell "Sheet 1","$C$10"".
The macro may not be available in this workbook or all macros may be disabled.
I am new to VBA scripting for excel so I have no idea if it is the formatting, but I know it is related to this line.
.OnAction = "'SelectCell """ & ws.Name & """,""" & cll.Address & """'"
I created a sub-procedure for SelectCell to display the values being sent as a debug. Same error.
I tried having excel allow all macros and disable all macros but it had no effect on the error.
If anyone has any idea of where I am going wrong or any resources I can use to further educate myself, it would be greatly appreciated.
This (both subs in a regular module) works for me.
Sub SelectCell(sht As String, rng As String)
ThisWorkbook.Sheets(sht).Range(rng).Select
End Sub
Sub Assign()
ActiveSheet.Shapes(1).OnAction = "'SelectCell """ & _
Selection.Parent.Name & """, """ & _
Selection.Address() & """'"
End Sub
If SelectCell is in a sheet code module, then you need to include the sheet code name:
Sub Assign()
ActiveSheet.Shapes(1).OnAction = "'Sheet1.SelectCell """ & _
Selection.Parent.Name & """, """ & _
Selection.Address() & """'"
End Sub
If I'm reading this right, "SelectCell" is a macro name, and you're passing in "Sheet 1","$C$10" as parameters (as strings). Whenever you make a call on the right side of an assignment operator (equals sign) the parameters need to be passed in with parenthases. for example,
.OnAction = "SelectCell "Sheet 1","$C$10""
is wrong, and
.OnAction = "SelectCell ("Sheet 1","$C$10")"
is right.
Try that, but there's not much I can go off without much code. You could also try to use the fully qualified macro name, in case the Macro is in another module.