VBA:复制范围。 运行时间错误9 – 下标超出范围

我是VBA新手。 我正试图将列从一个工作簿复制到另一个工作簿。 下面是我试图使用的子,但得到“运行时错误9 – 下标超出范围”的错误。 有什么build议么?

Sub copydata(wbSource As String, wsSource As String, rangeSource As String, wbDest As String, wsDest As String, rangeDest As String) Workbooks(wbSource).Worksheets(wsSource).Range(rangeSource).copy Destination:=Workbooks(wbDest).Worksheets(wsDest).Range(rangeDest) End Sub Sub result() ' I also tried to set wsSource and wsDest to 1 but still doesn't work Call copydata("es.csv", "es", "A:B", "Workbook1.xlsm", "result", "A:B") End Sub 

谢谢

编辑:他们在同一个目录。 我在Workbook1.xlsm中创build该模块

在这里输入图像说明

他们必须在复制时都是开放的,他们都在同一个文件夹中并不重要。

你应该打开它们,然后你可以复制数据。

检查两个工作表名称(es for es.csv,Workbook1.xslm的结果)

 Sub copydata(wbSource As String, wsSource As String, rangeSource As String, wbDest As String, wsDest As String, rangeDest As String) Workbooks(wbSource).Worksheets(wsSource).Range(rangeSource).Copy Destination:=Workbooks(wbDest).Worksheets(wsDest).Range(rangeDest) End Sub Sub result() ' I also tried to set wsSource and wsDest to 1 but still doesn't work If Not IsWorkbookOpen("es.csv") Then OpenWorkbook ("es.csv") End If Call copydata("es.csv", "es", "A:B", "Workbook1.xlsm", "result", "A:B") End Sub Sub OpenWorkbook(fileName As String) Dim activePath As String activePath = Application.ActiveWorkbook.Path Application.Workbooks.Open (activePath & Application.PathSeparator & fileName) End Sub Private Function IsWorkbookOpen(wbName As String) As Boolean Dim wb As Workbook On Error GoTo Handler Set wb = Workbooks(wbName) If wb.Name = wbName Then Set wb = Nothing IsWorkbookOpen = True Else Handler: IsWorkbookOpen = False End If End Function