擦除数组中的空单元格

我在列中粘贴一个数组,问题是它留下列中的一些单元格为空。 我如何擦除列中的单元格?

这是我的:

Private Sub Worksheet_Change(ByVal Target As Range) Worksheets("Info").Range("A1").Select Dim i As Integer Dim iLastRow As Long iLastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "A").End(xlUp).Row Dim arrmatrix() As String ReDim arrmatrix(1 To iLastRow, 1 To 1) For i = 1 To iLastRow Range("A2").Cells(i, 1).Select If Selection.Offset(0, 11) = "Pi emitida" Then arrmatrix(i, 1) = Range("A2").Cells(i, 1).Value End If Next i Worksheets("Inicio").Range("G4:G1000000").ClearContents Worksheets("Inicio").Range("G4").Resize(UBound(arrmatrix, 1)).Value = arrmatrix() end sub 

你的示例代码有几个问题。

  1. 希望这是在信息工作表的代码表上。 您不应该试图从Worksheet_Change事件macros.Activate另一个工作Worksheet_Change
  2. 目前还不清楚为什么在Worksheet_Change事件macros中需要这个。 按原样,只要工作表中任何地方的值被添加/修改/删除,就会运行。 这听起来像是过度杀伤,因为决定结果的唯一两列是A列和L列。
  3. ReDim语句可以与Preserve一起使用来展开数组,但只能重新排列最后一个等级。

这个修改不依赖于在处理它们之前select或激活工作表。 arrmatrixarrays根据需要进行扩展,所以不会在数组中出现空白值。

 Private Sub Worksheet_Change(ByVal Target As Range) If Not Intersect(Target, Range("A:A, L:L")) Is Nothing Then On Error GoTo Fìn Application.EnableEvents = False Dim i As Long, n As Long Dim arrmatrix As Variant ReDim arrmatrix(1 To 1, 1 To 1) For i = 2 To Cells(Rows.Count, 1).End(xlUp).Row If Cells(i, 12).Value = "Pi emitida" Then n = n + 1 ReDim Preserve arrmatrix(1 To 1, 1 To n) arrmatrix(1, n) = Cells(i, 1).Value End If Next i With Worksheets("Inicio") .Range("G4:G" & Rows.Count).ClearContents .Range("G4").Resize(UBound(arrmatrix, 2), 1) = Application.Transpose(arrmatrix) End With End If Fìn: Application.EnableEvents = True End Sub 

这只会在列A或列L中的值被添加/更改/删除时运行。

由于我一直在扩展和填充最后一个等级,我使用Application.Transpose重新定向数据,然后将其填充到Inicio工作表中。