Error in Excel VBA programming - multiple workbooks

98 Views Asked by At

I'm trying to parse through several Excel files and copy data to the currently open file. Here's my code. It fails at this line; dstDecTbl(rowNdx, DstDecDeath).Value = srcDecData(SrcDecDeath).Value Can I set ranges in different workbooks and reference them?

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
1

There are 1 best solutions below

0
On

Disregard, the error was in the constant definition.