Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 尝试将工作表数据复制到现有工作簿时,订阅超出范围_Excel_Vba - Fatal编程技术网

Excel 尝试将工作表数据复制到现有工作簿时,订阅超出范围

Excel 尝试将工作表数据复制到现有工作簿时,订阅超出范围,excel,vba,Excel,Vba,我是Excel VBA新手,尝试将工作表中的数据复制到现有工作簿中,但无法完成。总之,我有一个运行宏的工作簿,在同一个工作簿中有一个源工作表,我需要从中检索记录(A2:H2),然后检查目标工作簿是否存在,如果不创建它,否则它应该将记录复制/插入现有工作簿 VBA宏代码如下所示 Sub process() Dim fName As String Dim fExists As String Dim wb As Excel.Workbook Dim

我是Excel VBA新手,尝试将工作表中的数据复制到现有工作簿中,但无法完成。总之,我有一个运行宏的工作簿,在同一个工作簿中有一个源工作表,我需要从中检索记录
(A2:H2)
,然后检查目标工作簿是否存在,如果不创建它,否则它应该将记录复制/插入现有工作簿

VBA宏代码如下所示

Sub process()

    Dim fName As String
    Dim fExists As String
    
    Dim wb As Excel.Workbook
    
    Dim wsCopy As Worksheet
    Dim wsDest As Worksheet
    'Dim lCopyLastRows As Long
    Dim lDestLastRow As Long
    
    
    fName = "C:\TACs\ResumoTACs_" & Format(Date, "MM-YYYY") & ".xlsx"
    fExists = Dir(fName)
    
    If fExists = "" Then
        '\\ Create a new workbook
        Set wb = Workbooks.Add
        
        '\\ Copy sheet to the new workbook
        ThisWorkbook.Sheets("TAC Data").Copy Before:=wb.Sheets(1)
        
        '\\ Delete unused sheet
        Application.DisplayAlerts = False
        wb.Sheets(2).Delete
        Application.DisplayAlerts = True
        
        '\\ Save new workbook
        wb.SaveAs fileName:=fName, FileFormat:=xlOpenXMLStrictWorkbook
        ActiveWorkbook.Save
        ActiveWorkbook.Close

        MsgBox "New file " & fName & " created!"
    Else
        '\\ Set variables for copy and destinnation sheets
        Set wsCopy = ThisWorkbook.Worksheets("TAC Data")
        Set wsDest = Workbooks(fName) 'Worksheets("TAC Data")
        
        '\\ Find first blank row in the destination range based on data in column B
        lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "B").End(xlUp).Offset(1).Row
        
        '\\ Copy & Paste Data
        wsCopy.Range("A2:H2").Copy _
            wsDest.Range("A" & lDestLastRow)

        MsgBox "The file exists"
    End If

    On Error Resume Next
    Application.Dialogs(xlDialogPrint).Show
    
End Sub

当我尝试运行VBA宏时,在执行代码
Set wsDest=Workbooks(fName)'工作表(“TAC数据”)
时,出现以下错误: 运行时错误“9”: 订阅超出范围

关于这个问题,或者我如何能有效地将工作表数据复制到现有工作簿中,有什么想法吗?

这里有几个问题:

  • 目标工作簿可能已打开,也可能未打开。如果不是,打开它
  • 目标工作簿可能有也可能没有名为“TAC数据”的工作表
  • wsDest是一个工作表,而不是工作簿,因此您需要指定一个工作表(如在注释中)
  • 引用打开的工作簿时,只指定书本名称,不指定路径

  • 下标超出范围表示找不到工作表,但您仍在尝试使用它。如果将
    ThisWorbook.Sheets(“TAC数据”)
    分离到一个单独的
    工作表
    变量中,您将发现该变量未找到,因此无法调用其
    .Copy
    方法。感谢您的帮助。关于如何将工作表中的数据附加到另一个工作簿中,有什么建议吗?
    Dim fPath as String
    '...
    fPath = "C:\TACs\"
    fName = "ResumoTACs_" & Format(Date, "MM-YYYY") & ".xlsx"
    fExists = Dir(fPath & fName)
    '...
    If fExists = "" Then
        '...
    Else
        On Error Resume Next
        Set wbDest = Workbooks(fName) ' now that fName contains only the file name
        On Error GoTo 0
        If wbDest Is Nothing Then
            'Open it
            Set wbDest = Workbooks.Open(fPath & fName)
        End If
    End If
    '...
    On Error Resume Nest
    Set wsDest = wbDest.Worksheets("TAC data")
    On Error GoTo 0
    If wsDest Is Nothing Then
        ' Sheet missing.  What now?
    Else
        '...