0

以下代码应该从 xlsx 文件(子文件夹内)复制信息并将所有内容合并(粘贴)到单个文件中:

    Dim xlApp, xlApp1, xlApp2 As Excel.Application
    Dim xlWorkBook, xlWorkBook1, xlWorkBook2 As Excel.Workbook
    Dim xlWorkSheet, xlWorkSheet1, xlWorkSheet2 As Excel.Worksheet
    Dim folder, m, n As String
    Dim subfolders As String()
    Dim i, j, c, k, l, lastrow As Integer
    Dim fec As Date

    folder = My.Application.Info.DirectoryPath
    ChDir(CurDir())
    subfolders = IO.Directory.GetDirectories(CurDir())
    xlApp = New Excel.ApplicationClass
    xlApp2 = New Excel.ApplicationClass
    xlApp1 = New Excel.ApplicationClass

    Try

        xlWorkBook = xlApp.Workbooks.Open("\\excel1.xlsx")
        xlWorkSheet = xlWorkBook.Worksheets("SheetName")
        xlWorkBook2 = xlApp2.Workbooks.Open("\\excel2.xlsx")
        xlWorkSheet2 = xlWorkBook2.Worksheets("SheetName")
        i = 2

        For Each f1 In subfolders
            ChDir(f1)
            m = Dir("*.xlsx")

            Do While m <> ""                   

                    j = 1
                    Do While xlWorkSheet2.Cells(j, 1).Value <> ""
                        If xlWorkSheet2.Cells(j, 1).Value = m Then
                            fec = xlWorkSheet2.Cells(j, 2).value
                            If fec <> File.GetLastWriteTime(CurDir() & "\" & m) Then
                                l = 1
                                n = xlWorkSheet.Cells(l, 3).value()

                                Do While n <> ""

                                    If Trim(xlWorkSheet.Cells(l, 3).value) = Strings.Left(Strings.Right(m, 16), 11) Then
                                        xlWorkBook.Activate()
                                        xlWorkSheet.Rows(l).delete()

                                        If Trim(xlWorkSheet.Cells(l, 3).value()) <> Strings.Left(Strings.Right(m, 16), 11) Then
                                            n = ""
                                        End If
                                    Else
                                        l = l + 1
                                        n = xlWorkSheet.Cells(l, 3).value()
                                    End If
                                Loop

                                xlWorkBook1 = xlApp1.Workbooks.Open(CurDir() & "\" & m)
                                xlWorkSheet1 = xlWorkBook1.Worksheets("Test")
                                xlWorkSheet1.Visible = True
                                xlWorkSheet1.Activate()
                                xlWorkSheet1.Select()

                                If xlWorkSheet1.FilterMode = True Then
                                    xlWorkSheet1.ShowAllData()
                                End If

                                c = 5
                                Do While Trim(xlWorkSheet1.Cells(c, 4).value) <> "Entity Name"
                                    c = c + 1
                                Loop
                                c = c + 2

                                If xlWorkSheet1.Cells(c, 4).value <> "" Then
                                    xlWorkSheet2.Cells(j, 2) = File.GetLastWriteTime(CurDir() & "\" & m) 
                                    lastrow = xlWorkSheet1.Cells(65536, 3).End(Excel.XlDirection.xlUp).Row                                        
                                    xlWorkSheet.Cells(l, 1).Insert(Excel.XlInsertShiftDirection.xlShiftDown, xlWorkSheet1.Range("a" & c.ToString, xlWorkSheet1.Cells(lastrow, 42)).Copy())
                                End If
                                xlWorkBook1.Close()
                                releaseObject(xlWorkBook1)
                                releaseObject(xlWorkSheet1)
                            End If
                            j = j + 1000
                        ElseIf xlWorkSheet2.Cells(j + 1, 1).Value = "" Then
                            k = j + 1
                            xlWorkSheet2.Range("A" & k.ToString).Value = m
                            xlWorkSheet2.Range("B" & k.ToString).Value = "01/01/2000" 
                        End If
                        j = j + 1
                    Loop

                m = Dir()
            Loop

        Next

        xlWorkBook2.Close()
        xlWorkBook.Close()
        xlApp.Quit()
        xlApp2.Quit()
        xlApp1.Quit()
   Catch ex As Exception
   ...

但是在这条线之后

xlWorkSheet.Cells(l, 1).Insert(Excel.XlInsertShiftDirection.xlShiftDown, xlWorkSheet1.Range("a" & c.ToString, xlWorkSheet1.Cells(lastrow, 42)).Copy())

它未能显示错误:System.Runtime.InteropServices.COMException(0x800A03EC):范围类的插入方法失败。

Excel 文件没问题,具有写入权限并且是本地的。有任何想法吗?

4

0 回答 0