根据单元格值插入数组作为模板

时间:2016-04-25 14:42:40

标签: excel excel-formula

我已经构建了一些不同行(相同列)的模板,我希望根据更改单元格值来插入和插入这些模板。

因此,如果您将A1更改为temp1的值,它将插入" temp1"的行/值。来自另一个工作表的模板数组(100行),如果将A101更改为temp2的值,它将插入" temp2"的行/值。来自另一张纸的模板数组(25行)。

1 个答案:

答案 0 :(得分:0)

您要求插入行,而不是清理工作表,然后粘贴行。

附加的Worksheet_Change偶数代码是如何完成您指定的内容的示例...

Private Sub Worksheet_Change(ByVal Target As Range)
Dim srcSht As Worksheet
Dim lstRow As Long, lstCol As Long
Dim srcRng As Range
Dim tarRng As Range
If Target.Address = "$A$1" Then
    Set tarRng = ActiveSheet.Range("A2")
    If Target.Value = "temp1" Then Set srcSht = Worksheets("Shtemp1")
    If Target.Value = "temp2" Then Set srcSht = Worksheets("Shtemp2")
    If srcSht Is Nothing Then Exit Sub
    lstRow = srcSht.Range("A" & srcSht.Rows.Count).End(xlUp).Row
    Set srcRng = srcSht.Range(srcSht.Cells(1, 1), srcSht.Cells(lstRow, 1))
    srcRng.EntireRow.Copy
    tarRng.EntireRow.Insert shift:=xlDown
End If
Set tarRng = Nothing
Set srcRng = Nothing
Set srcSht = Nothing
End Sub

下面是Shtemp1的屏幕截图......

enter image description here

......和Shtemp2的屏幕上限......

enter image description here

当" temp1"输入代码附加到工作表的单元格A1中,你得到这个......

enter image description here

...将Cell A1更改为" temp2",你得到这个......

enter image description here

相关问题