保持行业廉洁贴上非空白行

这是一个困难的Visual Basic问题,所以我不确定这个论坛上的任何人是否能够提供帮助。 但值得一试。

我在Visual Basic中编写了一个程序,用作Excel中的一个macros。

在macros中,我在sheet1(FINAL)中获取数据并将值复制粘贴到sheet2(Data)中。 我在sheet1中的数据范围有很多空白单元格,所以我想创build一个程序,只能粘贴行的值(相对于只有空白单元格的行)。

我的程序现在在粘贴到sheet2之前修改了我在数据表1中的数据范围,我不希望这样…… ..我的格式也因此而被全部搞砸了。 相反,我希望我的sheet1中的数据保持完全相同,在粘贴操作中删除的空行将进入sheet2。

我在sheet1中的数据从列AL开始并进行到列CD。

保持行的完整性是非常重要的。 我不希望在粘贴过程中删除空白单元格,而是要在粘贴过程中删除范围内的空白行。 所以如果在AL和CD列之间有一行甚至只有一个数据点,那么整个行就必须保存在粘贴中。 但是对于列AL和CD之间的任何行,它们都是完全空白的,它们需要在进入sheet2的粘贴操作中被删除。

我现有的程序如下。 任何帮助将不胜感激。

Dim ws As Worksheet Set ws1 = Worksheets("FINAL") Set ws2 = Worksheets("Data") With ws1.UsedRange lastcolumn = .Cells(1, 1).Column + .Columns.Count - 1 lastrow = .Cells(1, 1).Row + .Rows.Count - 1 End With ws1.Range(Cells(1, 38), Cells(lastrow, lastcolumn)).AutoFilter field:=1, Criteria1:="<>" ws1.Range(Cells(1, 38), Cells(lastrow, lastcolumn)).Copy ws2.Range("A1").PasteSpecial xlPasteValues Application.CutCopyMode = False 

这是一个困难的Visual Basic问题,所以我不确定是否有人在这个论坛将能够提供帮助。 但值得一试。

希望这是值得一试:P

这是你正在尝试?

 Sub Sample() Dim wsInput As Worksheet, wsOutput As Worksheet Dim rng As Range, CellsTobeCopied As Range, aCell As Range '~~> Sheet which has range that you want to copy Set wsInput = ThisWorkbook.Sheets("Sheet1") '~~> Set range that you would like to copy Set rng = wsInput.Range("A1:E4") '~~> Output Sheet where you want to paste Set wsOutput = ThisWorkbook.Sheets("Sheet2") For Each aCell In rng.Rows '~~> Check if the entire row is blank If Application.WorksheetFunction.CountA(aCell) <> 0 Then '~~> Construct your range to be copied If CellsTobeCopied Is Nothing Then Set CellsTobeCopied = aCell Else Set CellsTobeCopied = Union(CellsTobeCopied, aCell) End If End If Next '~~> Copy final range If Not CellsTobeCopied Is Nothing Then CellsTobeCopied.Copy '~~> In case you want to preserve formats wsOutput.Range("A1").PasteSpecial xlPasteAll '~~> If you wan tto paste values then comment the above and use this ' CellsTobeCopied.Copy wsOutput.Range("A1") End If End Sub 

截图

在这里输入图像说明