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 SettingsAdministrator桌面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中文网其他相关文章!