struggling to get the parenthesis in the right spot. this is formula in a specified range in vba macro
Range("C" & i).Formula = "=INDEX($C$1:$C$2,MATCH(" * "&TRIM(RIGHT(A" & i & ",LEN(A" & i & ")-FIND(" - ",A" & i & ")))&" * ",$A$1:$A$2,0),0)"
Your formula should be..
.Formula = "=INDEX($C$1:$C$2,MATCH(""*"" & TRIM(RIGHT(A8,LEN(A8)-FIND(""-"",A8)))& ""*"",$A$1:$A$2,0),0)"
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 am having trouble with a Sum Countifs formula in VBA. I have tried Brackets and an array . Below is the Array attempt
Dim finRow As String
finRow = Sheets("Sheet1").Range("A500000").End(xlUp).Row
Sheets("Macro").Range("B51") = WorksheetFunction.Sum(WorksheetFunction.CountIfs(.Range("E2:E" & finRow), Array("Positions - Active Recruitment", "Positions - Not Actively Recruiting"), .Range("B2:B" & finRow), "=" & Sheets("Macro").Range("$E$3").Value, .Range("Y2:Y" & finRow), "=Contingent Worker", .Range("T2:T" & finRow), "=" & Sheets("Macro").Range("$D$3").Value))
Below is the Bracket attempt which gives me invalid character errors in VBA
Sheets("Macro").Range("B51") = WorksheetFunction.Sum(WorksheetFunction.CountIfs(.Range("E2:E" & finRow), {""=Positions - Active Recruitment"", ""Positions - Not Actively Recruiting""}, .Range("B2:B" & finRow), "=" & Sheets("Macro").Range("$E$3").Value, .Range("Y2:Y" & finRow), "=Contingent Worker", .Range("T2:T" & finRow), "=" & Sheets("Macro").Range("$D$3").Value))
Any help would be much appreciated. Thank you! Matt
I'm trying to do a VBA code to accomplish 2 things as follows:
Count how many characters there is on cell A1, using the formula LEN(A1) and one the last line, I'm trying to have the formula RIGHT(LEFT(A1;Q1-2);6) on cell J1
Please follow down my VBA code so far:
LR = Cells(Rows.Count, "A").End(xlUp).Row
For i = 1 To LR
cel = "A" & i
cel2 = "P" & i
cel3 = "Q" & i
Range("P" & i).Formula = "=LEN(" & cel & ")"
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-" & 2 & ")," & 6 & ")"
Next i
It seems something silly what is missing, however, I couldnt manage to solve it so far
Thanks in advance
You’re missing a Right, and some other things
Range("J" & i).Formula = "=RIGHT(LEFT(" & cel & "," & cel3 & "-2), 6)"
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 really appreciate if anyone could help; I've been working on this for a while...
I just want to define the formula of countif in a cell, here is the code:
Range("E" & PLrowstart).Formula = "= CountIf($B$PLrowstart:$B$PLrowend" & ",B2)"
PLrowstart and PLrowend are integer variables I set before the line. The range for count if is range("B" & PLrowstart & ":B" & PLrowend). I've also tried other ways, none worked...
TIA.
Range("E" & PLrowstart).Formula = "= CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"
Try this
Sub SetFormula()
PLrowstart = 2
PLrowend = 4
Range("E" & PLrowstart).Formula = "=CountIf($B$" & PLrowstart & ":$B$" & PLrowend & ",B2)"
End Sub