Sub t()
Dim fso, f, f1, fc, s, r
Const ForReading = 1, ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
'Set fc = fso.GetFile(WScript.ScriptFullName).ParentFolder.Files
Set fc = fso.GetFolder("c:\windows").Files '使用時把c:\windows改成實際的資料夾
L = 1
For Each f1 In fc
EXTName = UCase(fso.GetExtensionName(f1.Name))
If EXTName = "TXT" Then
Set fs = fso.OpenTextFile(f1, ForReading)
fb = fs.ReadAll
If InStr(1, fb, "蘋果") > 0 Then
Cells(L, 1) = f1.Name
Cells(L, 2) = f1.Path
L = L 1
End If
End If
Next
End Sub
步驟一 設定巨集先把載入巨集安全性設為中或低
步驟二 插入模組alt f11開啟VBA編輯器
打開 選單列》插入》模組,把下面程式碼加進去
Sub DaoChu()
Dim I As Integer, J As Long, RW As Long
For I = 1 To ActiveSheet.UsedRange.Columns.Count
Open ThisWorkbook.Path & "\" & Cells(1, I) & ".txt" For Output As 1
For J = 2 To Cells(65536, I).End(3).Row
Print #1, Cells(J, I).Value
Next J
Close 1
Next I
MsgBox "資料匯出完畢!", vbOKOnly,"匯出成功"
End Sub
步驟三 儲存以後關閉EXCEL步驟四 測試再重新開啟excel,按ctrl shift P就完成匯出了,檔案在D磁碟根目錄下
如果想放在其他目錄,可以吧Open “D:\” &中的D:\改成要的目錄,但是目錄不能有中文字元。
Option Explicit '強制變數宣告
Option Base 1 '陣列以1開頭
Private Sub Form_Load()
Dim a(3, 5) As String, tmp As String '定義3x5的陣列與暫存變數
Dim i As Integer, j As Integer '定義暫存變數
Open "C:\Documents and Settings\Administrator\桌面\Test.txt" For Input As #1 '開啟txt檔案
Do While Not EOF(1) '逐行讀取
Line Input #1, tmp '把目前行存到tmp裡
i = i 1 'i表示陣列的第幾行
For j = 1 To 5 'j表示陣列的第幾列
a(i, j) = Split(tmp, "|")(j - 1) '把tmp用」|「符號切割成5段,依序儲存到陣列第i行的5個欄位中
Next
Loop
Close #1
MsgBox a(3, 5) '輸出第3行第5列的字元
End Sub
新建一個excel工作薄,打開VBA編輯器,插入一個使用者窗體,在窗體中放一個textbox,兩個commandbutton,然後打開窗體代碼視窗貼上以下程式碼
Private Sub CommandButton1_Click()
'讀入一個ANSI編碼的文字文件,並顯示在textbox中
With Application.FileDialog(msoFileDialogOpen)
If .Show Then ipath = .SelectedItems(1)
End With
If ipath "" Then
Open ipath For Input As #1
#TextBox1.MultiLine = True
#TextBox1.Value = StrConv(InputB(LOF(1), 1), vbUnicode)
Close #1
End If
End Sub
Private Sub CommandButton2_Click()
'把textbox中的內容寫入一個文字文件,保存目前工作薄所在的目錄
arr = Split(TextBox1.Value, vbCrLf)
ipath = ThisWorkbook.Path & "\" & Left(arr(0), 8) & ".txt"
Open ipath For Output As #1
For i = 0 To UBound(arr)
Print #1, arr(i)
Next
Close #1
MsgBox "文字方塊內容已儲存!,儲存路徑:" & ipath
End Sub
以上是的字串?的詳細內容。更多資訊請關注PHP中文網其他相關文章!