Trying to summarize a database using SUMIFS - VBA - 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

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

Countifs statement compiles but fails with runtime error 438

I have a macro that builds charts on one sheet from data in other sheets in the same book. Currently, it compiles but stops on a countifs statement that looks at dates on one of the other sheets.
The offending line is:
thisBook.rSheet.Range(i, "T") = Application.WorksheetFunction.CountIfs( _
thisBook.sSheet.Cells("K2", "K" & sSheet.Cells(Rows.Count, "K").End(xlUp).Row), "APPROVED", _
thisBook.sSheet.Cells("M2", "M" & sSheet.Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheet.Range(i, "S").Value2 - 6), _
thisBook.sSheet.Cells("M2", "M" & sSheet.Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheet.Range(i, "S").Value2))
The statement is intended to count rows on another sheet where the text in column K equals "APPROVED" and has a date in column M within a specific week . i loops through the 8 rows of this table. Column S is the date that week ends.
Everything has been fully referenced to avoid
runtime error 1004
thisBook is the workbook, rSheet is the sheet with the charts, sSheet is the sheet with the table to evaluate.
Which object doesn't support the property or method here?
Try this, slightly tweaked to make it easier (I hope) to read/follow:
Dim outputRng As Range
Dim rSheetVal as Date
Set outputRng = rsheet.Range(i, "T")
rSheetVal = rsheet.Cells(i, "S").Value2 ' OR, rsheet.Range("S"&i).Value2
With thisBook.sSheet
outputRng = Application.WorksheetFunction.CountIfs( _
.Range("K2", "K" & .Cells(Rows.Count, "K").End(xlUp).Row), "APPROVED", _
.Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), ">=" & (rSheetVal-6), _
.Range("M2", "M" & .Cells(Rows.Count, "M").End(xlUp).Row), "<=" & (rSheetVal))
End With
Assuming the thisBook and rsheet are set like:
Set thisBook = ActiveWorkbook
Set rSheet = thisBook.Sheets("r Sheet")

VBA - use a sub or function

I have written a code for a command button in VBA that uses a (column) range as input and has a (column) range as output. I want to use the same code for other command buttons that refer to other columns. I do not want to repeat the entire code, as only the reference to the columns changes.
I cannot figure out how to define this code as a function or sub that I can 'call' in the code for other command buttons which execute the code on columns B, C, D, etc.
This is the code. It removes duplicates and adds the string "rename" to each element of the list:
Private sub rename_column_A_Click()
'copy values of sheet1 column A to active sheet
Range("A1:A30").Value = Worksheets("sheet1").Range("A1:A30").Value
'remove duplicates, keeping first value as column header
Columns("A:A").Select
ActiveSheet.Range("$A$2:$A$30").RemoveDuplicates Columns:=Array(1), _
Header:=xlNo
Range("A" & 2).Select
'add string to each element of list
For i = 2 To 30
If Not Range("A" & i).Value = "" Then
Range("A" & i).Value = "rename " & Range("A" & i).Value
End If
Next i
End Sub
Like John Coleman suggests, you can have your Sub take a Range parameter:
Private Sub rename_column_A_Click()
ProcessRange "A"
End Sub
Private Sub rename_column_B_Click()
ProcessRange "B"
End Sub
Sub ProcessRange(ColAddress As String)
'copy values of sheet1 column A to active sheet
Range(ColAddress & "1:" & ColAddress & "30").Value = Worksheets("sheet1").Range(ColAddress & "1:" & ColAddress & "30").Value
'remove duplicates, keeping first value as column header
ActiveSheet.Range("$" & ColAddress & ":$" & ColAddress & "$30").RemoveDuplicates Columns:=Array(1), Header:=xlNo
'add string to each element of list
For i = 2 To 30
If Not Range(ColAddress & i).Value = "" Then
Range(ColAddress & i).Value = "rename " & Range(ColAddress & i).Value
End If
Next i
End Sub
I removed the two Select lines of code. I don't think you need them.

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.