1. vba导入txt数据
代码示例:
Dim arr(1000, 1) As String, tmp As String
Dim i As Integer, j As Integer, s As Integer
ListBox1.Clear
Open path For Input As #1 '打开文件
Do While Not EOF(1) '循环到文件尾
Line Input #1, tmp '读1行
i = i + 1 'i表示数组的第几行
For j = 0 To 1 'j表示数组的第几列
arr(i, j) = Split(tmp, "=")(j)
Next
' s = UBound(arr) '获取数组下标
Loop
Close #1 '关闭文件
Me.ListBox1.List() = arr
2. vba读取txt文件
如果是二进制文件,那得先知道内容的格式,然后才能读取并解析出你想要的数据。
3. vba读取txt文件到excel
新建一个excel工作薄,打开VBA编辑器,插入一个用户窗体,在窗体中放一个textbox,两个commandbutton,然后打开窗体代码窗口粘贴以下代码 Private Sub CommandButton1_Click()
4. vba导入txt文件
在你的窗体上面加一个命令按钮,然后双击这个命令按钮进入后把下面代码粘贴上去就可以了。这里我只写了如何把数据写到文本里,没有对文本框里的输入数据进行前期格式限制,所以你可以输入任何形式的数据。
Private Sub cb1_Click()Dim myDate$, thisPath$, fname$, fp$fname = "日期.txt"myDate = TextBox1.Text & "-" & TextBox2.Text & "-" & TextBox3.TextthisPath = ThisWorkbook.Path & "\"fp = thisPath & fnameIf Dir(fp) = "" Then Open fp For Output As #1 Else Open fp For Append As #1Write #1, myDateClose #1End Sub
5. vba 读取txt
建一个文件夹,把目标txt文件和excel文件放入其中,在excel的宏编辑器中写入以下代码,有些地方可根据你的实际情况做相应改动:
Sub import_from_txt()
Dim file_name As String, my_path As String
Dim lines, cols
Dim i As Integer, j As Integer, k As Integer, q As Integer
Application.ScreenUpdating = False
Worksheets("Sheet1").Range("A1:Z65536").ClearContents
my_path = ThisWorkbook.Path
file_name = "test.txt"
'读取文件
Open my_path & "\" & file_name For Input As #1
lines = Split(StrConv(InputB(LOF(1), 1), vbUnicode), vbCrLf)
Close #1
k = UBound(lines) + 1 '文件的行数
'遍历每一行
For i = 1 To k
cols = Split(lines(i - 1), ",") '以逗号作为分隔,将每行字符分割,分隔符可根据实际情况自己修改
q = UBound(cols) + 1 '分隔成的列数
For j = 1 To q '遍历该行的每一列
Worksheets("Sheet1").Cells(i, j) = cols(j - 1) '输出到表格中
Next
Next
MsgBox ("文件" & file_name & "读取完成,共" & k & "行")
Application.ScreenUpdating = True
End Sub
6. vba整理txt文件中数据
如果你是直接在VBA中输入CMD命令打开“C:\a a.txt”,可以这样:
Set wshShell = CreateObject("WScript.Shell")wshShell.Run "%Comspec% /c " & """" & "C:\a a.txt" & """"如果是在VBA中打开一个BAT文件,BAT文件中是打开“C:\a a.txt”,那么VBA中命令为:
Set wshShell = CreateObject("WScript.Shell")wshShell.Run "C:\open.bat",0而“C:\open.bat”中的内容为:start "c:\a a.txt"(或直接写:"c:\a a.txt",注意加引号)
7. Excel记录用vba导出到txt
Excel每行数据导出成txt.文件的VBA代码:
步骤一 设置宏先把加载宏安全性设为中或低
步骤二 插入模块alt+f11打开VBA编辑器
打开 菜单栏》插入》模块,把下面代码加进去
1
2
3
4
5
6
7
8
9
10
11
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 1For 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:\改成你要的目录,但是目录不能有中文字符
8. vba 导出数据到txt
PublicFunctionopenfile(ByValfilepathAsString)AsString'读入文件函数
DimsAsString
OpenfilepathForInputAs#1
WhileNotEOF(1)
LineInput#1,sline
s=s&sline&vbCrLf
Wend
Close#1
openfile=s
EndFunction
PrivateSubCommand1_Click()
DimvStr
vStr=Split(openfile("C:\test.txt"),vbCrLf)
IfUBound(vStr)<>0Then
MsgBox"第8行数据是"&vStr(8)'这个8自己定义
EndIf
EndSub
当然这段代码也有局限性在读取小文本时就有用如果是过50KB的文本估计程序会down掉
那你可以使用类似
OpenfilepathForInputAs#1
DoWhileNotEOF(1)
LineInput#1,Stream
的方法来流式读取
9. vba导入txt数据报错内存溢出
'在下面的代码中,tf 是由 FileSystemObject 的 OpenTextFile 方法返回的 TextStream 对象:Const ForReading =
1, ForWriting =
2, ForAppending = 3Dim fs,tf,strSet fs = CreateObject("Scripting.FileSystemObject")Set tf = fs.OpenTextFile("c:\testfile.txt", ForReading, TristateFalse)'跳到指定字符数,这里指定10个字符。tf.Skip(10)'把指定数量的字符读到字符串,这里指定20个字符。str = tf.Read(20)tf.Close
- 相关评论
- 我要评论
-