VBA SumIfs范围对象

问题描述

我正在尝试建立一个宏,以基于具有2条条件的SumIfs公式创建表。行是条件1,列是条件2。SumIfs的结果是表。我问用户条件1,条件2和表中的总和(StrSheetName)的范围,然后在另一个表中构建结果。

不起作用的部分,我相信它与范围对象有关:

Cells(cellposY,cellPosX).Value = wf.SumIfs(sumRange,Criteria1,Worksheets("SumIfTable").Cells(cellposY,2).Value,Criteria2,Worksheets("SumIfTable").Cells(2,cellPosX).Value)

编辑:

那是我的测试表(输入数据):

这就是我想要做的:

完整代码:

Dim InputRangeCritY As Range
Dim InputRangeCritX As Range
Dim InputSumValue As Range

Dim temp As Variant

Set InputRangeCritY = Application.InputBox(prompt:="Select first criteria/column,would be displayed in Y :",Type:=8)
Set InputRangeCritX = Application.InputBox(prompt:="Select second criteria/column,would be displayed in X :",Type:=8)
Set InputSumValue = Application.InputBox(prompt:="Select sum valued,will be use for the sumif formula :",Type:=8)

InputRangeCritY.Select
Selection.Copy

Dim StrSheetName As String
StrSheetName = ActiveSheet.Name

Sheets.Add.Name = "SumIfTable"
Worksheets("SumIfTable").Activate
Range("B3").Select
ActiveSheet.Paste
Range("B3").CurrentRegion.RemoveDuplicates Columns:=1,Header:=xlNo

Worksheets(StrSheetName).Activate
InputRangeCritX.Select
Selection.Copy

Worksheets("SumIfTable").Activate
Range("D3").Select
ActiveSheet.Paste
Range("D3").CurrentRegion.RemoveDuplicates Columns:=1,Header:=xlNo
Range("D3").Select
Range(Selection,Selection.End(xlDown)).Select
Selection.Copy
Range("C2").Select
Selection.PasteSpecial Paste:=xlPasteAll,Operation:=xlNone,SkipBlanks:=False,Transpose:=True
Range("D3").Select
Range(Selection,Selection.End(xlDown)).Select
Selection.ClearContents

Dim OutputRangeCritY As Range
Dim OutputRangeCritX As Range

Range("B3").Select
Range(Selection,Selection.End(xlDown)).Select
Set OutputRangeCritY = Selection

Range("C2").Select
Range(Selection,Selection.End(xlToRight)).Select
Set OutputRangeCritX = Selection

Dim cellPosX As Long
Dim cellposY As Long
cellPosX = 3
cellposY = 3

Set sumRange = Worksheets(StrSheetName).Range(InputSumValue.Address)
Set Criteria1 = Worksheets(StrSheetName).Range(InputRangeCritY.Address)
Set Criteria2 = Worksheets(StrSheetName).Range(InputRangeCritX.Address)
Dim wf As WorksheetFunction

Dim i As Long
Dim J As Long
J = OutputRangeCritY.Cells.Count

Dim g As Long
Dim H As Long
H = OutputRangeCritX.Cells.Count

For g = 1 To H
    For i = 1 To J
        Cells(cellposY,cellPosX).Value)
        cellposY = cellposY + 1
    Next
    cellPosX = cellPosX + 1
Next

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...