This was my previous post Insert COUNTIF formula when you have variable holding the value.
Below was the Solution.
Range("Q" & minRow + 1).Formula = "=COUNTIF(P$" & minRow & ":P" & minRow & ",P" & minRow + 1 & ")=0"
I have a new Question. What if the column is a variable?
What is the syntax if both are Variables (column and row are unknown and their values stored in a variable) and what is the syntax if column is variable and row is a number?
I have tried these ways
"=COUNTIF( & Columnz $1: & Columnz &2 ,& Columnz &2000)=0"
and these way
"=COUNTIF( "& Columnz" $1: " & Columnz"2,& Columnz &2000)=0"
To define a range, you can also use Cells, for instance:
ActiveSheet.Cells(1,1) '=Range ("A1")
ActiveSheet.Cells(1,"A") '=Range ("A1")
If you want to define a range, you can do:
Range(Cells(1,1), Cells(10,5)) '=Range("A1:E10")
Thus, you can do:
'where Columnz is a Long or an Integer
"=COUNTIF(" & Range(Cells(1, Columnz), Cells(2, Columnz)).Address & "," & Cells(2000,Columnz).Address & ")=0"
I'd like to add to the nice above responses, that OFFSET is very usefull, specially while looping.
e.g.:
With Range("B3")
For i = 1 to 10
.offset(0, i) = "something"
Next i
End With
You can also make your VBA much more readable, and eliminate the need for "variable formulae" by using the native Excel (R1C1) syntax. Like
myRange.offset(0,i).FormulaR1C1 = "=SUM(R1C[-1]:RC[-1])"
which means sum from row 1 of previous column till same row of previous column.
Finally, you can use the "dual arguments" version of RANGE(cell1, cell2):
With Range("B3")
For i = 1 to 10
.offset(0, i).formula = "=SUM(" & Range(cells(10, 1),cells(10, i)).address & ")"
Next i
End With
The row number is already a variable in your example: minRow. String concatenation is done with an ampersand (&) in VB/A. You are halfway right but missing the second ampersand. You can think about it like this:
"first string" & variable1
This is a concat between 2 strings, if you want to add a third string, you have to use another ampersand:
"first string " & variable1 & "second string"
Your code:
"=COUNTIF(" & columnz & "$" & minRow & ":" & columnz & minRow & ",P" & (minRow + 1) & ")=0"
In response to your comments:
"=COUNTIF(" & columnz & "$1" & ":" & columnz & "1,P2)=0"
Just remove the variable from the string and include the row in the other string literals.
it can be somthing like that:
startRow = 3
endRow = 17
myRange=("B" & StartRow & ":" & "B" & EndRow)
then your range = ("B3":"B17")
Related
I need to create a formula that is going to concatenate 3 different cells into one date. The formula is going to be a part of a loop function so I need the cell reference to change as the loop function runs.
I am having trouble with the syntax such as the "&" and the """" that are necessary to distinguish parts of the formula from the cell references.
For now I am just trying to get the formula to paste into a single cell without the loop. The 3 cells that I am combining are in columns: N,O & P. I am trying to paste the formula into column M.
I tried creating the formula on a separate "Data" tab and then simply copy and pasting it into each cell using VBA but the row number does not update according to the row that the formula is pasted.
I tried rearranging the & and "" for a while and could not figure out the winning combination.
FormulaRow = Cells(Rows.Count, "M").End(xlUp).Offset(1).Row
M_Formula = "=N" & FormulaRow & "" / "" & "O" & FormulaRow & "" / "" & "P" & FormulaRow
Range("M" & FormulaRow).Value = M_Formula
I am expecting to get the following result: =N5&"/"&O5&"/"&P5 with the row number corresponding to the row that the formula is pasted.
When I tried the copy and paste method I got this message: "Object Doesn't Support this Property or Method"
Any help would be appreciated. Thank you!
Maybe:
Sub sub1()
' If you have 12 in N2 and 34 in O2 and 5678 in P2:
Dim FormulaRow&, M_Formula$
FormulaRow = 2
M_Formula = "=N" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"O" & FormulaRow & "&" & """" & "/" & """" & "&" & _
"P" & FormulaRow
Cells(FormulaRow, ColNum("M")) = M_Formula ' gives the formula you want 12/34/5678
Cells(FormulaRow, 13) = M_Formula ' also gives the formula you want 12/34/5678
End Sub
Function ColNum&(col$)
ColNum = Range(col & 1).Column
End Function
Excel is smart enough to increment the row reference when you do a range in one go:
Range("M5:M" & Range("N" & rows.count).end(xlup).row).formula = "=N5 & ""/"" & O5 & ""/"" & P5"
That will do what you want in 1 line.
Then you can copy the result, paste as values then format to date with something like this:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=N5 & ""/"" & O5 & ""/"" & P5"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).NumberFormat = "DD/MM/YYYY"
'Force a reevaluate to make it see actual dates
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Value
End Sub
Using this method I just tested on over 12,000 rows and it took under a second.
With regards to the comments about using the date function, using the date function is a much better method, I wanted to show you how to do it using your own method but you can get rid of the formatting code if you use Date like so:
Sub EnterDate()
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Formula = "=DATE(P5,O5,N5)"
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).Copy
Range("M5:M" & Range("N" & Rows.Count).End(xlUp).Row).PasteSpecial xlPasteValues
End Sub
If you want to leave the formulas then simply delete the last 2 lines in there.
I'm trying to use a variable into formula using VBA. I've referenced How to pass an integer variable into a vba formula this question and tried implementing that in my if formula.
=IF (startCell >0, startcell+confirmed, endcell+confirmed)
but I'm still unsure and getting an error.
My code is the following:
headerrow = 7
startCell = 8
endCell = 3
confirmed= 5
DDSS_Actual.Cells(headerrow + 3, 3).Formula = "=IF(" & startCell & ">" & endCell & ","& startCell & "+" &confirmed&", " & endCell & "+" &confirmed &")"
Thanks.
You just need to put spaces before and after the &
which makes it
DDSS_Actual.Cells(headerrow + 3, 3).Formula = "=IF(" & startCell & ">" & endCell & "," & startCell & "+" & confirmed & "," & endCell & "+" & confirmed & ")"
I am trying to average a column but only if the value is greater than zero. I then want it to put the information in the next blank cell in that row.
The below code was working as a simple Average but I want it to exclude any values of zero from the above cells.
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGE(" & .Address & ":" & .End(xlDown).Address & ")"
End With
I Tried with the following code to have it as if the cell address is greater than zero. But it keeps giving me an error to debug?
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & "," & Cell.Address & " > 0," & .Address & ":" & .End(xlDown).Address & ")"
End With
Any help would be great.
Thanks
Al
Your syntax for the formula is wrong.
You need to create a formula like
=AVERAGEIF(D2:Dxx, ">0")
So use this
With Range("D2")
.End(xlDown)(2, 1) = _
"=AVERAGEIF(" & .Address & ":" & .End(xlDown).Address & ","">0"")"
End With
I have used the below code to get the answer. I m getting the result at my required location but sum is not displayed.
Cells(lastrow, 2).Offset(3, 1).Value = "=Sumif(L4:L" & lastrow & "," & "NEW ALBERTSONS INC" & ",J4:J" & lastrow - 1 & ")"
Could some one help me how to sort it out.
Try this one:
Cells(lastrow, 2).Offset(3, 1).Formula = "=SUMIF(L4:L" & lastrow & "," & """NEW ALBERTSONS INC""" & ",J4:J" & lastrow & ")"
Some notes:
I'm using """NEW ALBERTSONS INC""" instead "NEW ALBERTSONS INC"
(you should escape your quotes when constucting excel formulas
through vba)
sum_range and criteria_range should have same dimmentsion, that's
why you should use L4:L" & lastrow and J4:J" & lastrow (or &
lastrow-1 for both ranges)
actually, you can slightly simplify your formula by changing "," & """NEW ALBERTSONS INC""" to ", ""NEW ALBERTSONS INC"""
UPDATE:
Got a new issue with a formula, can't quite get it to work because of text in the formula, the formula (as taken from excel) should be,
=IF(D2<=0,"No Sales Price",E2/D2)
I have tried as many combinations as I can think of but the "no sales price" is causing an issue with the quotation marks. My current code is
For i = 2 To LastRowG
Range("Q" & i).Formula = "=IF(D" & i & "<=0," & "(No Sales Price)", & "(E" & i & "/D" & i & "))"
Next i
have had a look around but been unable to see any resolutions to the problem, any enlightenment will be met with the greatest appreciation
EDIT:This was fixed by inserting the following lines;
For i = 2 To LastRowG
Range("Q" & i).Formula = "=IF(D" & i & "<=0," & Chr(34) & "No Sales Price" & Chr(34) & "," & "E" & i & "/" & "D" & i & ")"
Next i
The Chr(34) inserts the ASCII character appertaining to that number which just so happens to be ". The program doesn't read it as having typed in the quote marks and continues to read the line of code correctly but then places the "no sales price" correctly in the formula.
It will output the line as the formula is intended to be and the Chr(34) is like writing ""No Sales Price"" without the inevitable "expected end of statement" error
What I suggested will result in something like this:
LastRow = Cells(Rows.Count, "C").End(xlUp).Row
Cells(LastRow + 1, 3).Formula = "=SUM(C1:C" & LastRow & ")"
Extra 1
Is it possible to use this formula to enter the word Total in the cell to the left?
Range("B" & LastRow + 1) = "Total"
Extra 2
One more just to push my luck, how about copying a formula all the way down a column the the last cell? =G2*57.5 copied until the last row in I
LastRowG = Cells(Rows.Count, "G").End(xlUp).Row
For i = 2 To LastRowG
Range("I" & i).Value = "=G" & i & "*57.5"
Next i