Excel 如何将多个文本文件导入到多个工作表中 您所在的位置:网站首页 怪猎世界冰原主线 Excel 如何将多个文本文件导入到多个工作表中

Excel 如何将多个文本文件导入到多个工作表中

#Excel 如何将多个文本文件导入到多个工作表中| 来源: 网络整理| 查看: 265

Excel 如何将多个文本文件导入到多个工作表中

Microsoft Excel在管理和分析大量数据方面的效率备受赞赏。然而,将多个文本文件导入到Excel中的不同工作表中的过程可能一开始看起来令人生畏。幸运的是,Excel本身提供了一个简单而强大的解决方案,可以无缝地实现这一目标。本文将逐步探讨将多个文本文件导入到不同工作表中的技术,以增强组织结构并促进对数据集的有效分析。

一旦掌握了这些方法,您将更好地能够从多个文本文件中导入数据到Excel中,从而在处理大型数据集时节省时间和精力。无论您是在处理来自各个来源的数据还是需要 cons.olidate.txt ,文本文件的信息,Excel的多功能导入能力将使您能够简化工作流程。让我们深入研究在Excel中导入文本文件的世界,发现能够增强您数据管理技巧的实用技术。

使用VBA宏来导入多个文本文件

如果您熟悉Excel VBA (Visual Basic for Applications),您可以利用其编程能力将多个文本文件导入到不同的工作表中。此方法提供了处理特定要求的灵活性和自定义选项。

启用Excel中的VBA请按照以下说明

在功能区上单击右键,选择”自定义功能区”选项。

Excel 如何将多个文本文件导入到多个工作表中

勾选”开发工具”,然后点击”确定”。

Excel 如何将多个文本文件导入到多个工作表中

方法1:使用VBA宏通过选择包含所有所需文本文件的文件夹来导入多个文本文件

如果您熟悉Excel VBA (Visual Basic for Applications),您可以利用其编程能力将多个文本文件导入到不同的工作表中。此方法提供了处理特定要求的灵活性和自定义选项。

步骤1: 打开Excel中的Visual Basic编辑器。您可以按下”Alt+F11″,或导航到功能区中的”开发工具”选项卡,然后选择”Visual Basic”选项。

Excel 如何将多个文本文件导入到多个工作表中

步骤2: 在Visual Basic编辑器中,单击”插入”并选择”模块”以插入一个新模块。

Excel 如何将多个文本文件导入到多个工作表中

步骤3: 在模块中,粘贴以下VBA代码 – Sub LoadPipeDelimitedFiles() 'UpdatebyExtendoffice20181010 Dim xStrPath As String Dim xFileDialog As FileDialog Dim xFile As String Dim xSheetCount As Long Dim xWS As Worksheet Dim xRow As Long ' Added variable for row reference On Error GoTo ErrHandler Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker) xFileDialog.AllowMultiSelect = True xFileDialog.Title = "Select a folder [Kutools for Excel]" If xFileDialog.Show = -1 Then xStrPath = xFileDialog.SelectedItems(1) End If If xStrPath = "" Then Exit Sub ' Ask the user for the number of copies xSheetCount = InputBox("Enter the number of sheets:", "Number of Copies") If Not IsNumeric(xSheetCount) Or xSheetCount < 1 Then MsgBox "Invalid number of copies. Please enter a positive number.", vbExclamation, "Invalid Input" Exit Sub End If Application.ScreenUpdating = False Set xWS = Sheets.Add(After:=Sheets(Sheets.Count)) xWS.Name = "Sheet 1" xRow = 1 ' Start with row 1 xFile = Dir(xStrPath & "\*.txt*") Do While xFile "" With xWS.QueryTables.Add(Connection:="TEXT;" & xStrPath & "" & xFile, Destination:=xWS.Cells(xRow, 1)) ' Update destination range .Name = "a" & xSheetCount .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With xFile = Dir xRow = xRow + 1 ' Increment row reference Loop ' Create copies of the sheet For i = 2 To xSheetCount xWS.Copy After:=Sheets(Sheets.Count) Sheets(Sheets.Count).Name = "Sheet " & i Next i Application.ScreenUpdating = True Exit Sub ErrHandler: MsgBox "No txt files found.", , "Kutools for Excel" End Sub

Excel 如何将多个文本文件导入到多个工作表中

步骤4 - 选择”宏”选项卡。

Excel 如何将多个文本文件导入到多个工作表中

步骤5 - 选择可用的宏并点击运行。

Excel 如何将多个文本文件导入到多个工作表中

步骤6 - 它会打开一个窗口来选择包含文本文件的文件夹。

Excel 如何将多个文本文件导入到多个工作表中

步骤7 - 它会询问你需要的表格数量。输入所需数量并点击确定。

