Insert formula in one cell using VBA - vba

I know this topic was already asked and I tried to copy on how to insert a formula in one cell, however, I got an error in my vba code.
Here is my code:
ws.Range("C9").Formula = "=CountIf(wsRD.Range(C & Rows.count).End(xlUp).Row, ""Event"")" 'CountIf(wsRD.Range("C" & Rows.count).End(xlUp).Row, "Event") 'count(Search("Event", wsRD.Range("C" & Rows.count).End(xlUp).Row, 1))
I need to insert a formula in ws.Range("C9"), in which, it summarizes the count of the cell that have a value of "Event" in wsRD.Range("C" & Rows.count).End(xlUp).Row. May I know what's the problem in my code? Appreciate your help.
Thank you.

You could get rid of the LRow variable and just drop it in your equation if you wanted to.
Dim LRow as Long
LRow = ws.Range("C" & ws.Rows.Count).End(xlUp).Row
ws.Range("C9").Formula = "=COUNTIF(C10:C" & LRow & ", ""Event"")"

I'm sure this could be the correct answer
ws.Select
LRow = ws.Range("C" & Rows.Count).End(xlUp).Row
Range("C9").FormulaLocal = "=COUNTIF(C10:C" & LRow & ";""Event"")"
So basically, I used FormulaLocal to write the formula the same way I write it in Excel, then, because the formula must be a big String, I separated it in 2 strings, put the value LRow, and used & & to concatenate

Related

Can i store Cells(Rows.Count, 1).End(xlUp).Row in a variable?

I am wondering if I can store this function as a variable. As I am working with vlookups and want my code to be adaptable if rows are added/removed from the source worksheet.
For example, instead of the "2402" being hard coded into this vlookup. Can I place a variable there that can reference the row count formula?
Cells(xlRow, 149).Formula = "=VLOOKUP(EG" & xlRow & "," & FILEPATH & "RDWY'!$A$2:$N$2402,6,0)"
I know I can just do the below, but it could result in the code becoming very long and even more confusing for all my vlookups.
Cells(xlRow, 149).Formula = "=VLOOKUP(EG" & xlRow & "," & FILEPATH & "RDWY'!$A$2:$N$" & Cells(Rows.Count, 1).End(xlUp).Row & ",6,0)"
What I would like to do is have say, " "RDWY'!$A$2:$N$" & rowcount & ",6,0)". Is this possible? If not, why is it not possible?
EDIT: I have set my rowCount variable to rowCount = blanketWB.Sheets("RDWY").Cells(Rows.Count, 1).End(xlUp).Row and it worked!
Thanks for confirming the syntax everyone. I should have just tried it before posting. At least it's here now in case anyone else has a similar question in the future.
Some of my vlookups will have to be hard coded numbers though because there are certain weight breaks in some of the source worksheets. Where just by getting the row count from the last row wouldn't help. However, I can apply this to the majority.
Sure, you can add your "formula" inside a varible, just like this:
Dim formula as String
formula = "=VLOOKUP(EG" & xlRow & "," & FILEPATH & "RDWY'!$A$2:$N$2402,6,0)"
and then you can use it inside your code:
Cells(xlRow, 149).Formula = formula

VBA Sumifs to paste result as values

