I want to put a formula to a cell and the values that I will be using is based on another cell(not a fix cell).
This is the formula if it is on the excel's formula bar.
=IF(AND(D9=0,J9<>0),100%,IFERROR(K9/D9,0))
But I cannot use this in vba because the formula should be flexible in every cell that I will be using this. Here's my code(I know its not good.)
ws.Range("L" & src1).Formula = "=IF(AND(" & ws.Range("D" & src1).Value & "=0," & ws.Range("J" & src1).Value & "<>0),100%,IFERROR(" & ws.Range("K" & src1).Value / ws.Range("D" & src1).Value & ",0))"
Assuming src1 is an integer value, I believe you want:
ws.Range("L" & src1).Formula = "=IF(AND(D" & src1 & "=0,J" & src1 & "<>0),100%,IFERROR(K" & src1 & "/D" & src1 & ",0))"
Related
EndRowH = ActiveSheet.Range("H65536").End(xlUp).Row
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", "">=" & lngStart & """)"
I Have a criteria on the C column where it has to be greater than the lngStart Value.
The code is working till here.. But I have to add an extra criteria where the sumif needs to be done only if there is blank value in the column I
Dim Blank As Long
Blank = ""
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", "">=" & lngStart & "",I3:I" & EndRowH & ", ""!=" & Blank & "" ")"
When working with complex strings you want to put as a Formula, you might want to use an "helper" string, in the code below I use FormulaString, so when you run the Debug.Print line, you can see if the formula string is valid.
Also, and it's only my preference, to get the " inside, I prefer to use Chr(34).
Code
Dim FormulaString As String
FormulaString = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & "," & Chr(34) & ">=" & lngStart & Chr(34) & ",I3:I" & EndRowH & "," & Chr(34) & "<>" & Chr(34) & ")"
Debug.Print FormulaString
ActiveSheet.Range("H1").Formula = FormulaString
Running this code, you will get in the immediate window the following formula:
=SUMIFS(H3:H10, C3:C10,">=5",I3:I10,"<>")
I'd go this way
ActiveSheet.Range("H1").Formula = "=SUMIFS(H3:H" & EndRowH & ", C3:C" & EndRowH & ", concatenate("">=""," & lngStart & "),I3:I" & EndRowH & ", ""="")"
I have numerous cells on a worksheet with the following formula:
=IF(COUNTIF(O7:O9;"Fail");"Fail";"Pass")
I want to replace all the cells with this formula with the following formula:
=IF(COUNTIF(O7:O9;"");"Default";(IF(COUNTIF(O7:O9;"Fail");"Fail";"Pass")))
How can I change them all to the new formula in one swoop? It is a bunch of cells and please notice that all the cells with a formula have different references.
You can just do a ctrl+h and replace the forumulae directly.
This should work
Just select all cells with "those" formulas you mentioned above, If you select any blank cells, or cells with different formulas, it will crash
You can alternatively select a few cells at a time
Sub changeFormulaOfSelectedCells()
Dim rng As Range
Dim rangeInFormula As String
Dim cellFormula As String
For Each rng In Selection
cellFormula = rng.Formula
rangeInFormula = Mid(cellFormula, 13, InStr(cellFormula, ";") - 13)
rng.Formula = "=IF(COUNTIF(" & rangeInFormula & ";" & Chr(34) & Chr(34) & ");" & Chr(34) & "Default" & Chr(34) & ";(IF(COUNTIF(" & rangeInFormula & ";" & Chr(34) & "Fail" & Chr(34) & ");" & Chr(34) & "Fail" & Chr(34) & ";" & Chr(34) & "Pass" & Chr(34) & ")))"
Next rng
End Sub
If you get an error, Just check if I have not missed replacing any , with ;in the above code
Line 4 is messing my loop up with a type mismatch! What am I doing wrong?
For i = 4 To 8
j = 20 + i
Col = Columns(j)
Range("'" & Col & "3'").FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
Try this:
For i = 4 To 8
Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
By using Cells or Range on their own Excel will assume you want to reference the active worksheet, in the active workbook. It's a much better idea to specify exactly which workbook/ worksheet you want the code to run on. E.g.:
For i = 4 To 8
ThisWorkbook.Worksheets("Sheet1").Cells(3, 20 + i).FormulaR1C1 = "=IF(RC[-11]=0,0,(IF(SUMIF(R3C2:R" & lRow & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")>RC[-11]*1000000, SUMIF(R3C2:R" & lRow1 & "C2, RC2,R3C" & i & ":R" & lRow & "C" & i & ")- RC[-11]*1000000,0)))"
Next i
You're doing many errors.
First, col is a column, not a (range address) string. You cannot concatenate a column to a string.
Second, you should not enclose a range address with single-quotes (').
What you probably wanted to do is:
Cells(3, j).Formula = ...
Please could you tell me what is wrong with my second last SUMIFS formula where I use " < " &$F$1. All that is returned is FALSE in all the cells. The last SUMIFS without the < works fine.
Sub SumGroups()
Worksheets("Database").Activate
Dim lastCode, lastFiltCode As Integer
Dim Formula As String
'Determine Last Row in Column O (Unfiltered Codes)
lastCode = Range("O" & Rows.Count).End(xlUp).Row
'Filter Unique Codes into Column A Sheet2
Range("O1:O" & lastCode).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Sheet2.Range("A1"), Unique:=True
'Determine last Row in Column A (Filtered Codes)
Worksheets("Sheet2").Activate
lastFiltCode = Sheet2.Range("A" & Rows.Count).End(xlUp).Row
'Place SUMIF Formulas in Columns Sheet2
Worksheets("Sheet2").Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
Worksheets("Sheet2").Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & "," < " &$F$1)"
Worksheets("Sheet2").Range("F2:F" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ",$F$1)"
End Sub
Just to clarify why the other answers work:
SumIfs (as well as various other Excel functions requiring string operators (such as CountIf) expect that logical operators and their following statements be expressed as a string. I.e. should be enclosed in speech marks e.g. "myString".
Since a formula is also a string e.g. ActiveCell.Formula = "=If(A1=3, 1, 0)" the compiler gets confused about which set of quotation marks denotes the string. So for example this will not work: ActiveCell.Formula = "=If(A1=3, "Yes", "No")".
Technically the way to deal with this is to enclose the required quotation mark in quotation marks of it own; """ myValue """.
However, this quickly becomes confusing. Instead, use the Character function to return the char you require. In this case 34; chr(34) & myvalue & (chr34).
Worksheets("Sheet2").Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ","" < "" &$F$1)"
This will do it.
put < in quotes.
First, in order to get <$F$1 inside the formula I use the Chr(34) to add the parenthesis "" inside the formula.
Besides that, you are mixing-up a few things:
Try to avoid using Worksheets("Database").Activate and Worksheets("Sheet2").Activate, and instead use fully qualified worksheets and ranges, like: With Worksheets("Database") and inside lastCode = .Range("O" & .Rows.Count).End(xlUp).Row.
You have Sheet2 and Worksheets("Sheet2"), these two not always mean the same worksheet. You could rename Sheet2 (by code name) to "Something", and that will be your 2nd sheet, and rename Sheet3 (by code name) to "Sheet2" and you have a problem. So decide on which method you want to use (I prefer to use Worksheets("Sheet2")).
Dim lastCode, lastFiltCode As Integer means lastCode is actually Variant and lastFiltCode is Integer. Anyway, it's better to use Long for both of these variables when you are trying to get the last row.
Code
Option Explicit
Sub SumGroups()
Dim lastCode As Long, lastFiltCode As Long
'Determine Last Row in Column O (Unfiltered Codes)
With Worksheets("Database")
lastCode = .Range("O" & .Rows.Count).End(xlUp).Row
'Filter Unique Codes into Column A Sheet2
.Range("O1:O" & lastCode).AdvancedFilter Action:=xlFilterCopy, _
CopyToRange:=Worksheets("Sheet2").Range("A1"), Unique:=True
End With
With Worksheets("Sheet2")
'Determine last Row in Column A (Filtered Codes)
lastFiltCode = .Range("A" & .Rows.Count).End(xlUp).Row
'Place SUMIF Formulas in Columns Sheet2
.Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
.Range("D2:D" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & "," & Chr(34) & "<" & Chr(34) & "&$F$1)"
.Range("F2:F" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2,Database!$I$2:$I$" & lastCode & ",$F$1)"
End With
End Sub
i come to ask your help i need to rank data using vba i have a block of results in column D and i want to rank them in column E without skiping any value so i tried this vba code but it gives me only zeros in all the column then my computer becom slow untill i close the excel file this is the vba code i am using if anyone can help me :
Sub Mactro5()
LastRow = Range("D" & Cells.Rows.Count).End(xlUp).Row
Range("E2:E" & LastRow).Formula = _
"=IF(D2=" & Chr(34) & Chr(34) & "," & Chr(34) & Chr(34) & ",SUMPRODUCT((D$2:D$" & LastRow & ">D2)/COUNTIF(D$2:D$" & LastRow & ",D$2:D$" & LastRow & "&" & Chr(34) & Chr(34) & "))+1)"
End Sub