【发布时间】:2017-10-11 05:11:49
【问题描述】:
我正在寻找一种方法来复制一系列单元格,但只复制包含值的单元格。
在我的 Excel 工作表中,我有从 A1-A18 运行的数据,B 为空,C1-C2。现在我想复制所有包含值的单元格。
With Range("A1")
Range(.Cells(1, 1), .End(xlDown).Cells(50, 3)).Copy
End With
这将复制 A1-C50 中的所有内容,但我只希望复制 A1-A18 和 C1-C2,就好像它们包含数据一样。但它需要以一种方式形成,一旦我在 B 中有数据或我的范围扩展,这些也会被复制。
'So the range could be 5000 and it only selects the data with a value.
With Range("A1")
Range(.Cells(1, 1), .End(xlDown).Cells(5000, 3)).Copy
End With
谢谢!
感谢 Jean,当前代码:
Sub test()
Dim i As Integer
Sheets("Sheet1").Select
i = 1
With Range("A1")
If .Cells(1, 1).Value = "" Then
Else
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Sheet2").Range("A" & i)
x = x + 1
End If
End With
Sheets("Sheet1").Select
x = 1
With Range("B1")
' Column B may be empty. If so, xlDown will return cell C65536
' and whole empty column will be copied... prevent this.
If .Cells(1, 1).Value = "" Then
'Nothing in this column.
'Do nothing.
Else
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Sheet2").Range("B" & i)
x = x + 1
End If
End With
Sheets("Sheet1").Select
x = 1
With Range("C1")
If .Cells(1, 1).Value = "" Then
Else
Range(.Cells(1, 1), .End(xlDown)).Copy Destination:=Sheets("Sheet2").Range("C" & i)
x = x + 1
End If
End With
End Sub
A1 - A5 包含数据,A6 为空白,A7 包含数据。它停在 A6 并前往 B 列,并以相同的方式继续。
【问题讨论】:
-
注意:您的第一个代码示例将复制 A1:C67 范围内的所有内容,而不是 A1:C50...
-
您的问题在得到解答后不断变化,这有点令人沮丧...
-
很抱歉 :) 但你已经回答了所有问题,我感谢你。