如果单元格为空,则Excel VBA复制行;如果删除值,则删除

时间:2016-12-15 21:16:38

标签: excel vba

如果列D不是空白到工作簿中的其他工作表,我有一个vba代码来复制行(A到G)。它工作得很好。但是,如果从列中删除了值,我需要它从电子表格中删除复制到的行。

因此,用户将在D列的单元格中放入一个值。该行将复制到另一个电子表格中。但如果他们改变主意并删除该值,我需要从其他电子表格中删除该行。

以下是我所拥有的:

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim sIn As Worksheet, sOut As Worksheet, rIn As Range, rOut As Range
    Dim inputdata() As Variant
    Dim tmpArr(1 To 7) As Variant
    Dim i As Long, outcount As Long

   Set sIn = Sheets("Supply Request")
   Set sOut = Sheets("Order")
   Set rIn = sIn.UsedRange
   Set rOut = sOut.Range("A5:G5")

   inputdata = rIn.Value
   outcount = 0

   For i = 1 To UBound(inputdata, 1)
       If inputdata(i, 4) <> "" Then
           outcount = outcount + 1
           tmpArr(1) = inputdata(i, 1)
           tmpArr(2) = inputdata(i, 2)
           tmpArr(3) = inputdata(i, 3)
           tmpArr(4) = inputdata(i, 4)
           tmpArr(5) = inputdata(i, 5)
           tmpArr(6) = inputdata(i, 6)
           tmpArr(7) = inputdata(i, 7)
           rOut.Offset(outcount - 1, 0).Value = tmpArr
           Erase tmpArr
       End If
   Next i
   Erase inputdata
End Sub

0 个答案:

没有答案