|
Sub ImportDataWithSelection()
Dim SourceWorkbook As Workbook
Dim TargetWorkbook As Workbook
Dim SourceRange As Range
Dim TargetRange As Range
Dim filePath As String
' 弹窗选择工作簿
filePath = Application.GetOpenFilename(FileFilter:="Excel Files,*.xl*;*.xlsx", Title:="选择工作簿")
If filePath <> "" Then
' 打开源工作簿
Set SourceWorkbook = Workbooks.Open(filePath)
Set TargetWorkbook = ThisWorkbook
' 弹窗选择导入区域
Set SourceRange = Application.InputBox(Prompt:="选择导入区域", Type:=8)
If Not SourceRange Is Nothing Then
' 弹窗选择目标区域放置位置
Set TargetRange = Application.InputBox(Prompt:="选择目标放置区域", Type:=8)
If Not TargetRange Is Nothing Then
' 复制选定区域的数据
SourceRange.Copy Destination:=TargetRange
Else
MsgBox "请选择一个有效的目标区域"
End If
Else
MsgBox "请选择一个有效的导入区域"
End If
' 关闭源工作簿,不保存更改
SourceWorkbook.Close SaveChanges:=False
End If
End Sub
|
|