簡體   English   中英

Excel VBA編程錯誤-多個工作簿

[英]Error in Excel VBA programming - multiple workbooks

我正在嘗試解析幾個Excel文件並將數據復制到當前打開的文件中。 這是我的代碼。 它在此行失敗; dstDecTbl(rowNdx,DstDecDeath).Value = srcDecData(SrcDecDeath).Value是否可以在不同的工作簿中設置范圍並引用它們?

Option Explicit

Private Const ConstModuleName As String = "ProcessModule"


Public Sub BuildReport()

  Dim srcBook As Workbook
  Dim dstBook As Workbook

  Dim fileSys As Object
  Dim fileDir As Object
  Dim fileObj As Object
  Dim fileCnt As Integer

  Dim srcDecData As Range
  Dim srcCntData As Range

  Dim dstDecTbl As Range
  Dim dstCntTbl As Range

  Dim fileNdx As Integer
  Dim rowNdx As Integer


  On Error GoTo Exception

    Set dstBook = Application.ThisWorkbook
    Set dstDecTbl = dstBook.Sheets(DstPage).Range(DstDecRange)
    Set dstCntTbl = dstBook.Sheets(DstPage).Range(DstCntRange)

    Set fileSys = CreateObject("Scripting.FileSystemObject")
    Set fileDir = fileSys.GetFolder(DftSourceDir)

    rowNdx = 1
    fileNdx = 1
    For Each fileObj In fileDir.Files

      dstBook.Sheets(DstPage).Range(DstFileCount) = fileNdx
      dstBook.Sheets(DstPage).Range(DstFileName) = fileObj.Name
      Application.ScreenUpdating = False      

      Set srcBook = Workbooks.Open(fileObj.Path)
      Set srcDecData = srcBook.Sheets(SrcPage).Range(SrcDecRange)

      dstDecTbl(rowNdx, DstDecDeath).Value = srcDecData(SrcDecDeath).Value

      rowNdx = rowNdx + 1

      srcBook.Close SaveChanges:=False
      Application.ScreenUpdating = True

      fileNdx = fileNdx + 1
    Next fileObj
  Exit Sub

Exception:
    Call ExceptionModule.LogException(ConstModuleName & "." & _
                                 "buildReport", Err, vbCritical)

End Sub

忽略該錯誤是在常量定義中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM