Not able to use a variable inside Find command in VBA - 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 & """)"

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.

Excel File Open Macro

So I'm trying to create a Macro That will update a cell when the file is opened. I'm getting the 424 Error and so I tried to do a better job defining my code/object but It still wasn't succesfull. I think I'm missing/overlooking something really easy but I can't figure it out and this is my first project so I'm trying to learn and gain a better understanding then just googling a segment of code that will work.
Private Sub Auto_Open()
Dim Try1 As String
Try1 = ActivateSheet.Cells(3, 2).Select
Tryl = "-"
' My first attempt is shown below
'
'Sheets("Current Calc").Activate
'ActivateSheet.Cells(3, 2).Value = "-"
End Sub
You've got a typo in your commented code
What you have...
Sheets("Current Calc").Activate
ActivateSheet.Cells(3, 2).Value = "-"
What it should be...
Sheets("Current Calc").Activate
ActiveSheet.Cells(3, 2).Value = "-"
Also, I should mention you should avoid using .Activate and .Select unless necessary. With that being said, I'd suggest the below code instead...
Sheets("Current Calc").Cells(3, 2).Value = "-"
EDIT:
When using Auto_Open, Excel must be opened MANUALLY in order for the code to execute; thus if it is opened via VBA this event will NOT trigger. If you want an event to trigger via VBA as well as manually, I'd suggest using Workbook_Open
Try with the following sub:
Private Sub Workbook_Open()
Dim Try1 As String
Try1 = ActiveSheet.Cells(3, 2).Select
Tryl = "-"
End Sub
Some advices:
write your code in lowercase. When you change a line, your code will change to upper and lower case automatically, this way you will detect if you have some typo error.
write a function or object ant type . This will open a dropdown list, this way you will also avoid typo error.

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

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

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.

How do I change the text of a cell without changing the formula?

I am new to VBA. This question has been asked and answered here but the answer seems way too hackish. Is there a better way to change the value of a cell without changing the underlying formula in it? I've tried target.text="changed" but that gives me an error of "Object Required"
The reason I ask is that in a UDF you can change the display of the cell but the formula remains there. How can I do this outside of a UDF?
EDIT:
In the example below, myudf and my_udf appear to do the same thing and everyone is telling me to just do NumberFormat. The problem is that Numberformat will change the cell permanently. If you entered "=my_udf()" in A2 then just go back and type some random text there. Unless you go back and manually re-format the cell (or enter in an excel built-in function), it will display "ThisThatThere".
Module1
Function myudf()
myudf = "ThisThatThere"
End Function
Function my_udf()
my_udf = "Temporary"
End Function
ThisWorkBook:
Sub Workbook_SheetChange(ByVal sh As Object, ByVal target As Range)
If target.HasFormula Then
If LCase(target.Formula) Like "=my_udf(*" Then
target.NumberFormat = "0;0;0;""ThisThatThere"""
End If
End If
End Sub
It is easy if the formula returns a number value rather than a text value .Place a formula in a cell, select it and run:
Sub ChangeText()
Dim DQ As String, mesage As String
DQ = Chr(34)
mesage = DQ & "override" & DQ
ActiveCell.NumberFormat = mesage & ";" & mesage & ";" & mesage & ";"
End Sub
By itself, a UDF , like a non-VBA worksheet formula, can only return a value to a cell.................the best the value can do it to affect conditional formatting.