Would like to know how to code properly a SUMIFS formula and paste the results as value only.
Is it also possible to just have the formula loop only on blank cells? I tried running the code and it seems that is pastes the formula for all cells.
I've attached a sample code which I only got from other forums for reference. Would really appreciate your help guys!
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
End With
With Worksheets("Sheet3")
'Determine last Row in Column A (Filtered Codes)
lastFiltCode = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("B2:K" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",$A2,Database!$I$2:$I$" & lastCode & ",B$1)"
End With
End Sub
You just have to have the work occur in VBA:
cells(1,1).value = Application.SumIfs(Range(Cells(1,1),Cells(10,1)),Range(Cells(1,2),Cells(10,2)), "<0")
Or, you can do this AFTER you have shown the formulas, so the value of the formula is pasted in place of the formula:
cells(1,1).value = cells(1,1).value
Edit1:
To point out why your code is entering the formula, by using .Formula = "", you are telling the system to display within the defined range that exact information. These each enter whatever is within the quotations to the cell:
cells(1,1).value = "Cat"
cells(1,1).formula = "=A1+B1"
cells(1,1).formula = "=A" & i & "+B" & i 'where i is a variable

VBA Evaluate Index Match

I have done a fair bit of searching and can't find an answer for this specific case.
My data set is ~650k lines long so I have been trying to make the code as quick as possible.
What I am trying to do is get VBA to index match an entire column with another sheet.
So far my code is VB:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("Q2:Q" & LastRow).Value = Evaluate("INDEX(MATCH(C2:C" & LastRow & ",'CC Map'!A:A,0),0)")
This very quickly compares column C to sheet 'CC Map'!A:A; and puts the value in column Q.
However, I want to return the corresponding value from 'CC Map'!B:B
When I use
VB:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
Range("Q2:Q" & LastRow).Value = Evaluate("INDEX('CC Map'!B:B,MATCH(C2:C" & LastRow & ",'CC Map'!A:A,0))")
It returns every value the same (corresponding to match C2...)
I think the issue is with having an array as the second argument in INDEX, but I am trying to avoid loops.
What I have at the moment runs almost instantly.
If the return values are text, you could use:
Range("Q2:Q" & LastRow).Value = Evaluate("INDEX(IF(1,T(OFFSET('CC Map'!B1,MATCH(C2:C" & LastRow & ",'CC Map'!A:A,0)-1,0))),)")
For numeric values, replace T with N.

How to use a cell value as part of a range using VBA?

How can you use a cell value to populate a range in VBA?
In this example I want to copy a selection and paste it into a row based on the value of cell L2 with the row number being the same as the L2 value.
The last line of code i understand is incorrect, but can't figure out a suitable solution that would select B100:K100 for example.
Set Rng1 = Range("L2").Value
Range("B2:K2").Select
Selection.Copy
Range("B" & Range(Rng1) : "K" & Range(Rng1).Select
Thanks in advance
If you want row of L2 cell, use as follow:
Range("B" & Rng1.Row & ":K" & Rng1.Row).Select
If you want value of L2 cell, use as follow:
Range("B" & Rng1 & ":K" & Rng1).Select
Here's a slightly cleaner version of DragonSamu's response:
Dim IntRow As Integer
IntRow = Range("L2").Value
Range("B2:K2").Copy
Range("B" & IntRow & ":K" & IntRow).Paste
VBA best practices avoid using .select whenever possible.
Building on DragonSamu & Dr. Treys response:
Dim IntRow As Integer
IntRow = Range("L2").Value
Range("B2:K2").Copy Destination:=Cells(IntRow, 2)
Cells references a single cell, but will paste the whole range as required.
As long as you're not using paste special you can copy and paste in a single command.
Another option would be:
Dim IntRow As Integer
IntRow = Range("L2").Value
Range("B2:K2").Select
Selection.Copy
Range("B" & IntRow & ":K" & IntRow).Select
Instead of defining a range by collecting the value of a cell and then applying it as the row number for the selection, you simply define IntRow as Integer store your row value in IntRow and use it in the range select.

Specifying cell addresses in Excel formula arguments using VBA

I have A column and B column, They compare their previous values And Iam comparing it with the following formulae
=IF((OR(AND(A2=A1,B2=B1),K2=0),0,1) it puts the 0 or 1 on the coresponding Q column
so when it goes to the 5th cell then it becames
=IF((OR(AND(A5=A4,B5=B4),K5=0),0,1)
But im trying to apply it in my VBA code like these
For numm = 2 To lastRow
Sheet1.Cells(numm, "Q").Value = ="IF(OR(AND(sheet1.cells(numm,""A"").value=sheet1.cells(numm-1,""A"")simlar way becolumn),sheet1.cells(numm,""k"").value=0),1,0)"
Next numm
But Im unable to peform the action it says 1004 error and object error
How do i use cells(numm,"A") in my VBA formulae or atleast any other way to put my formula and make it work
The reference to the looping numm within your formula needs to be out of the string.
Maybe you can set in VBA the cell formula itself...
For numm = 2 To lastRow
Sheet1.Cells("Q" & numm).Formula = _
"=IF((OR(AND(A" & numm & "=A" & numm - 1 & ",B" & numm & _
"=B" & numm - 1 & "),K" & numm & "=0),0,1)"
Next numm
Personally, I'd do the whole statement within VBA (ifs, ors, ands) and just drop the value back to Excel. Using Excel formulas makes the code harder to read.
Are you looking for something like:
Public Sub test()
Dim Sheet1 As Excel.Worksheet
Set Sheet1 = ActiveSheet
Dim numm As Integer, lastrow As Integer
lastrow = 5
For numm = 2 To lastrow
Sheet1.Cells(numm, "Q").Value = "=IF(OR(AND(" & Sheet1.Cells(numm, "A").Address & "=" & Sheet1.Cells(numm - 1, "A").Address & "," & Sheet1.Cells(numm, "B").Address & "=" & Sheet1.Cells(numm - 1, "B").Address & ")," & Sheet1.Cells(numm, "k").Address & "=0),1,0)"
Next numm
End Sub
Sheet1 can't be referred to inside the value of the cell, it only exists in the vba function you're creating, so instead you can append the strings together, and just get the cell addresses out to match the sort of function you were creating earlier.