Dim arr As Variant
arr = Array("1001", "1201", "1571", "1701", "2102")
读取文件
Dim wk As Workbook
Dim ws As Worksheet
Set wk = Application.Workbooks.Open(path)
Set ws = wk.Worksheets(1)
'获取sheet最大行数
maxRow = ws.[A500000].End(xlUp).Row
判断一个文件是否存在
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
If objFileSystem.fileExists(path) <> True Then
MsgBox "文件不存在。"
Exit Sub
End If
判断文件夹是否存在,不存在创建
If Dir(desktopPath & "\文件夹名\", vbDirectory) = "" Then
MkDir desktopPath & "\文件夹名\"
End If
判断文件是否存在,存在删除
Dim newFile As String
newFile = desktopPath & "\文件夹\" & "文件名.xlsx"
If objFileSystem.fileExists(newFile) Then
Kill newFile
End If
创建一个新EXCEL文件
Set w = Application.Workbooks.Add
On Error Resume Next
w.SaveAs Filename:=newFile
On Error Resume Next
Sheets("sheet1").Name = "sheet名"
With Application.FileDialog(msoFileDialogFilePicker)
'单选择
.AllowMultiSelect = False
'清除文件过滤器
.Filters.Clear
'添加文件过滤后缀
.Filters.Add "Excel Files", "*.xls;*.xlsx"
.Filters.Add "All Files", "*.*"
'FileDialog 对象的 Show 方法显示对话框,并且返回 -1(OK)和 0(Cancel)。
If .Show = -1 Then
'得到选择的文件
Sheet1.Cells(3, 3) = .SelectedItems(1)
End If
End With