Excel 如何将多个文本文件导入到多个工作表中

Excel将会将数据从文本文件中导入到单独的表格中。

Excel 如何将多个文本文件导入到多个工作表中

方法2:使用VBA宏选择要导入的多个文本文件 步骤1 - 要在Excel中打开Visual Basic编辑器,可以按”Alt + F11″。或者,您可以打开功能区的开发者选项卡,然后选择Visual Basic选项。

Excel 如何将多个文本文件导入到多个工作表中

步骤2 - 在Visual Basic编辑器中,点击”插入”并选择”模块”以插入新的模块。

Excel 如何将多个文本文件导入到多个工作表中

步骤3 - 在模块中,粘贴以下的VBA代码 – Sub LoadPipeDelimitedFiles() 'UpdatebyExtendoffice20181010 Dim xFileDialog As FileDialog Dim xFile As Variant Dim xSheetCount As Long Dim xWS As Worksheet Dim xRow As Long ' Added variable for row reference Dim i As Long ' Added variable for loop counter On Error GoTo ErrHandler Set xFileDialog = Application.FileDialog(msoFileDialogFilePicker) xFileDialog.AllowMultiSelect = True xFileDialog.Title = "Select text files [Kutools for Excel]" xFileDialog.Filters.Clear xFileDialog.Filters.Add "Text Files", "*.txt" If xFileDialog.Show = -1 Then xSheetCount = InputBox("Enter the number of sheets:", "Number of Copies") If Not IsNumeric(xSheetCount) Or xSheetCount < 1 Then MsgBox "Invalid number of copies. Please enter a positive number.", vbExclamation, "Invalid Input" Exit Sub End If Application.ScreenUpdating = False Set xWS = Sheets.Add(After:=Sheets(Sheets.Count)) xWS.Name = "Sheet 1" xRow = 1 ' Start with row 1 For Each xFile In xFileDialog.SelectedItems With xWS.QueryTables.Add(Connection:="TEXT;" & xFile, Destination:=xWS.Cells(xRow, 1)) ' Update destination range .Name = "a" & xSheetCount .FieldNames = True .RowNumbers = False .FillAdjacentFormulas = False .PreserveFormatting = True .RefreshOnFileOpen = False .RefreshStyle = xlInsertDeleteCells .SavePassword = False .SaveData = True .AdjustColumnWidth = True .RefreshPeriod = 0 .TextFilePromptOnRefresh = False .TextFilePlatform = 437 .TextFileStartRow = 1 .TextFileParseType = xlDelimited .TextFileTextQualifier = xlTextQualifierDoubleQuote .TextFileConsecutiveDelimiter = False .TextFileTabDelimiter = False .TextFileSemicolonDelimiter = False .TextFileCommaDelimiter = False .TextFileSpaceDelimiter = False .TextFileOtherDelimiter = "|" .TextFileColumnDataTypes = Array(1, 1, 1) .TextFileTrailingMinusNumbers = True .Refresh BackgroundQuery:=False End With xRow = xRow + 1 ' Increment row reference Next xFile ' Create copies of the sheet For i = 2 To xSheetCount xWS.Copy After:=Sheets(Sheets.Count) Sheets(Sheets.Count).Name = "Sheet " & i Next i Application.ScreenUpdating = True Exit Sub End If ErrHandler: MsgBox "No text files selected.", , "Kutools for Excel" End Sub

Excel 如何将多个文本文件导入到多个工作表中

步骤4 - 选择“宏”选项卡。

Excel 如何将多个文本文件导入到多个工作表中

步骤5 - 选择可用的宏,并点击运行。

Excel 如何将多个文本文件导入到多个工作表中

步骤6 - 它将打开一个文本文件窗口。按下“Ctrl”键并点击您想要的文件选择多个文本文件,然后按“确定”。

Excel 如何将多个文本文件导入到多个工作表中

步骤7 - 它将询问您需要的表格数量。输入所需数量并点击“确定”。

Excel 如何将多个文本文件导入到多个工作表中

Excel将把文本文件中的数据导入到单独的工作表中。

Excel 如何将多个文本文件导入到多个工作表中

结论

在Excel中将多个文本文件导入到多个工作表中是一个非常有效的功能,大大提高了您管理和分析数据的能力。在本文中,我们介绍了两种成功完成此任务的方法。第一种方法利用Excel的Power Query Editor,可以轻松地导入和转换多个文本文件的数据。第二种方法涉及使用VBA宏来自动化导入过程,为特定要求提供自定义选项。

将这些技巧纳入您的Excel工作流程中,可以简化文本文件的导入过程,节省时间和提高数据管理能力。Excel的多功能导入功能使您能够无缝地使用来自各种来源的数据,促进数据分析和决策过程。



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有