Sub CopyToAbort()
Dim folder As String
Dim source As String
Dim dest As String
Dim msg1 As String
Dim msg2 As String
Dim p As Integer
Dim s As Integer
Dim i As Long
On Error GoTo ErrorHandler
folder = "C:\Abort"
msg1 = "The selected file is already in this folder."
msg2 = "was copied to"
p = 1
i = 1
' get the name of the file from the user 從用戶處獲取文件名稱
source = Application.GetOpenFilename
' don’t do anything if cancelled 如果取消則不進(jìn)行任何操作
If source = "False" Then Exit Sub
' get the total number of backslash characters "\" in the source 獲取文件來(lái)源字符
串中的反斜杠數(shù)
' variable’s contents
Do Until p = 0
p = InStr(i, source, "\", 1)
If p = 0 Then Exit Do
s = p
i = p + 1
Loop
' create the destination file name 創(chuàng)建目的文件名稱
dest = folder & Mid(source, s, Len(source))
' create a new folder with this name 創(chuàng)建同名文件夾
MkDir folder
' check if the specified file already exists in the 檢查該文件是否在目的地已經(jīng)存
在
' destination folder
If Dir(dest) <> "" Then
MsgBox msg1
Else
' copy the selected file to the C:\Abort folder 復(fù)制所選文件到文件夾“C:\Abort”
更多建議: