乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > 怎么把多个txt文本的内容,<em>批量导入</em>到<em>excel</

怎么把多个txt文本的内容,<em>批量导入</em>到<em>excel</

作者:乔山办公网日期:

返回目录:excel表格制作


Public Sub dbhb()
Dim FilesToOpen
Dim x As Integer
'Sheets.Add.Name = "空表"

On Error GoTo ErrHandler
Application.ScreenUpdating = False

FilesToOpen = Application.GetOpenFilename _
(FileFilter:="MicroSoft Excel文件(*.txt),*.txt", _
MultiSelect:=True, Title:="要合并的文件")

If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "没有选中文件"
GoTo ExitHandler
End If

x = 1
u = 1
While x <= UBound(FilesToOpen)
cd = 1
lj = FilesToOpen(x)
33
If InStr(Right(FilesToOpen(x), cd), "\") Then
GoTo 44
Else
cd = cd + 1
GoTo 33
End If
44

mz = Mid(FilesToOpen(x), (Len(FilesToOpen(x)) - cd + 2), cd - 5) & u

sr = "TEXT;" & lj
With ActiveSheet.QueryTables.Add(Connection:=sr, _
Destination:=Range("A" & u))
.Name = mz
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 936
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = True
.TextFileTabDelimiter = True
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = True
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

x = x + 1
u = Range("A1").End(xlDown).Row

Wend

ExitHandler:
Application.ScreenUpdating = True
Exit Sub

ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
将多个TXT文件导入到一个工作表里。
能直接打开对话框让你选TXT取文件,不足之处在于你选择的第一个文7a64e4b893e5b19e338件将在最后导入。

1、如下图所示,我们在TXT文本内有一些数据内容,想要把他们转到excel表格中。接下来我们先打开excel表格。

2、在表格上面的菜单工具栏中有一项数据选项,我们点击选择它,在数据选项下有一项是获取外部数据选项卡,如下图所示。

3、在获取外部数据选项卡下,其中就有一项是来自文本选项,我们点击选择,接下来就会蹦出来一个文本路径选择的对话框。

4、在对话框中我们点击选择我们要导入数据的文本,点击右下角的导入按钮,就会蹦出来一个文本导入的一个向导。

5、根据向导对话框指示,我们点击右下角的一个下一步按钮,即分隔符的选择,即数据分割为两个单元格的分隔符。

6、在这里是想要把前面的人名和后面的产品名字分割为两个单元格,故分隔符就选择为文本中显示的即空格,如下图所示,左下角还有一个数据预览,如果没问题就点击下一步。

7、在最后一步中,可以根据你自己的数据来进行数据格式的选择,这个根据实际情况来进行选择操作就可以了,设置完成就点击右下角的完成按钮。

8、最后再选择一下数据放置的位置。完成效果图。


用VBA来做.
你要提供txt文件的格式样本,
以及要导入excel文件是一个新建文件还是已有的文件,
.新建文件的文件名有没有特定要求,或已有文件的文件名是什么.

试试下面的代码:

Sub 导入指定行()
    Dim myfile
    Dim f As String, mypath As String
    f = ThisWorkbook.Path & "" & "*.txt"
    myfile = Dir(f)
    
    s2 = 1
    Do While myfile <> ""
    mypath = ThisWorkbook.Path & "" & myfile
 '   On Error Resume Next
    Open mypath For Input As #1 '只能导入1号文件,请教如何e5a48de588b6e799bee5baa6e79fa5e98193366导入多个txt文件
    s1 = Split(StrConv(InputB(LOF(1), #1), vbUnicode), vbCrLf)
    ReDim s(0 To UBound(s1))
    For i = 2 To UBound(s1) - 1 '从第二行开始导入,至倒一行结束
    s(i - 2) = s1(i)
    Next
   Cells(1, s2).Resize(UBound(s) + 1, 1) = Application.Transpose(s)
    s2 = s2 + 1
    Close #1
    myfile = Dir
    Loop
 '以下,以空格分列
'  Dim a, y$, t$
'    For i = 2 To [a65536].End(3).Row
'
'       y = Trim(Cells(i, 1))
'
'       For j = 1 To Len(y)
'
'          t = Mid(y, j, 1)
'
'          If t = Space(1) Then y = Replace(y, t & t, t)
'
'       Next
'
'       a = Split(y, Space(1))
'
'       Cells(i, 2).Resize(1, UBound(a) + 1) = a
'    Next
' Columns("A:A").Delete
'
' With Intersect(ActiveSheet.UsedRange, [A:d])
'     .NumberFormatLocal = "G/通用格式"
'     .Value = .Value
' End With
'
' Range("A:H").Columns.AutoFit
End Sub

相关阅读

关键词不能为空
极力推荐

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