2011-03-17 73 views
5

我正在尋找一種方法來複制一系列單元格,但僅複製包含值的單元格。複製一系列單元格並僅選擇包含數據的單元格

在我的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列,並以相同的方式繼續。

+0

注:您的第一個代碼示例將在範圍A1的所有內容複製:C67,不是A1:C50 ... – 2011-03-18 07:16:02

+1

你的問題在他們被回答後不斷移動,這有點激動人心...... – 2011-03-18 10:33:58

+0

對不起:)但你已經回答了他們所有的問題,我感謝你。 – CustomX 2011-03-18 13:21:19

回答

5

由於您的三列有不同的大小,最安全的做法是逐個複製它們。 PasteSpecial的任何快捷方式都可能最終導致您頭痛。

With Range("A1") 
    Range(.Cells(1, 1), .End(xlDown)).Copy myDestinationRangeA 
End With 

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 myDestinationRangeB 
    EndIf 
End With 

With Range("C1") 
    Range(.Cells(1, 1), .End(xlDown)).Copy myDestinationRangeC 
End With 

現在這是醜陋的,和更清潔的選擇是遍歷列,特別是如果你有很多欄目,你就以相同的順序將其粘貼到相鄰列。

Sub CopyStuff() 

    Dim iCol As Long 

    ' Loop through columns 
    For iCol = 1 To 3 ' or however many columns you have 
     With Worksheets("Sheet1").Columns(iCol) 
      ' Check that column is not empty. 
      If .Cells(1, 1).Value = "" Then 
       'Nothing in this column. 
       'Do nothing. 
      Else 
       ' Copy the column to the destination 
       Range(.Cells(1, 1), .End(xlDown)).Copy _ 
        Destination:=Worksheets("Sheet2").Columns(iCol).Cells(1, 1) 
      End If 
     End With 
    Next iCol 

End Sub 

編輯 那麼,你改變你的問題......試圖通過單個細胞循環,檢查當前單元格是空的,如果不是複製。沒有測試過這一點,但你的想法:

iMaxRow = 5000 ' or whatever the max is. 
    'Don't make too large because this will slow down your code. 

    ' Loop through columns and rows 
    For iCol = 1 To 3 ' or however many columns you have 
     For iRow = 1 To iMaxRow 

     With Worksheets("Sheet1").Cells(iRow,iCol) 
      ' Check that cell is not empty. 
      If .Value = "" Then 
       'Nothing in this cell. 
       'Do nothing. 
      Else 
       ' Copy the cell to the destination 
       .Copy Destination:=Worksheets("Sheet2").cells(iRow,iCol) 
      End If 
     End With 

     Next iRow 
    Next iCol 

該代碼就會在iMaxRow很大很慢。我的直覺是你試圖以某種低效率的方式解決問題......當問題不斷變化時,要解決最佳策略有點困難。

+1

這幾乎是完美的吉恩,但是一旦他看到一個空單元格,他就會前往下一列,而這個空單元格還有值。用現狀編輯主要問題。 – CustomX 2011-03-18 09:21:06

+0

我知道,但這比現在更適合。我知道我的代碼不是乾淨的或者是最優的,但只要它做了它需要做的事情,我會很高興:P你的編輯應該工作,看起來好像它將循環遍歷整個列直到預定義值。非常感謝您的幫助:) – CustomX 2011-03-18 11:10:43

+0

只需添加到此 - 如果您不希望它將空白單元格複製到Sheet 2目標中,請創建一個只在「else」子句中增加的新行計數器,並且告訴'Cells(newRowCounter,column)'只粘貼給定的newRowCounter值。 – Growler 2014-03-14 18:49:56

2

看看貼的特殊功能。有一個'空白'屬性可以幫助你。

+0

好吧,謝謝你,我會給你一個嘗試:) – CustomX 2011-03-17 12:30:54

+1

小心:PasteSpecial強迫你使用剪貼板,所有的隱含意味着,根據我以前的警告... http://stackoverflow.com/questions/ 5327265/excel-macro-copy-until-the-piece-piece-data/5336542#5336542 – 2011-03-18 07:18:32

+0

好點,讓!儘管如此,取決於範圍的大小(特別是巨大的範圍),我相信使用PasteSpecial更好,而不是掃描範圍內的每個單元格,對吧? – 2011-03-18 10:47:04

相關問題