首页 文章

Excel数组公式#N / A错误

提问于
浏览
0

我正在使用excel数组函数来选择范围并根据参考单元格应用公式 . 但是,每次添加或删除值时,我都可以使用宏复制粘贴引用,但数组公式不会更新范围以重新选择新范围 .

这是我在顶部和底部表格上的主要表格我想要一个已经实现的数组公式,但是当使用vba进行更新时,不通过更新公式中的范围来添加/删除新条目 .

No. Name    V1  V3  V3  V4
1   Wood    10  10  10  10
2   wood    28  28  28  28
3   tree    30  45  60  68
4   plastic 50  50  50  50
5   tree    50  50  50  50
6   iron    64  75  75  80


No. Name        V1  V3  V3  V4
1   Wood - A    25  25  25  25
2   Wood - A    50  50  50  50
3   tree - A    50  50  75  75
4   plastic - A 75  75  75  75
5   tree - A    75  75  75  75
6   iron - A    75  100 100 100

第一个公式:名称列

=concatenate(A1:A6," - A")
  • Ctrl shift enter - 在右表名列中给出了我需要的东西 .

第二个公式: Value 变化

=value(if(C1:F6<25,"25",if(C1:F6<50,"50",if(C1:F6<75,"75","100"))))

我用这个公式分配实际值,左表中的值是预测值 . 我甚至可以使用数组“ctrl shift enter”实现这一点 .

问题:但问题是,每当我通过添加新的条目(如A7,A8,A9)更新表格时使用vba应用公式时,它不会将新范围作为A1:A9(A1:A6)作为第一个公式而C1:F9 (C1:C6)为第二个公式,但是用括号中的旧范围表示 . 因为我的错误就像#N / A一样,因为它没有采用新的范围,所以公式无法理解剩余单元格中的内容 .

1 回答

  • 0

    从...开始:

    enter image description here

    把它放在工作表的代码表中 .

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Not Intersect(Target, Range("A:F")) Is Nothing Then
            On Error GoTo meh
            Application.EnableEvents = False
            Dim t As Range, tr As Long, v As Long
            For Each t In Intersect(Target, Range("A:F"))
                tr = t.Row
                If Cells(tr, "B").Value2 <> vbNullString And _
                  Application.Count(Range(Cells(tr, "A"), Cells(tr, "F"))) = 5 Then
                    Cells(tr, "A").Offset(0, 7) = Cells(tr, "A").Value
                    Cells(tr, "B").Offset(0, 7) = Cells(tr, "B").Value & " - A"
                    For v = 3 To 6
                        Select Case Cells(tr, v).Value
                            Case Is < 25
                                Cells(tr, v).Offset(0, 7) = 25
                            Case Is < 50
                                Cells(tr, v).Offset(0, 7) = 50
                            Case Is < 75
                                Cells(tr, v).Offset(0, 7) = 75
                            Case Else
                                Cells(tr, v).Offset(0, 7) = 100
                        End Select
                    Next v
                End If
            Next t
        End If
    
    meh:
        Application.EnableEvents = True
    End Sub
    

    添加两行后的结果 .

    enter image description here

相关问题