我已经在VBA中编写了代码来执行一些自动化任务。 Sheet1是一个表单,Sheet2是一个存储此数据的数据库。
我想在最后一行按下SUMIF时也有一个Button
。
SUMIF(Column containing search values, value to search for, column conaintining items to add)
我的代码如下
Dim ws1, ws2 As Worksheet
Set ws1 = ThisWorkbook.Sheets("DataBase")
Set ws2 = ThisWorkbook.Sheets("Emails")
Set rngr = ws2.Range("C17")
Set rngs = ws2.Range("C18")
Set rngt = ws2.Range("C19")
Set rngu = ws2.Range("C20")
Set rngy = ws2.Range("C21")
Set rngw = ws2.Range("C24")
Set rngz = ws2.Range("C25")
h = ws2.Range("C1")
i = ws2.Range("C2")
j = ws2.Range("C3")
tot1 = "=RC[-1]-RC[-2]"
tot2 = "=SUM(RC[-5]:RC[-1])"
tot3 = "=SUM(RC[-5]:RC[-1])"
tot4 = "=SUM(RC[-2]:RC[-1])"
tot = "=SUM(RC[-16]+RC[-10]+RC[-4]+RC[-1])"
k = ws2.Range("C5")
l = ws2.Range("C6")
m = ws2.Range("C7")
N = ws2.Range("C8")
f = ws2.Range("C11")
o = ws2.Range("C12")
p = ws2.Range("C13")
G = ws2.Range("C15")
r = Application.WorksheetFunction.VLookup(rngr, ws2.Range("E18:F19").Value, 2, False)
s = Application.WorksheetFunction.VLookup(rngs, ws2.Range("E18:F19").Value, 2, False)
t = Application.WorksheetFunction.VLookup(rngt, ws2.Range("E18:F19").Value, 2, False)
u = Application.WorksheetFunction.VLookup(rngu, ws2.Range("E18:F19").Value, 2, False)
y = Application.WorksheetFunction.VLookup(rngy, ws2.Range("E18:F19").Value, 2, False)
w = Application.WorksheetFunction.VLookup(rngw, ws2.Range("E25:F26").Value, 2, False)
Z = Application.WorksheetFunction.VLookup(rngz, ws2.Range("E25:F26").Value, 2, False)
lastRow = ws1.Range("A" & Rows.Count).End(xlUp).Row + 1
last_day = ws1.Range("A" & Rows.Count).End(xlUp).Value
col_search = ws1.Range("A3", Range("A" & Rows.Count).End(xlUp))
col_contain = ws1.Range("G3", Range("G" & Rows.Count).End(xlUp))
ws1.Cells(lastRow, 7).Value = Application.WorksheetFunction.SumIf( col_search, "=" & last_day, col_contain)
ws1.Cells(lastRow, 1).Value = Date
ws1.Cells(lastRow, 2) = h
ws1.Cells(lastRow, 3) = i
ws1.Cells(lastRow, 4) = j
ws1.Cells(lastRow, 5) = k
ws1.Cells(lastRow, 6) = l
ws1.Cells(lastRow, 8) = N
ws1.Cells(lastRow, 9) = tot1
ws1.Cells(lastRow, 9).Font.Bold = True
ws1.Cells(lastRow, 10) = f
ws1.Cells(lastRow, 11) = o
ws1.Cells(lastRow, 12) = p
ws1.Cells(lastRow, 13) = G
ws1.Cells(lastRow, 15) = tot2
ws1.Cells(lastRow, 15).Font.Bold = True
ws1.Cells(lastRow, 16) = r
ws1.Cells(lastRow, 17) = s
ws1.Cells(lastRow, 18) = t
ws1.Cells(lastRow, 19) = u
ws1.Cells(lastRow, 20) = y
ws1.Cells(lastRow, 21) = tot3
ws1.Cells(lastRow, 21).Font.Bold = True
ws1.Cells(lastRow, 22) = w
ws1.Cells(lastRow, 23) = Z
ws1.Cells(lastRow, 24) = tot4
ws1.Cells(lastRow, 24).Font.Bold = True
ws1.Cells(lastRow, 25) = tot
ws1.Cells(lastRow, 25).Font.Bold = True
Dim TargetColumns As Variant
Dim SourceCells As Range
Dim rCell As Range
Dim rAddToCell As Range
Dim x As Long
TargetColumns = Array(20, 23) 'Column numbers to place into.
Set SourceCells = ThisWorkbook.Worksheets("Emails").Range("C22,C26")
'Look at each cell in turn.
For Each rCell In SourceCells
'Find the last cell in the correct column.
Set rAddToCell = LastCell(ThisWorkbook.Worksheets("DataBase"), CLng(TargetColumns(x)))
'If there's already a comment then delete it first
'Then add value from SourceCell into comment in Target column.
With rAddToCell
If HasComment(rAddToCell) Then
.ClearComments
End If
.AddComment
.Comment.Text Text:=rCell.Value
End With
x = x + 1
Next rCell
End Sub
要搜索的值将是最后日期,因此实际上我所拥有的是
last_day = ws1.Range("A" & Rows.Count).End(xlUp).Row
我的SUMIF实际格式错误,因为我没有得到想要的结果。也许我的逻辑是错误的,或者代码本身是错误的
ws1.Cells(lastRow, 7).Value = Application.WorksheetFunction.SumIfs(col_search, last_day, col_contain)
错误:
Run-time error 1004
Application-defined or object-defined error
任何建议都值得欢迎,谢谢
答案 0 :(得分:0)
将其更改为
last_day = ws1.Range("A" & ws1.Rows.Count).End(xlUp).Value
否则,您将获得最后使用的行的行号,而不是其值。
另外,您将具有不同语法顺序的SumIf
和SumIfs
混合在一起。
根据WorksheetFunction.SumIfs method的语法是
SumIfs(RangeToSum, Criteria_range1, Criteria1, Criteria_range2, Criteria2, …)
所以我认为您需要切换参数并在条件中添加"=" &
SumIfs(col_contain, col_search, "=" & last_day)
col_contain
=要加和的列col_search
=列以根据条件进行测试或者您使用的语法是WorksheetFunction.SumIf method
SumIf(Criteria_range, Criteria, RangeToSum)
任何可以使用的
SumIf( col_search, "=" & last_day, col_contain)
请注意,我建议稍微减少代码并删除不必要的变量。还使用有意义的变量名,例如wsDB
和wsEmails
,它们比无意义的数字变量或单字母变量(例如a
,b
,{{1})更易于阅读和理解。 } ...
激活c
也是一个好习惯:在VBA编辑器中,转到工具› 选项› Require Variable Declaration < / em>并正确声明所有变量。
Option Explicit