|
Sub HONG5()
Dim filePath As String
Dim wbSource As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
' 提示用户选择文件
filePath = Application.GetOpenFilename("Text Files (*.txt), *.txt", , "请选择一个文本文件")
' 如果用户取消选择,则退出子程序
If filePath = "False" Then
MsgBox "未选择文件,操作已取消", vbExclamation, "提示"
Exit Sub
End If
' 打开选定的文件
On Error GoTo ErrorHandler
Set wbSource = Workbooks.Open(filePath)
Set wsSource = wbSource.Sheets(1)
Set wsTarget = ThisWorkbook.Sheets("卷1")
' 复制内容到目标工作表
wsSource.Cells.Copy Destination:=wsTarget.Range("A1")
' 关闭源工作簿,不保存更改
'wbSource.Close SaveChanges:=False
' 提示完成
MsgBox "文件已成功导入", vbInformation, "提示"
Exit Sub
ErrorHandler:
MsgBox "发生错误:" & Err.Description, vbCritical, "错误"
On Error GoTo 0
End Sub
|
|