需求
某一文件夹下有多个txt
文件,文件里每行是一个内容,类似:
data1.txt
1
2
3
4
5
data2.txt
a
b
c
d
e
最终需要把这些txt
内容复制到excel
里,并存在同一个sheet
的不同列里
编码
Sub AddWorkbook()
'sub表示一个过程
'声明一个变量
Dim isSheetOk As Boolean
'调用一个叫做copyTXT2Sheet的函数
isSheetOk = copyTXT2Sheet()
If Not isSheetOk Then
Exit Sub
End If
' 保存结果
saveResult
End Sub
Function copyTXT2Sheet() As Boolean '函数返回一个布尔值,函数返回值即 函数名=xxx 则表示返回了 xxx
Dim resultName As String
Dim xFile As String
Dim xFileDialog As FileDialog
Dim xToBook As Workbook
Dim xWb As Workbook
Dim xFiles As New Collection
'文件选择弹窗
Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker)
xFileDialog.AllowMultiSelect = False
xFileDialog.Title = "选择包含txt的文件夹"
If xFileDialog.Show = -