SUMIF equation in worksheet not working with VBA - vba

I have lists of $ sale amounts in one column and then another column near it that shows if it's a "YOR" for sales or "RE" for returns and I want to sum the dollar figures of the YORs.
I have an equation that I am trying to insert into a worksheet using a macro. When you run the macro there is a cell in H16 that should fill in with the resulting total sale number
There's a variable entitled CellNumber (Long) that holds the # of items in the list.
VA05NDump is the name of the worksheet with the data.
Range("H16").FormulaR1C1 = "SUMIF((VA05NDump!R2C3:VA05NDump!R"&CellNumber&"C3),"YOR",R2C13:R"&CellNumber&"C13)
when I click out of the equation in VBA it says "compile error" expected: end of statement" and highlights what is inside this bracket: ["C3),"]
I've tried changing the "YOR" to ="YOR" and "=YOR" but that doesn't change anything.
Any help would be awesome!
Thanks!

Try this:
Range("H16").FormulaR1C1 = "=SUMIF((VA05NDump!R2C3:VA05NDump!R" & CellNumber & _
"C3),""YOR"",R2C13:R" & CellNumber & "C13)"
Revisions:
1. Added = sign upfront.
2. Double quoted YOR in this part: "C3),""YOR"",R2C13:R". Read this which discusses ways of putting double quotes which applies to all strings in "" for worksheet formulas.
3. Added the missing " at the end.

You are missing a closing " at the end and probably a = at the beginning of your formula.

Here is a way that I have previous entered a formula into a cell.
It is done by entering the formula as a string into the cell.
'Make Formula into a string
Dim Formula As String
Formula = "=SUMIF((VA05NDump!R2C3:VA05NDump!R" & CellNumber & _
"C3)," & chr(34) & "YOR" & chr(34) & ",R2C13:R" & CellNumber & "C13)"
'Activate the desired range
Range("H16").Activate
'Enter formula string into active cell
ActiveCell = Formula
Also "Chr(34)" can be used as ". I find this help to create a string where
chr(34) & "YOR" & chr(34) = ""YOR"" so when it is entered into the cell it becomes "YOR"

Related

Linking cells to different worksheet by using vba to insert dynamic formulas

I am creating a new worksheet called varRef and am trying to link cells of an existing worksheet to this new worksheet by using VBA. I have been trying to solve it for a couple of hours but cannot get it right and cannot find a similar case on the web either.
The code is:
Dim varRef as Variant 'name of the new sheet
varRef = Inputbox("xyz") 'this is how I define the name
Dim intRow As Integer 'row that corresponds to the appropriate postion in the existing sheet
intRow = ActiveCell.Row 'see above
Cells(intRow, 22).Formula = "=IF(varRef & ""!I148""="""","""",varRef & _
""!I148"")" 'trying to link the contents within the same workbook
The result in the cell I get is
=IF(varRef & "!I148"="","",varRef & "!I148"
which is obviously not working.
Issue 1) is that VBA does not recognize my variable in the formula. It is working for naming the sheet however.
Issue 2) is the quotation marks, that are not working as intended. One is supposed to use double quotation marks to not end the string. However the second marks will not disappear in the final code of the cell.
Any help is very much appreciated and hopefully valuable for other users as well!
You had some count issues with hoe many " you have before and after the varRef variable.
Also, I prefer to use Chr(34) to have " inside the Formula string, this way I don't get confused with how many " I need to use.
Try the code below:
Cells(intRow, 22).Formula = "=IF(" & varRef & "!I148=" & Chr(34) & Chr(34) & "," _
& Chr(34) & Chr(34) & "," & varRef & "!I148)"

Excel vba add named range

I trying to add named range with VBA
Nname = "FindMe"
Formulas = "Vlookup(" & Chr(34) & Nname & Chr(34) & ";Translations!$A:$C;2;False)"
ActiveWorkbook.Names.Add Name:=Nname, RefersToR1C1:=Formulas
But i get
Vlookup(""FindMe"";Translations!$A:$C;2;False)
Instead of
Vlookup("FindMe";Translations!$A:$C;2;False)
Why Excel put double qoutes? How to Get string which i want?
Vlookup("FindMe";Translations!$A:$C;2;False)
The string assigned to Formulas is missing an '=' at the beginning of the string. Thus, the name is being assigned a string constant instead of a formula. When you look in Name Manager dialog, you will that name FindMe refers to ="Vlookup(""FindMe"";Translations!$A:$C;2;False)". Note that two double quotes represents a single quote in a string constant.
If you add a '=' to the beginning of the string, then name will be assigned a formula. Also, the formula is using A1 notation but the VBA code is using RefersToR1C1. This will cause a formula parsing error. Either change the formula to R1C1 notation or change the VBA code to use RefersTo. For example...
Nname = "FindMe"
Formulas = "=Vlookup(" & Chr(34) & Nname & Chr(34) & ",Translations!$A:$C,2,False)"
ActiveWorkbook.Names.Add Name:=Nname, RefersTo:=Formulas

