麻辣家族討論版版's Archiver

poison1 發表於 2011-7-20 11:07

EXCEL檔案內的儲存格內容如何寫VBA匯出成TXT檔案格式??

例如A1儲存格~A10儲存格裡面內容
如何利用VBA寫一個巨集按鈕將A1~A10儲存格內容匯出成一個TXT檔案呢?
煩請EXCEL達人大大可以寫出一個範例教學嗎?

GBKEE 發表於 2011-7-20 11:26

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=23285&ptid=4143]1#[/url] [i]poison1[/i] [/b]
先錄製巨集看看    另存新檔可存為txt檔

StanleyVic 發表於 2011-7-20 11:27

[i=s] 本帖最後由 StanleyVic 於 2011-7-20 11:35 編輯 [/i]

[attach]7065[/attach]

這是我平時為了方便工作而寫的.
如大大們看了.有不善的地方,也有待大大們的指導..( 本人也耐心受教.謝謝^_^)

Private Sub CommandButton1_Click()
Dim AA, BB, CC, DD, EE, FF, GG As String
Dim r As Integer

Open "D:\test\test.txt" For Output As #1      [color=SeaGreen]'定義Output File位置[/color]
For r = 1 To [A65536].End(xlUp).Row            [color=SeaGreen]'定義行內容範圍[/color]   

[color=SeaGreen]'自由設定( 本人的例子是每個格都有它各自規定的字符長度,所以就建立了公式,同時也為了方便對齊) :[/color]   
    AA = Cells(r, 1) & Application.WorksheetFunction.Rept(" ", 20 - Len(Cells(r, 1)))
    BB = Application.WorksheetFunction.Rept(" ", 3 - Len(Cells(r, 2))) & Cells(r, 2)
    CC = Application.WorksheetFunction.Rept(" ", 13 - Len(Cells(r, 3))) & Cells(r, 3) & ".00"
    DD = Cells(r, 4) & Application.WorksheetFunction.Rept(" ", 10 - Len(Cells(r, 4)))
    EE = Cells(r, 5) & Application.WorksheetFunction.Rept(" ", 8 - Len(Cells(r, 5)))
    FF = Cells(r, 6) & Application.WorksheetFunction.Rept(" ", 10 - Len(Cells(r, 6)))
    GG = Cells(r, 7) & Application.WorksheetFunction.Rept(" ", 10 - Len(Cells(r, 7)))
   
        mystr = AA & BB & CC & DD & EE & FF & GG
        
    Print #1, mystr
   
Next r
   
Close #1

End Sub

StanleyVic 發表於 2011-7-20 11:45

[i=s] 本帖最後由 StanleyVic 於 2011-7-20 12:00 編輯 [/i]

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=23287&ptid=4143]2#[/url] [i]GBKEE[/i] [/b]

    啊.????!!!!! 果然強 !!! 我忘了可以用錄!!!  Y_Y"  ( 版主~~我又學習學習了...)

Sub Macro1()
    ChDir "D:\Test"
    ActiveWorkbook.SaveAs Filename:="D:\Test\Macro.txt", FileFormat:=xlText, _
    CreateBackup:=False
End Sub

e..等一下.等一下..請看看下圖.
為什麼出來的TXT 下面有這麼多空白的地方 ?

[attach]7066[/attach]

(ps: 我的EXCEL 中 己經查看了.只有頭10行有東西,下面的沒有內容!)

poison1 發表於 2011-7-20 11:54

看不懂= =
有更簡單的寫法嗎?

StanleyVic 發表於 2011-7-20 11:59

[i=s] 本帖最後由 StanleyVic 於 2011-7-20 14:43 編輯 [/i]

" GBKEE ~ 版主" 在上面,己經說了可以試一下用 錄製巨集 !
如果不明白的話, 你不介意.可以用我.剛才測試的代碼

Sub Macro1()
    ChDir "D:\Test"
    ActiveWorkbook.SaveAs Filename:="D:\Test\Macro.txt", FileFormat:=xlText, _
    CreateBackup:=False
End Sub

****************************

如果樓主不明白的話.也可以用以這段代碼. 你會容易簡讀

Sub XlsToTxT()
Dim MYstr As String, i As Integer                '定義屬性
Open "D:\test\test.txt" For Output As #1     '定義Output File位置
    For i = 1 To 10                                    '由 Row 1to10
        MYstr = Cells(i, 1)                            '輸出的內容 (或你要的東西,可在此開始自己定義吧~)
        Print #1, MYstr
    Next i
Close #1
End Sub

希望這個兩例子對你幫助 !

7777 發表於 2017-10-26 21:08

[b]回復 [url=http://forum.twbts.com/redirect.php?goto=findpost&pid=23287&ptid=4143]2#[/url] [i]GBKEE[/i] [/b]


[size=4]GBKEE大大

能請教
1.如何將 excel
Worksheets("圖表").Range("L16:AE75").Value   '內容資料
匯出成  資料.txt檔


[size=3][color=Blue]附上檔案
資料檔案       (test.xls)
希望匯出 txt檔 (資料.txt)[/color][/size]
[attach]27870[/attach]
感激不盡

2.先錄製巨集看看
  爬文沒找到,能請教 GBKEE大大
   excel 錄製的時候,看到匯出是 灰色的無法點
   該怎麼 錄製,不懂

能請教一下 感恩[/size]

頁: [1]

麻辣家族討論版版為 麻辣學園 網站成員  由 昱得資訊工作室 © Since 1993 所提供