I am trying to insert a SUMIF formula into a cell using VBA. VBA is not allowing my code to run as it is. Any suggestions are appreciated.
ws and na are properly set earlier on in the code. If I simply change the SUMIF formula to a random value "x" , it appears in the desired cell. The error is occurring within the SUMIF formula that I am trying to insert into a cell.
ws.Range("B" & na.Row + 2).Value = "=SUMIF(OFFSET(B1,,,ROW()-1,1),"<>#N/A"))"
The purpose of this formula is to SUM a column of numbers while ignoring any cells that contain "#N/A".
When using quotes in a formula, you need to "double up":
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1),""<>#N/A"")"
You can use AGGREGATE and remove the OFFSET which is volatile
ws.Range("B" & na.Row + 2).Formula= "=AGGREGATE(9,6,B1:B" & na.Row + 1 & ")"
Try using 'Chr(34)':
ws.Range("B" & na.Row + 2).Formula = "=SUMIF(OFFSET(B1,,,ROW()-1,1)," & Chr(34) & "<>#N/A" & Chr(34) & ")"
Edit: Deleted quotes written by mistake
Related
Range(A1).Value = "=SUM(Range(B1), Range(B1).Offset(xlToRight))"
I want to write a formula in a cell by using VBA, but I want to make it dynamic.
How can I write this code?
What I've tried is written above.
try this:
Range("A1").Formula = "=SUM(" & Range("B1").Value & "," & Range("B1").Offset(0, 1).Value & ")"
I'm trying to run the below code and it gives me
Run-Time Error "1004"
Application-defined or Object-defined error
Every Single Time!!
Attached is a snippet of the code, any suggestions what's wrong? (The numbers in the Range(Cells( * ) sections are actually mostly variables in my overall macro, it's pretty complex but I've taken them out for simplicity here)
Code:
'Declare variables
Dim CriteriaRng As String
Dim SumRng As String
Dim Criteria As String
'Set a variable for each of the 3 parts of the SUMIF Formula
CriteriaRng = "'" & Sheets(1).Name & "'!" & Range(Cells(2, 4), Cells(88, 4)).Address
SumRng = "'" & Sheets(1).Name & "'!" & Range(Cells(2, 3), Cells(88, 3)).Address
Criteria = Chr(34) & "=" & Chr(34) & " & RC[-1]"
'Here goes the SUMIF Formula
With Sheets(2).Range(Cells(4, 13), Cells(9, 13))
Debug.Print "So the Whole Formula Should be:" & Chr(13) & "= SUMIF(" & CriteriaRng & ", " & Criteria & ", " & SumRng & ")"
'That was a vain attempt to find out what was wrong with the formula; didn't work though.
.FormulaR1C1 = "= SUMIF(" & CriteriaRng & ", " & Criteria & "," & SumRng & ")"
'Then adds NumberFormat and stuff here, but that isn't relevant to this question.
End With
The error always hits on the line where it's putting in the .FormulaR1C1 = .
Yes, I know I could get the same result using a nested loop, but that would return just the value without the SUMIF formula - I need that formula so the sheet updates when edited (without needing a macro - I'm sending the sheet on to other people who won't have or want any macros, but might need to edit the data).
Can anyone point out to me what is wrong? I'm prepared for it to be something pretty basic - only last week I spent 2 hours figuring out a problem from misspelling 'Columns' !!!
Any and all advice welcome - Many Thanks in advance.
I have a table in which I count the records through Worksheet.Function.Countif.
It is nice because it counts the rows using .Rows.Count and so I am alwasy ensured if my table changes the size.
It looks like that (subset of the code):
endrow = .Cells(.Rows.Count, 20).End(xlUp).Row
ws1.Cells(6, 34).Formula = "=COUNTIF(" & .Range("U6:U" & endrow).Address & ",U6)"
I wish to write the the worksheet.function formula in the same way as above but for 'Countifs'. In excel, I would type it like that:
=COUNTIFS($U$6:$U$144;U6;$T$6:$T$144$;"<>"&T6)
How to write it in vba, using 'endrow' as in the first demonstarted code, i.e. without '144' as the last row but with '& endrow' ?
I was trying multiple times, but I cannot get it to work :/
I will appreciate any help.
Try this:
ws1.Cells(6, 34).Formula = "=COUNTIFS($U$6:$U$" & endrow & ",U6,$T$6:$T$" & endrow & "," & """" & "<>" & """" & "&T6" & ")"
This formula gets the last row of column A:
=IFERROR(LOOKUP(2,1/(NOT(ISBLANK(A:A))),ROW(A:A)),0)
I have the following problem,
Say I have the following cell (very simplified):
CBA 4.5 01/22/2019, I want to use VBA to plant a BDP() function in the adjacent cell in order to find out what the ISIN is. Without excel I would use =BDP(A1 & " Corp"; "ID_ISIN")right?
I am attempting to insert this function in VBA, and it does not work:
TOMS.Cells(1, 2).Value.Formula = "=BDP(" & TOMS.Cells(1, 1).Value & " Corp, ID_ISIN)"
Any ideas?
You forgot the " inside the formula, which must be doubled in vba:
TOMS.Cells(1, 2).Formula = "=BDP(""" & TOMS.Cells(1, 1).Value & " Corp"", ""ID_ISIN"")"
I have a summary sheet which I use as an index to access several sheets via a hyperlink. I'm extending it so they link directly to the appropriate row within each sheet.
I'm newish to VBA and not sure on the syntax.
I'm basically trying to use the value from variable j as the row number in the cell reference.
"'!A" is the first part of my cell ref in the code below. I'm then trying to concatenate the string to add j as the row number. Can this be done using the code below? I tried surrounding j with brackets, apostrophe to no avail.
For j = 2 To LastUsedRow
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j",""" & (ThisWorkbook.Worksheets(i).Name) & """)"
Many thanks
Debug.Print is your friend. This is what you get with your current code (assuming j = 1 and ThisWorkbook.Worksheets(i).Name = "Sheet1"):
=Hyperlink("#'Sheet1'!A1,"Sheet1")
The formula should look like this:
=HYPERLINK("#'Sheet2'!A1","Sheet2")
So...add just add the missing ":
link = "=HYPERLINK(""#'" & (ThisWorkbook.Worksheets(i).Name) & _
"'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
The including of double quotes within VBA strings is tricky.
link = "=Hyperlink(""#'" & (ThisWorkbook.Worksheets(i).Name) & "'!A" & j & """,""" & (ThisWorkbook.Worksheets(i).Name) & """)"
will get link as =Hyperlink("#'SheetName'!A2","SheetName")
You need to call on what you want to pull from j, such as: j.Value