Excel custom hyperlink for each cell in range using VBA

I have a module that removes all formulas from an entire sheet and then, it should, create a hyperlink formula on each cell using the cell value.
Sub Test()
Dim ws1 As Worksheet
Set ws1 = ThisWorkbook.Worksheets("Test")
ws1.Range("B33:F533").Value = ws1.Range("B33:F533").Value
For Each i In ws1.Range("B33:B533")
i.Formula = "=HYPERLINK(""https://somelocation/" & i.Value & """,""" & i.Value & """)"
Next
End Sub
EDIT: It's now working perfectly. Thanks For all the help!
i.Formula = "=HYPERLINK('https://somelocation/"&i.Value&","& i.Value"')"
Has an error in the formula. At the i.Value"') you are missing &, i.Value & "'. The correct one, that 'compiles', is added below:
i.Formula = "=HYPERLINK('https://somelocation/" & i.Value & "," & i.Value & "')"
Also it is worth noting that instead of writing "&i.Value&" and let the VBA IDE add the spaces, it is better to do it yourself, eg: " & i.Value & ".
You're code has 2 flaws, you should add the last & to get a syntax correct formula, and add spaces between the " and &, or else VBA won't see it is right.
Edit; a third flaw:
The line to create the formula is wrong too. Let's break it down:
"=HYPERLINK('https://somelocation/" & i.Value & "," & i.Value & "')"
The parameter in the formula would be:
'https://somelocation/" & i.Value & "," & i.Value & "' so an output example of this line could be 'https://somelocation/1,1'. At first, I thought you are writing an URL with a comma, alright..? But then I looked at the HYPERLINK function in Excel and it requires two parameters. You are only giving one parameter. Excel formulas also expect a double quote instead of a single quote. You can escape a double quote in VBA like this "".
The correct line should be:
i.Cells(1, 1).Formula = "=HYPERLINK(""https://somelocation/" & i.Value & """,""" & i.Value & """)"

Excel vba error name in formula

I'm trying to pass one formula from vba excel to a cell i have this code
atmFecha = "=IF(L" & tmLastRow & "=0," & Chr(34) & " " & Chr(34) & ",'ws2'!$E$3)"
.Cells(tmLastRow, "H").Value = atmFecha
this instruction insert in the cell the correct formula but display #NAME? I have to press "F2" and then "Enter" to Excel recognize the formula and display the correct value.
I use the instructions formula and formulaR1C1 but the result is the same.
what can i do to recognize automaticly the correct value of the formula ?
After your code, add this line:
Application.Calculate
It sounds like you are set to Manual Calculation, so that line will refresh everything. If you're setting a number of formulas, wait until they're all inserted, then execute that line.
I don't know how it fix but now is working, I was testing with the second file I made this is the code"
Sub btnCalculate()
Dim atmFecha As String
Dim tmLastRow As Integer
With Worksheets("ws1")
tmLastRow = 1
atmFecha = "=IF(C1=0," & Chr(34) & " " & Chr(34) & ",'ws2'!$A$1)"
.Cells(tmLastRow, "A").Value = atmFecha
End With
End Sub

VBA Set Cell value to text containing ' not working

I'm trying to get a macro to type out a heap of formulae for me. The formula goes something like this:
=COUNTIF('other_sheet'!A:A,"hello*")
The user can specify A and other_sheet, so I select the cell and then go
ActiveCell.Value = "=COUNTIF('" & othercell & "'!" & column & ":" & _
column & """,hello*"")"
But it keeps giving me errors like:
1004: Object Defined Error
I have tried using ActiveCell.Text, ActiveCell.Formula etc. and they all don't work.
It needs to be ActiveCell.Formula and ", is the wrong way around next to Hello. It should be:
ActiveCell.Formula= "=COUNTIF('" & othercell & "'!" & column _
& ":" & column & ",""hello*"")"
Apparently you can only put proper, completed answers into a cell.
i.e. if the output turns out to be "=sum(A:A" and a bracket isn't closed, then the application throws an error just as if you typed it in manually.