Excel 将导入的文件限制为50个vba文件
我有两个sub将文本文件导入excel工作簿。但是,我的代码将导入所有选定的文件。如何修改此代码以限制用户仅选择50个或更少的文件?此外,程序必须通知用户最后导入的文件的名称Excel 将导入的文件限制为50个vba文件,excel,vba,Excel,Vba,我有两个sub将文本文件导入excel工作簿。但是,我的代码将导入所有选定的文件。如何修改此代码以限制用户仅选择50个或更少的文件?此外,程序必须通知用户最后导入的文件的名称 Sub CopyData() Application.ScreenUpdating = False Dim fileDia As fileDialog Dim I As Integer Dim done As Boolean Dim strpathfile As String,
Sub CopyData()
Application.ScreenUpdating = False
Dim fileDia As fileDialog
Dim I As Integer
Dim done As Boolean
Dim strpathfile As String, filename As String
I = 1
done = False
Set fileDia = Application.fileDialog(msoFileDialogFilePicker)
With fileDia
.InitialFileName = "C:\Users\5004239346\Desktop\Subhaac\PD_BACKUP"
.AllowMultiSelect = True
.Filters.Clear
.Title = "Navigate to and select required file."
If .Show = False Then
MsgBox "File not selected to import. Process Terminated"
Exit Sub
End If
Do While Not done
On Error Resume Next
strpathfile = .SelectedItems(I)
On Error GoTo 0
If strpathfile = "" Then
done = True
Else
filename = Mid(strpathfile, InStrRev(strpathfile, "\") + 1, Len(strpathfile) - (InStrRev(strpathfile, "\") + 4))
If Len(filename) > 31 Then filename = Left(filename, 26)
Transfer strpathfile, filename
strpathfile = ""
I = I + 1
End If
Loop
End With
Set fileDia = Nothing
Application.ScreenUpdating = True
WorksheetLoop
End Sub
Sub Transfer(mySource As String, wsName As String)
Dim wbSource As Workbook
Dim wsDestin As Worksheet
Dim lrow As Long
Set wsDestin = ActiveWorkbook.Sheets.Add(, ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count)) 'Add the worksheet at the end
On Error Resume Next
wsDestin.Name = wsName 'set the name
On Error GoTo 0
Application.DisplayAlerts = False
If InStr(wsDestin.Name, "Sheet") <> 0 Then wsDestin.Delete: Exit Sub
Workbooks.OpenText filename:=mySource, _
Origin:=xlWindows, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
Comma:=False, Space:=False, Other:=False, FieldInfo:=Array(1, 1), _
TrailingMinusNumbers:=True
Set wbSource = ActiveWorkbook
With wsDestin
lrow = .Range("A" & Rows.Count).End(xlUp).Row
wbSource.Sheets(1).UsedRange.Copy .Range("A" & lrow).Offset(1, 0)
wbSource.Close False
End With
Application.DisplayAlerts = True
End Sub
Sub-CopyData()
Application.ScreenUpdating=False
Dim fileDia As fileDia对话框
作为整数的Dim I
Dim作为布尔值完成
Dim strpathfile作为字符串,filename作为字符串
I=1
完成=错误
设置fileDia=Application.fileDialog(msoFileDialogFilePicker)
用fileDia
.InitialFileName=“C:\Users\5004239346\Desktop\Subhaac\PD\u备份”
.AllowMultiSelect=True
.过滤器
.Title=“导航到并选择所需文件。”
如果.Show=False,则
MsgBox“未选择要导入的文件。进程已终止”
出口接头
如果结束
未尽而为
出错时继续下一步
strpathfile=.SelectedItems(I)
错误转到0
如果strpathfile=“”,则
完成=正确
其他的
filename=Mid(strpathfile,InStrRev(strpathfile,“\”)+1,Len(strpathfile)-(InStrRev(strpathfile,“\”)+4))
如果Len(filename)>31,则filename=Left(filename,26)
传输strpathfile,文件名
strpathfile=“”
I=I+1
如果结束
环
以
设置fileDia=Nothing
Application.ScreenUpdating=True
工作表环
端接头
子传输(mySource作为字符串,wsName作为字符串)
将wbSource设置为工作簿
将wsDestin设置为工作表
暗淡的光线和长的一样
设置wsDestin=ActiveWorkbook.Sheets.Add(,ActiveWorkbook.Sheets(ActiveWorkbook.Sheets.Count))'在末尾添加工作表
出错时继续下一步
wsDestin.Name=wsName'设置名称
错误转到0
Application.DisplayAlerts=False
如果InStr(wsDestin.Name,“Sheet”)为0,则wsDestin.Delete:Exit Sub
Workbooks.OpenText文件名:=mySource_
原点:=xlWindows,StartRow:=1,数据类型:=xlDelimited,TextQualifier:=_
xlDoubleQuote,连续delimiter:=False,制表符:=True,分号:=False_
逗号:=假,空格:=假,其他:=假,字段信息:=数组(1,1)_
TrailingMinusNumbers:=真
设置wbSource=ActiveWorkbook
与wsDestin
lrow=.Range(“A”和Rows.Count).End(xlUp).Row
wbSource.Sheets(1).UsedRange.Copy.Range(“A”&lrow).Offset(1,0)
wbSource。关闭False
以
Application.DisplayAlerts=True
端接头
AFAIK您不能限制要选择的文件数,但您可以检测所选的文件数并对其进行操作
If fileDia.SelectedItems.Count > 50 then
' User selected more than 50 files
对于第二个问题,最后选择的文件名为
fileDia.SelectedItems(fileDia.SelectedItems.Count)
非常感谢您的帮助!:)我的程序在处理大量文件时会发疯。有没有办法修改上面的代码,使其只循环遍历前50个文件?