我正在使用
=SumIf(ws!A:A,"Some Text", ws!G:G)
将一些数据添加到新电子表格中。
我想知道是否有办法将A或G转换为1或7,或者使用列索引而不是字母形式给我与Sumif函数具有相同效果的其他一些解决方法。
这对我来说似乎至关重要,因为我的下一个单元格将是:
sumif(ws!A:A, "Some Text", ws!I:I)
sumif(ws!A:A, "Some Text", ws!K:K)
等等。
我希望以两列的间隔循环(如示例所示),但我不知道如何使用范围的字母形式来完成它。一些帮助将不胜感激。
答案 0 :(得分:1)
这是使用VBA:
Sub concious()
Dim i As Integer, j As Integer: j = 0
For i = 0 To 10 Step 2 '<~~ depends how many columns you wish to populate
With Sheet2
Sheet1.Range("A1").Offset(0, j).Formula = _
"=SUMIF(" & .Columns("A:A").Address(, , , True) & _
",""Some Text""," & .Columns("G:G").Offset(0, i).Address(, , , True) & ")"
End With
j = j + 1
Next
End Sub
嗯,Sheet1
是您放置公式的地方,Sheet2
是您的参考表
您可以将Sheet1
替换为Sheets("NameofYourActualSheet")
或您的实际工作表代码。
这是公式版本:
=SUMIF(Sheet2!$A:$A,"Some Text",OFFSET(Sheet2!$G:$G,,(COLUMN()-1)*2))
此部分COLUMN()-1
取决于您希望放置公式的位置
仅当公式在Sheet1的A列中开始时,它才有效。
答案 1 :(得分:1)
编辑:在实现OP后编辑每个不均匀列的公式......
假设“NewSheet”作为要通过引用SumIf()
工作表的ws
函数添加数据的工作表的名称,这里有两个替代代码
“数组”方法
Option Explicit
Sub main()
Dim ws As Worksheet
Dim myArr As Variant
Dim i As Long
Dim formulaStrng As String
Set ws = Worksheets("OldSheet") '<-- change as per your actual "ws" name
formulaStrng = "=SumIf(" & ws.Name & "!C1,""Some Text""," & ws.Name & "!C)"
With Worksheets("NewSheet").Range("A1:NC1")
ReDim myArr(1 To .Columns.Count)
For i = 1 To .Columns.Count
If 2 * Int(i / 2) <> i Then myArr(i) = formulaStrng
Next i
.value = myArr
End With
End Sub
“帮助行”方法
Sub main2()
Dim ws As Worksheet
Set ws = Worksheets("OldSheet")
With Worksheets("NewSheet").Range("A1:NC1")
.FormulaR1C1 = "=SumIf(" & ws.Name & "!C1,""Some Text""," & ws.Name & "!C)"
With .offset(1) '<--| helper row. assuming row 2 is "free". should it not be, simply adjust the offset
.FormulaR1C1 = "=if(2*int(column()/2)<>column(),1,"""")" '<-- mark "uneven" columns with "1"
.value = .value '<-- get rid of formulas
.SpecialCells(xlCellTypeBlanks).EntireColumn.Clear ''<-- clear "even" colums
.ClearContents ''<-- clear helper row
End With
End With
End Sub
两种方法都可以加速,在开头添加以下代码:
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
Application.ScreenUpdating = False
以及以下代码的结尾:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.Calculation = xlCalculationAutomatic
答案 2 :(得分:0)
= SUBSTITUTE(ADDRESS(1,X,4), “1”, “”)及 “:” &安培;替换(ADDRESS(1,X,4), “1”, “”)
x将是您的专栏
然后,您必须将其从文本转换为范围:
= SUMIF(INDIRECT(SUBSTITUTE(ADDRESS(1,X,4), “1”, “”)及 “:” &安培;替换(ADDRESS(1,X,4), “1”, “” )), “B”,A:A)