Specifying cell addresses in Excel formula arguments using VBA - 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.

Related

Insert formula in one cell using 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

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

Trying to summarize a database using SUMIFS - VBA

'The number rows in the database changes each month hence the variable lastCode.
'The code runs without problems when I manually put last row in i.e. $M$22510 and $O$22510 however when I put the variable & lastCode in the SUMIFS I receive the error
Run-time '1004'application-defined or object defined.
Please tell me what is wrong with my SUMIFS in the code below.
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 Formula in Column B Sheet2
Worksheets("Sheet2").Range("B2:B" & lastFiltCode).Formula = _
"=SUMIFS(Database!$M$2:$M$ & lastCode,Database!$O$2:$O$ & lastCode,A2)"
End Sub
What you are looking for is:
"=SUMIFS(Database!$M$2:$M$" & lastCode & ",Database!$O$2:$O$" & lastCode & ",A2)"
The reason for this is when you put something into "quotation marks" VBA will read it as just text, you can use Debug.Print "=SUMIFS(Database!$M$2:$M$ & lastCode,Database!$O$2:$O$ & lastCode,A2)" and either press Ctrl + G or in VBA select "View" > "Immidiate Window" and it will show you what exactly is going into Excel

Vba to vlookup value & copy next cell value to invoice sheet

Formula use for Vlookup
activeWS.cell("A9") =INDEX(Breaking_Data!F5:F,MATCH(A8,Breaking_Data!A5:A,0))
I try to run it in VBA code but it doesn't work, any one know how to modify it for VBA code.
that lookup A8 cell value in ws("Breaking_Data") with Range("A5:A").lastcellvalue if any value match then copy same row cell value form range ("F5:F") & paste it in active sheet cell A9.
Try this:
dim lastR as Long: LastR = Worksheets("Breaking_Data").Range("A" & Rows.Count).End(xlUp).Row
activeWS.cell("A9").Value = Application.Evaluate _
("Index(Breaking_Data!F5:F" & lastR & ", Match(A8, Breaking_Data!A5:A" & lastR & ", 0))")
You have 2 options.
You can include it as a function:
activeWS.cell("A9") = "INDEX(Breaking_Data!F5:F1048576,MATCH(A8,Breaking_Data!A5:A1048576,0))"
or you can get the value itself:
activews.Range("A9") = Application.Index(Sheets("Breaking_Data").Range("F5:F1048576"), Application.Match(activews.Range("A8"), Sheets("Breaking_Data").Range("A5:A1048576"), 0))

VBA for Excel 2007 and above

I have written some VBA code that looks at a column, finds the next cell in that column which contains data, and sets the cells in between the two as a range. This was originally written for an Excel 2003 Workbook. And of course the same command does not work for a 2007 and above workbook. Can anyone help translate this for 2010 Excel VBA.
Here is the original Code:
Dim first As Integer
Dim Last As Integer
Dim i As Integer
Dim n As Integer
n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 2
Range("A3").Select
For i = 1 To n
first = (ActiveCell.Row + 1)
Selection.End(xlDown).Select
Last = (ActiveCell.Row - 1)
Range("J" & first & ":J" & Last).Select
Selection.Value = "=J$" & (first - 1)
Range("A" & Last + 1).Select
Next i
When I run it in Excel 2010. Instead of finding the next cell in Column A that contains data it just select the entire column.
Thanks in Advance.
Sub GoDownList()
Dim first As Integer
Dim Last As Integer
Dim i As Integer
Dim n As Integer
first = Range("A3").Row
Last = Range("A3").End(xlDown).Row
Range("J" & first & ":J" & Last).Value = "=J$" & (first - 1)
Do
first = Range("A" & Last).End(xlDown).Row
Last = Range("A" & first).End(xlDown).Row
Range("J" & first & ":J" & Last).Value = "=J$" & (first - 1)
If Range("A" & Last).End(xlDown).Row > 1000000 Then
Exit Do
End If
Loop
End Sub
I think your code doesnt work because you used Selection.Value instead of Selection.Formula.