根据if逻辑将多列相乘

时间:2015-09-09 17:56:42

标签: excel vba if-statement

我正在尝试制定vba逻辑,它将根据标题名称将一列中的单元格乘以其他列。例如,如果我有以下数据: enter image description here

在第一列中,我试图将单元格范围A2:A11乘以B2:B11乘以D2:D11。由于A1中的值与D1中的左侧两个字符相同,因此B1与D1中的右侧两个字符相同。在列J中,它将是单元格范围A2:A11乘以E2:E11,依此类推。结果应如下所示: enter image description here

宏试图通过比较单元格范围A1:B1到D1:G1中的单元格值来做到这一点,但我无法弄清楚如何将3个单元格范围相乘。

VBA:

YES

Sub IfThenLogic2() Dim SrchRng1 As Range, cel1 As Range Dim SrchRng2 As Range, cel2 As Range Set SrchRng1 = Sheets("Sheet2").Range("A1:B1") Set SrchRng2 = Sheets("Sheet2").Range("F1:G1") c = 13 For Each cel1 In SrchRng1 For Each cel2 In SrchRng2 If (cel1.Value = Left(cel2.Value, 2) Or cel1.Value = Right(cel2.Value, 2)) Then 'If cel1.Value = cel2.Value Then For r = 2 To 11 Cells(r, c).FormulaR1C1 = "=" & cel1.Offset(r - 1, 0).Address(ReferenceStyle:=xlR1C1) & "*" & cel2.Offset(r - 1, 0).Address(ReferenceStyle:=xlR1C1) & "" Next r c = c + 1 End If Next cel2 Next cel1 End Sub 运算符生成(如预期的那样): enter image description here

是否有更好的方法可以通过不使用OR来实现这一目标? 谢谢你的任何建议!

3 个答案:

答案 0 :(得分:1)

这是一个正常的(非数组)公式。只需将其输入单元格I2并根据需要进行复制:

=INDEX($D2:$G2,MATCH(I$1,$D$1:$G$1,))    *    IFERROR(INDEX($A2:$B2,MATCH(LEFT(I$1,2),$A$1:$B$1,)),1)   *    IFERROR(INDEX($A2:$B2,MATCH(RIGHT(I$1,2),$A$1:$B$1,)),1)

如果您想要自动化,可以使用此VBA:

Public Sub excelhero()
    [i2].Formula = "=INDEX($D2:$G2,MATCH(I$1,$D$1:$G$1,))    *    IFERROR(INDEX($A2:$B2,MATCH(LEFT(I$1,2),$A$1:$B$1,)),1)   *    IFERROR(INDEX($A2:$B2,MATCH(RIGHT(I$1,2),$A$1:$B$1,)),1)"
    [i2].Copy [i2:L11]
    [i2:L11].Value = [i2:L1].Value   '<-- replaces formulas with literal values
End Sub

答案 1 :(得分:1)

如果你想在VBA中使用它:

Sub IfThenLogic2()
Dim SrchRng1 As Range, cel1 As Range
Dim SrchRng2 As Range, cel2 As Range
Dim i As Integer
Dim sht As Worksheet
Set sht = Sheets("Sheet2")
Set SrchRng1 = sht.Range("A1:B1")
Set SrchRng2 = sht.Range("D1:G1")


Dim title() As String
c = 13

For Each cel1 In SrchRng1

    For Each cel2 In SrchRng2
    title = Split(cel2, "_")
        For i = LBound(title) To UBound(title)
            If title(i) = cel1.Value Then
            'If cel1.Value = cel2.Value Then
            For r = 2 To 11
                If sht.Cells(r, c).HasFormula Then
                    sht.Cells(r, c).Formula = sht.Cells(r, c).Formula & "*" & cel1.Offset(r - 1, 0).Address
                Else
                    sht.Cells(r, c).Formula = "=" & cel1.Offset(r - 1, 0).Address & "*" & cel2.Offset(r - 1, 0).Address
                End If
            Next r

            End If
        Next i
        c = c + 1
    Next cel2
    c = 13
Next cel1


End Sub

答案 2 :(得分:0)

如果你不介意公式我得到了一个有效的数组公式。

以下是数据的布局方式(如果公式不同,您可能需要调整公式):

enter image description here

放入I2的公式是: =IF(AND(COUNTIF($A$1:$G$1,LEFT(I$1,2)),COUNTIF($A$1:$G$1,RIGHT(I$1,2))),INDEX($A2:$G2,,MATCH(LEFT(I$1,2),$A$1:$B$1,0))*INDEX($A2:$G2,,MATCH(RIGHT(I$1,2),$A$1:$B$1,0))*INDEX($A2:$G2,,MATCH(I$1,$A$1:$G$1,0)),INDEX($A2:$G2,,MATCH(RIGHT(I$1,2),$A$1:$B$1,0))*INDEX($A2:$G2,,MATCH(I$1,$A$1:$G$1,0)))

并与CTRL+SHIFT+ENTER确认。点击ENTER

这基本上是三个索引/匹配公式相乘。索引/匹配使用标题的左右两个字符来查找具有相应编号的列。

PS:只为其他人,这里是文本形式的信息表(所以你可以复制/粘贴)。括号代表一个空列,将其他列分开。

A1  A5  []  A1_A5   A3_A1   A3_A5   A9_A5   []  A1_A5   A3_A1   A3_A5   A9_A5
16  16      61      17      38      46                  
41  6       6       58      22      95                  
39  38      19      37      92      14                  
98  99      64      74      53      88                  
14  35      92      47      34      58                  
18  76      93      20      54      25                  
94  47      78      37      96      44                  
32  25      61      61      66      39                  
9   74      31      43      75      22                  
8   19      14      83      9       89