Excel以动态方式插入字段

时间:2015-07-14 08:16:05

标签: excel vba dynamic

我有一个Excel电子表格,专门用于监控设备上的标志和其他变量,它们显示如下并动态更新:

Flag name    TimeStamp    Value
SomeFlag     05:45:12     0
SomeOther    08:22:23     1
Another      08:22:23     0

我有另一个电子表格,其中提供了所有受监控设备的概述。在此电子表格中,我只想在专用电子表格中显示值为1的标志。因此,对于上面的示例,它将如下所示:

Voltage    09:22:45    230V
Current    09:22:45    15A
Flags:     SomeOther   1

如果要将更多标志变为1,则必须动态添加它们。例如,如果Another标志变为1,则概述将如下所示:

Voltage    09:22:46    230V
Current    09:22:46    15A
Flags:     SomeOther   1
           Another     1

有人可以帮助我吗?我知道如何使用VBA,但我需要一些指导才能开始使用它。

1 个答案:

答案 0 :(得分:0)

以下将数据读入数组

Sub t()
Dim arr()
ReDim arr(1)
i = 0

With ActiveSheet
    lastrow = .Range("A2").End(xlDown).Row
    For Each cell In Range("A2:A" & lastrow)
        If cell.Offset(0, 2).Value = 1 Then
            arr(i) = cell.Value
            arr(i + 1) = cell.Offset(0, 2).Value
            ReDim Preserve arr(UBound(arr) + 2)
            i = i + 2
        End If
    Next
End With

For i = 0 To UBound(arr()) - 2 Step 2
    Debug.Print arr(i) & ":" & arr(i + 1)
    'change debug.print to the range/sheet etc of where you would like the output to start
    'may need to clean output range first
Next
End Sub

输出当前只输出到即时窗口,因此需要更改,但应足以让您前进。在输出数组时请记住步骤2,因为每次迭代需要输入2个值