2016-02-19 46 views
5

創建變量數組,你可以從一系列創建一個變量數組,像這樣:使用文本而不是價值

Dim x As Variant 

x = Range("A1:Z1").Value 

這顯然放置.Value屬性到數組。我試圖做同樣的事情,但獲得單元格的.Text屬性,但我不認爲這是可能的。

Dim x As Variant 

x = Range("A1:Z1").Text '// <~~ type mismatch 

的原因如下,我有數據行,像這樣:

|------A------|------B------|------C------| 
1| 01-Jan-2003 27-Feb-2005 15-Sep-2015 

我要輸出的行到一個文本文件使用管道分隔符,目前我米使用此:

With WorksheetFunction 
    x = .Transpose(.Transpose(Cells(1, 1).Resize(1, 3).Value)) 
End With 

Print #1, Join(x, "|") 

其中一期工程,但它變得如此的輸出看起來像它的格式爲DD/MM/YYYY的.Value此:

01/01/2003|27/02/2005|15/09/2015 

問:我可以保留而不必解析/迴路的陣列的第一中的每個值使用這種方法的格式化?

+2

使用** For **循環。 –

+0

我試圖避免必須循環通過值,實際上它是一大堆相當大的文件,因此循環會對處理時間產生重大影響。我已經更新了我的問題,以反映這是因爲它不是立即清楚tbf –

回答

2

不是非常優雅,我不確定這會大量工作,但它避免了一個循環。您可以將最終輸出放入一個字符串並替換:

Dim x As Variant, y As String 

x = Range("A1:Z1") 

With WorksheetFunction 
    x = .Transpose(.Transpose(ActiveSheet.Cells(1, 1).Resize(1, 5).Value)) 
End With 

y = Join(x, "|") 

y = Replace(y, "/01/", "-Jan-") 
y = Replace(y, "/02/", "-Feb-") 
y = Replace(y, "/03/", "-Mar-") 
y = Replace(y, "/04/", "-Apr-") 
y = Replace(y, "/05/", "-May-") 
y = Replace(y, "/06/", "-Jun-") 
y = Replace(y, "/07/", "-Jul-") 
y = Replace(y, "/08/", "-Aug-") 
y = Replace(y, "/09/", "-Sep-") 
y = Replace(y, "/10/", "-Oct-") 
y = Replace(y, "/11/", "-Nov-") 
y = Replace(y, "/12/", "-Dec-") 

Debug.print y 
+1

我實際上最終不得不循環,因爲有間歇的空白字符,需要整理'修剪$()',但這肯定是一個可以接受的解決方案,所以我會接受它 –

+1

甚至可以做一個迷你'對於我= 1到12'循環/替換,它仍然會比循環數組中的所有值更快 –

相關問題