乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>Excel</em>批量读取文件夹下txt文本固定行

<em>Excel</em>批量读取文件夹下txt文本固定行

作者:乔山办公网日期:

返回目录:excel表格制作


'将如下宏代码粘贴到excel的VBA编辑器中,按F5运行即可,注意修改你的txt文件所在的目录。
Sub ReadTxt()
Dim Path As String, MyValue As String, fn As Long
Path = "C:\test" '假定你的24个txt文件处在e799bee5baa6e997aee7ad94e4b893e5b19e330C:\test 文件夹中,可以自行修改
fn = FreeFile
For i = 1 To 24
j = 0
Open "C:\test\" + Format(i, "000") + ".txt" For Input As fn
Do Until EOF(fn)
Line Input #fn, MyValue
j = j + 1
If j = 3 Then Exit Do
Loop
Close #fn
MyValue = Trim(MyValue)
If InStr(MyValue, ",") Then '假设各列的数据分隔符为中文逗号,如果是英文逗号,自行修改为InStr(MyValue, ",")
pos = inst(MyValue, ",")
If pos Then
pos = InStr(pos + 1, MyValue, ",")
If pos Then
pos = InStr(pos + 1, MyValue, ",")
If pos Then
MyValue = Mid(MyValue, pos + 1)
pos = InStr(MyValue, ",")
If pos Then MyValue = Left(MyValue, pos - 1)
End If
End If
End If
ElseIf InStr(MyValue, " ") Then '假设各列的数据分隔符为空格
Do
a = Len(MyValue)
MyValue = Replace(MyValue, " ", "")
Loop While a <> Len(MyValue)
pos = inst(MyValue, " ")
If pos Then
pos = InStr(pos + 1, MyValue, " ")
If pos Then
pos = InStr(pos + 1, MyValue, " ")
If pos Then
MyValue = Mid(MyValue, pos + 1)
pos = InStr(MyValue, " ")
If pos Then MyValue = Left(MyValue, pos - 1)
End If
End If
End If
ElseIf InStr(myvale, vbTab) Then '假设每一行的各列的分隔符为一个Tab制表符
Do
a = Len(MyValue)
MyValue = Replace(MyValue, vbTab + vbTab, vbTab)
Loop While a <> Len(MyValue)
pos = inst(MyValue, vbTab)
If pos Then
pos = InStr(pos + 1, MyValue, vbTab)
If pos Then
pos = InStr(pos + 1, MyValue, vbTab)
If pos Then
MyValue = Mid(MyValue, pos + 1)
pos = InStr(MyValue, vbTab)
If pos Then MyValue = Left(MyValue, pos - 1)
End If
End If
End If
End If
Cells(i, 1) = myvale '将提取的数据填充在第一列的各行中
Next
MsgBox "处理提取完毕!"
End Sub

可以使用如下的代码,复制如下代码到VBA模块中运行即可。
如下代码代表4行4列,如果其余行或列数则可更改相应的数e69da5e887aa7a64332字。

具体步骤如下:
打开office excel;
Alt+F11打开VBA编辑器;
复制如下代码到模块中;
F5运行该模块;
按照提示进行如下步骤即可。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

Sub xxx()
Dim myFile As String
Dim myText As String
Dim myString As String
Dim i As Single
With Application.FileDialog(msoFileDialogFolderPicker)
.Show
.AllowMultiSelect = False
myFile = .SelectedItems(1)
End With
myText = Dir(myFile & "\" & "*.txt")
Do While Len(myText) <> 0
Open myFile & "\" & myText For Input As #1
myText = Split(Split(VBA.StrConv(InputB(LOF(1), 1), vbUnicode), Chr(10))(3), vbTab)(3) '4行4列,如果其余行或列数则更改相应的数字
Debug.Print myText
Close
myText = Dir
Loop
EXCEL读取tet文件只能使用VBA实现读取Txt文件内容

直接采用Open函数+Line input命令读取tet文件
Private Sub txt_read()
Dim txt As String
Open "D:\dzh.txt" For Input As #1 '
'对文件做任何 I/O 操作之前都必须先打开文件。Open 语句分配一个缓冲区供文件进行 I/O 之用,
'并决定缓冲区所使用的访问方式。
'打开文件作为数据输入用,文件号为#1
Do While Not EOF(1)
Line Input #1, txt '从已打开的顺序文件中读出一行并将它分配给 String 变量
'Line Input # 语句一次只从文件中读出一个字符,直到遇到回车符 (Chr(13))
'或回车–换行符 (Chr(13) + Chr(10)) 为止。回车–换行符将被跳过,而不会被附加到字符串上
MsgBox txt
Loop
Close #1
End Sub

@echo off
setlocal enabledelayedexpansion
for /r %%j in (*.txt) do (
for /f "tokens=3" %%i in (%%~nxj) do (
set  /a sum=sum+%%i
)
echo %%~nxj  !sum! 
set /a sum=0
)
pause

给你一个批处理方案:

1、在你txt所在文件夹下新建一个1.bat,保存以上代码到1.bat;

2、双击运行1.bat

运行效果如图;然后复制内容到你的表格。

到表格里后,使用数据-分裂功能就很轻松的分成两列了。


注:或者改一句  echo %%~nxj  !sum! >>求和.txt

结果就会保存到”求和.txt“文件里了

如果不想要 .txt    改成这句代码 echo %%~nj  !sum!

目前我在学Excel vba呐,还不熟练,哈哈

楼上copy 合并txt那个哥们思路也挺好

相关阅读

  • 如何从excel把数据导入到r中

  • 乔山办公网excel表格制作
  • 做分析没有数据真是一大悲剧,正是无源之水、无根之木再加上无米之炊。通常我们搜寻到的数据格式主要有文本格式、Excel格式和SPSS格式。当然也会有从数据库等其它途径进来,这不
  • matalb 怎么读excel中的字符型数据?

  • 乔山办公网excel表格制作
  • [N, T] = xlsread(filename.xls)用这个,zd把excel文件放入工作目录下,默认工作目录是‘work ’文件夹。filename就是excel的名字。用这个函数加载后,N里面就内是数字,T里面就是字符。你试一下
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网