![](/img/trans.png)
[英]Is there a way to run Autofilter to more than one column simultaneously in Excel VBA?
[英]VBA: Export more than one column to txt
我正在嘗試將excel中的幾列數據(其中包含空白單元格)導出到單個 - 沒有空白 - 到.txt文件中。 到目前為止,我已經匯編了一些代碼,我可以成功導出一列。 我希望做的是復制/粘貼之前的一列,依此類推到.txt而不留空格。
Private Sub CommandButton2_Click()
Dim r As Range, c As Range, rng As Range
Dim sTemp As String
Dim UnusedColumn As Range
Dim Filename As String
Filename = ActiveWorkbook.Path & "\xxx.txt"
Open Filename For Output As #1
Set rng = Range("A2:A1000")
'Find a column with nothing in it
Set UnusedColumn = Cells.Find("*", , xlFormulas, , xlByColumns, xlPrevious).EntireColumn.Offset(0, 15)
'Create temporary calculation column to determine which cells to select (marked by an X)
Intersect(rng.EntireRow, UnusedColumn) = Evaluate("IF(" & rng.Address & "="""","""",""X"")")
'Make Selection
Intersect(UnusedColumn.SpecialCells(xlConstants).EntireRow, rng.EntireColumn).Select
'Remove Temporary Blank Caluclations
UnusedColumn.Clear
For Each r In Selection
sTemp = ""
For Each c In r.Cells
sTemp = sTemp & c.Text & Chr(9)
Next c
'Get rid of trailing tabs
While Right(sTemp, 1) = Chr(9)
sTemp = Left(sTemp, Len(sTemp) - 1)
Wend
Print #1, sTemp
Next r
Close #1
End Sub
這里有一些說明可以自動選擇非空白單元格並導出每行沒有引號。
以下是.txt與原始代碼(一列)的相似之處 : http : //i.stack.imgur.com/DiB33.jpg
以下是列的外觀: http : //i.stack.imgur.com/9Tn3m.jpg
對於未經修改的英語和代碼,我們深表歉意。 提前致謝!
當您使用For Each Loop
遍歷數組時,VBA按列迭代每個元素(第1列中的所有元素,然后是第二列中的所有元素......等)。
用2.38秒寫入10,000行×255列,其中%50填充了14個數字存儲為文本
Sub WriteToValues()
Dim Start: Start = Timer
Dim Data, v
Dim FileName As String
FileName = ActiveWorkbook.Path & "\xxx.txt"
Data = ActiveSheet.UsedRange.Value2
Open FileName For Output As #1
For Each v In Data
If v <> vbNullString Then Print #1, CStr(v)
Next
Close #1
Debug.Print Timer - Start
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.