我正在尋找一種方法來複制一系列單元格,但僅複製包含值的單元格。複製一系列單元格並僅選擇包含數據的單元格
在我的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 ... – 2011-03-18 07:16:02
你的問題在他們被回答後不斷移動,這有點激動人心...... – 2011-03-18 10:33:58
對不起:)但你已經回答了他們所有的問題,我感謝你。 – CustomX 2011-03-18 13:21:19