我正在尝试使用vba为用户按下一个按钮,让他们选择一个.csv工作簿。然后代码将复制和粘贴一个范围到另一个打开的工作簿。这是我目前的代码:
Private Sub CommandButton1_Click()
FileToOpen = Application.GetOpenFilename _
(Title:="Open Report File", _
FileFilter:="Excel Files *.csv (*.csv),")
Dim sourceworkbook As Workbook
Dim currentworkbook As Workbook
Set currentworkbook = ThisWorkbook
Set sourceworkbook = Workbooks(FileToOpen)
sourceworkbook.Worksheets(1).Select.Range("B4:F4").Copy
currentworkbook.Activate
currentworkbook.Worksheets("Sheet1").Activate
currentworkbook.Worksheets("Sheet1").Range("B14:F14").Select
ActiveSheet.Paste
sourceworkbook.Close
Set sourceworkbook = Nothing
Set currentworkbook = Nothing
ThisWorkbook.Activate
Worksheets("Sheet1").Activate
Worksheets("Sheet1").Range("B14").Select
End Sub
但我得到“运行时错误'9”:下标超出范围”在以下步骤:Set sourceworkbook = Workbooks(FileToOpen)
有没有更好的方法来实现这一目标?
1条答案
按热度按时间8cdiaqws1#
从关闭的工作簿导入区域