数组问题#N / A

时间:2018-03-09 18:03:31

标签: excel vba excel-vba

我在使用数组公式更新数据时遇到错误。

我正在录制一个宏来复制到表格下面并粘贴在“sheet2”中作为链接两次,一个用于实际,另一个用于根据我的宏预测值。根据我的宏,我已经将表格单元格和标题分别复制到“sheet2”,因为它们不能将一个表链接到另一个表,所以我必须分开并这样做。

Name    V1  V3   V3 V4
Wood    10  10  10  10
wood    28  28  28  28
tree    30  45  60  68
plastic 50  50  50  50
tree    50  50  50  50
iron    64  75  75  80
table   20  25  0   30
plastic 54  35  21  0

当我使用宏复制并粘贴表格时,它会根据记录的宏将更新的单元格作为其选择整个范围,这很好,但问题/问题是每当我复制第二次实际值时如下表所示,我正在应用连接公式,如果条件使用数组公式,它不采用新范围,则采用宏中记录的范围并将错误抛出为#N / A.

下面是实际值表,其中我只复制除标题之外的单元格,因此我可以在整个实际和预测表范围内创建一个数据透视表,并且只获得一个标题,以避免数据透视表占用标题实际表作为值,我必须这样做。

Wood - A        25   25   25   25
wood - A        50   50   50   50
tree - A        50   50   100  100
plastic - A     100  100  100  100
tree - A        100  100  100  100
iron - A        100  100  100  100
#N/A            #N/A #N/A #N/A #N/A
#N/A            #N/A #N/A #N/A #N/A

那么,我怎样才能让我的下面的vba进行编辑,并要求它在每次添加/删除新条目时采用新范围。

录制的VBA: 根据下面的vba,我选择了连接公式的范围是“A2:A7”,如果条件范围是“B2:E7”,但我不明白为什么vba显示不同的值,我无法理解在R&下进行。

Sub test1a()
    Sheets("Sheet1").Select
    Range("Table4").Select
    Range("C4").Activate
    Selection.copy
    Sheets("Sheet2").Select
    Range("A2").Select
    ActiveSheet.Paste link:=True
    Sheets("Sheet1").Select
    Range("Table4[[#Headers],[Name]]").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.copy
    Sheets("Sheet2").Select
    Range("A1").Select
    ActiveSheet.Paste link:=True
    Selection.copy
    Range("A2").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.copy
    Range("A20").Select
    ActiveSheet.Paste link:=True
    Range("A20").Select
    Range(Selection, Selection.End(xlDown)).Select
    Application.CutCopyMode = False
    Selection.FormulaArray = "=CONCATENATE(R[-18]C:R[-13]C,"" - A "")"
    Range("B20").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.FormulaArray = _
        "=VALUE(IF(R[-18]C:R[-13]C[3]<25,""25"",IF(R[-18]C:R[-13]C[3]<50,""50"",""100"")))"

End Sub

由于

1 个答案:

答案 0 :(得分:0)

You can do the following, without adding formulas and just run to update when more rows added

Note: You can change destRange = ws.Range("A" & lastRow + 2) to somewhere else if you want the projected data put in a different place.

Option Explicit

Sub GetRows()

Dim wb As Workbook
Dim ws As Worksheet
Dim lastRow As Long
Dim sourceData As Range

Set wb = ThisWorkbook
Set ws = wb.Worksheets("Sheet2") 'change
lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row

Set sourceData = ws.Range("A1:E" & lastRow)

Dim outputArray()

outputArray = sourceData.Value2

Dim i As Long

For i = 2 To UBound(outputArray, 1)

    outputArray(i, 1) = outputArray(i, 1) & " - A"

    Dim y As Long

        For y = 2 To UBound(outputArray, 2)

          Select Case outputArray(i, y)

              Case Is < 25
                  outputArray(i, y) = 25
              Case Is < 50
                 outputArray(i, y) = 50
              Case Else
                  outputArray(i, y) = 100
          End Select

        Next y

Next i

Dim destRange As Range

Set destRange = ws.Range("A" & lastRow + 2)

destRange.Resize(UBound(outputArray, 1), UBound(outputArray, 2)) = outputArray

End Sub