3

我正在设置一个电子表格,目的是允许用户按员工和日期输入数据。我想创建一个动态表,该表根据条目扩展到另一个表中。例如,我是否可以根据输入到 TABLE2 的新员工自动扩展 TABLE1,使其看起来像 TABLE3,使 TABLE3 准备好让新用户在“值”列中输入?

表格1

表格1

表 2

表格1

表3

在此处输入图像描述

4

1 回答 1

2

不是最好的做事方式,但这可能会对您有所帮助,或者至少为您指明您正在尝试做的事情的正确方向。

如果表1在Sheet1中,表2在Sheet2中,当向表2中添加新行时,以下代码将复制该名称并将其粘贴三次到Sheet 1的表1中,然后从上面的记录中复制日期,在这种情况下比尔的日期。

将以下代码放在 Worksheet Change 事件的 Sheet 2 下,如下图所示:

在此处输入图像描述

代码:

Private Sub Worksheet_Change(ByVal Target As Range)
LastRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row 'check the last row on Sheet 2 Table 2
LastRow1 = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row + 1 'Check the last row on Sheet1 Table 1

If Target.Row = LastRow Then    'if a new row is added to Table 2
    Sheet1.Cells(LastRow1, Target.Column).Value = Target.Value 'copy their name to table 1 three times
    Sheet1.Cells(LastRow1 + 1, Target.Column).Value = Target.Value
    Sheet1.Cells(LastRow1 + 2, Target.Column).Value = Target.Value

    Sheet1.Cells(LastRow1, 2).Value = Sheet1.Cells(LastRow1, 2).Offset(-3, 0).Value 'copy the Date from the date above on Sheet1
    Sheet1.Cells(LastRow1 + 1, 2).Value = Sheet1.Cells(LastRow1 + 1, 2).Offset(-3, 0).Value
    Sheet1.Cells(LastRow1 + 2, 2).Value = Sheet1.Cells(LastRow1 + 2, 2).Offset(-3, 0).Value
End If
End Sub
于 2017-12-14T10:20:31.073 回答