我正在尝试打开多个工作簿,将多个工作簿/工作表中所有COUNTIFS的SUM值相加,然后将总和除以主工作簿中的现有值(不断打开),然后将该最终值发送到目标单元格在主工作簿中。
Sub TotalCountIfs()
Workbooks.Open "C:\Book1.xls"
Workbooks.Open "C:\Book2.xls"
Workbooks.Open "C:\Book3.xls"
Workbooks.Master.Sheet1.Range(“B2″).Value = Application.WorksheetFunction.
SUM
(Workbooks.Book1.Sheet1.Countif(Range(“A2:A8″), “>”50,
Workbooks.Book2.Sheet1.Countif(Range(“A2:A8″), “>”50,
Workbooks.Book3.Sheet1.Countif(Range(“A2:A8″), “>”50)
/Workbooks.Master.Sheet1.Range(“A2”).Value
Workbooks("Book1.xls").Close
Workbooks("Book2.xls").Close
Workbooks("Book3.xls").Close
End Sub
为松散的代码表示歉意。只是试图传达所需的逻辑。我不是专家,但是我认为函数或循环会为我省去一些麻烦,因为实际任务涉及打开10多个工作簿,每个工作簿的列范围超过25个。
答案 0 :(得分:1)
尝试一下。
Sub TotalCountIfs()
Dim wbm As Workbook, wb1 As Workbook, wb2 As Workbook, wb3 As Workbook
Set wbm = ThisWorkbook '<~~ might need to clarify this
Set wb1 = Workbooks.Open("C:\Book1.xls", ReadOnly:=True)
Set wb2 = Workbooks.Open("C:\Book2.xls", ReadOnly:=True)
Set wb3 = Workbooks.Open("C:\Book3.xls", ReadOnly:=True)
wbm.worksheets(1).Range("B2").Value = _
(application.countif(wb1.worksheets(1).Range("A2:A8"), ">50") + _
application.countif(wb2.worksheets(1).Range("A2:A8"), ">50") + _
application.countif(wb3.worksheets(1).Range("A2:A8"), ">50")) / _
wbm.worksheets(1).Range("A2").Value
wb1.Close savechanges:=False
wb2.Close savechanges:=False
wb3.Close savechanges:=False
End Sub
假设wbm.worksheets(1).Range(“ A2”)。Value不为零或#DIV / 0!错误。
请注意那些“智能引号”,它们在代码编辑器中无效。