2016-03-07 23 views
-1

我有一個Excel表格。創建Excel表格的單列(不包括零)

而且我需要將它的數據逐行寫入到一個列中,每一行從左到右都是「讀取」,不包括零。請看圖片更好地瞭解:

What I have and what I need to get

有沒有辦法做到這一點快速使用VBA?我嘗試過只使用公式,它的工作,但它需要幾個步驟(創建一個列,不包括零,重寫列......),並真正放慢整個過程。

回答

1

爲了所有的非空值從一個範圍複製到單個列:

Dim source(), arr(), r&, c&, i& 

' read the data from the range 
source = [A1:G3].Value2 

' copy the non empty value 
ReDim arr(1 To UBound(source, 1) * UBound(source, 2), 1 To 1) 
For r = 1 To UBound(source, 1) 
    For c = 1 To UBound(source, 2) 
    If source(r, c) <> Empty Then 
     i = i + 1 
     arr(i, 1) = source(r, c) 
    End If 
    Next 
Next 

' write the data back to the sheet 
[A7].Resize(i, 1) = arr