在 VBA 中附加 TXT 文件并在 Excel 中打开

瓦尔泽

新手来了

所以我有十几个看起来像这样的 TXT/DTA 文件,我想将它们并排堆叠。我希望每个文件都附加到右边,合并成一个大文件

数据文件

不太了解 VBA 我环顾四周并合并了一些代码,这些代码似乎对 xlsx 文件执行此操作,但不适用于我所拥有的 DTA 文件。该代码要求一个文件夹并一个一个地循环遍历这些文件。

Sub AllWorkbooks()
   Dim MyFolder As String   'Path collected from the folder picker dialog
   
   Dim MyFile As String 'Filename obtained by DIR function
   Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
   If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
      Exit Sub
   End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore

'---Open the first file only
Workbooks.Open (MyFile)
Workbooks(MyFile).Worksheets("Sheet1").Range("A1").Select
Range(Selection, Selection.End(xlToRight)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy Workbooks("CV Combined.xlsm").Worksheets("Sheet1").Range("A1")
Workbooks(MyFile).Close SaveChanges:=False
MyFile = Dir

Do While MyFile <> ""
   'Opens the file and assigns to the wbk variable for future use
   Set wbk = Workbooks.Open(fileName:=MyFolder & MyFile)
   'Replace the line below with the statements you would want your macro to perform
    Workbooks.Open (MyFile)
    Workbooks(MyFile).Worksheets("Sheet1").Range("A1").Select
    Range(Selection, Selection.End(xlToRight)).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy Workbooks("CV Combined.xlsm").Worksheets("Sheet1").Range("A1").End(xlToRight).Offset(0, 1)
    Workbooks(MyFile).Close SaveChanges:=False

wbk.Close SaveChanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub

任何帮助,将不胜感激。

CDP1802

MyFile = Dir(MyFolder)仅返回MyFileso 中的文件名以打开第一个文件 use Workbooks.Open (MyFolder & MyFile)打开文本文件时,工作表名称是文件名,因此Workbooks(MyFile).Worksheets("Sheet1")需要是Workbooks(MyFile).sheets(1). 因为您的文本文件只有第 1 行 A 列中的数据,所以Selection.End(xlToRight)将转到工作表的最后一列XFD1,然后Selection.End(xlDown)转到最后一行XFD1048576

Option Explicit

Sub AllWorkbooks()

    Dim MyFolder As String   'Path collected from the folder picker dialog
    Dim MyFile As String 'Filename obtained by DIR function
    Dim wbDTA As Workbook 'Used to loop through each workbook
    Dim ws As Worksheet, wsDTA As Worksheet, rng As Range
    Dim iCol As Long, n As Long
    
    'Opens the folder picker dialog to allow user selection
    With Application.FileDialog(msoFileDialogFolderPicker)
        .Title = "Please select a folder"
        .Show
        .AllowMultiSelect = False
        If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
            MsgBox "You did not select a folder"
            Exit Sub
        End If
        MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
    End With

    Set ws = Workbooks("CV Combined.xlsm").Sheets(1)
    iCol = 1

    'Loop through all files in a folder until DIR cannot find anymore
    Application.ScreenUpdating = False
    MyFile = Dir(MyFolder)
    Do While MyFile <> ""
        
        Set wbDTA = Workbooks.Open(MyFolder & MyFile, False, False)
        Set wsDTA = wbDTA.Sheets(1)
        Set rng = wsDTA.UsedRange
        
        rng.Copy ws.Cells(1, iCol)
        iCol = iCol + rng.Columns.Count + 1 ' add blank column
        n = n + 1
        wbDTA.Close SaveChanges:=False
        MyFile = Dir 'DIR gets the next file in the folder
 
    Loop
    Application.ScreenUpdating = True
    MsgBox n & " files imported from " & MyFolder, vbInformation

End Sub

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

VBA Excel宏打开txt文件浏览

打开excel文件并在特定单元格vba中获取值的功能

解析.txt文件并在python中写入excel

Excel VBA 在 .txt 文件中查找/替换文本

如何打开多个excel文件并在DataGridView中打开它们

VBA在PDF XChange Viewer中打开文件导出的Excel文件

在excel中根据变量文件名VBA打开文件

如何在文本文件中查找特定文本,并在excel VBA中的以下行中添加附加文本

Excel VBA代码无法打开名为“ CON.txt”的文件

打开目录中的所有MSProject文件-Excel VBA

如何在Excel VBA中打开“新的PPT文件”选项

PowerPoint VBA:从已经打开的 Excel 文件中获取数据

用VBA代码中的URL打开Excel文件错误

Excel VBA:按模式在子目录中打开文件

EXCEL-VBA 从 txt 文件 VBA 的查找函数中返回最高值

打开txt中列出的文件

在Python中打开'.txt'文件

我如何使用VBA打开Excel文件的附件并在文件中进行验证,然后才能在Outlook中确定的文件夹中保存消息?

循环遍历不同文件中的命名范围并在 VBA Excel 中创建 jpg

Python:在txt文件中写入附加文本

在Python中的标签中打开txt文件

从[excel] [vba]中删除txt导出中的空行

如何使用VBA在txt文件中写入

如何在VBA中激活打开的excel

嵌入图像并将文件附加到通过Excel VBA发送的电子邮件中

VBA Excel 将行导出到 .txt 中的列

如何使用VBA在Excel电子表格中的列表中打开文件夹

在Excel中打开.csv文件,并在.csv文件更改时对其进行更新

在 VBA 中附加多个文件