使用数组粘贴忽略空白单元格

时间:2014-06-28 15:48:44

标签: arrays excel vba excel-vba

感谢本论坛上的一些人,我有这个代码,可以在转置它们的同时将多个单元格从参考活动工作表复制到主工作表。我唯一的问题是有一个范围有空白单元格,我希望函数忽略这些。我尝试将specialpaste函数与skipblanks=true一起使用,但这并没有帮助。有什么建议?

Macro1()
Dim master As Worksheet, source As Worksheet, copyCols() As Variant, pasteCols() As Variant, i As Integer

Set master = Workbooks("Combined Spreadsheet.xlsx").Worksheets(1)
Set source = ActiveSheet
copyCols = Array("F", "H", "N", "R", "S")
pasteCols = Array("B", "L", "V", "AF", "AP")

For i = 0 To UBound(copyCols)
    source.Range(copyCols(i) & "4:" & copyCols(i) & 14).Copy
    master.Range(pasteCols(i) & master.Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlAll, Operation:=xlNone, SkipBlanks:=True, Transpose:=True
    Next i
End Sub

1 个答案:

答案 0 :(得分:0)

通过选择类型为常量的SpecialCells选择非空白单元格,然后粘贴结果,该结果将仅通过所选单元格。

尝试以下代码的代码。

copyCols = Array("D", "E")
pasteCols = Array("H", "I")

For i = 0 To UBound(copyCols)
    Range(copyCols(i) & "4:" & copyCols(i) & 14).SpecialCells(xlCellTypeConstants, 23).Copy
    Range(pasteCols(i) & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial Paste:=xlAll,     Operation:=xlNone, SkipBlanks:=True, Transpose:=True
Next i