Sandra Smith
asked on
Subscript out of range when trying to set worksheet variable
i have the below which sometimes work and sometimes does not. It stops at the Set wks line. This module is in an ACCESS 2003 database opening the Excel workbook.
Sandra
Option Explicit
'Daily upload of debits and credits from accounting
Const strPath = "\\Sf1\User1\shared\EFT\AT M\Private\ Backend\AT MUpload.xl s" 'Path to directory
Const strTab = "Cumulative Journal Transaction " 'Name of data source tab
Const strWKBName = "ATMUpload.xls"
Public Sub DefineDataRange()
On Error GoTo ErrorHandler
'Late Binding (Needs no reference set)
Dim objExcel As Object
Dim wks As Worksheet
Dim wkb As Workbook
Dim intRow As Integer
Dim rngDataSource As Range
' Create a new Excel instance
Set objExcel = CreateObject("Excel.Applic ation")
' Open it
With objExcel
.Visible = True
.Workbooks.Open (strPath)
.Workbooks(strWKBName).Act ivate
.Workbooks(strWKBName).Wor ksheets(st rTab).Sele ct
'SOMETIMES RUNS SOMETIMES DOESN'T-Subscript out of range error
Set wks = Workbooks(strWKBName).Work sheets(str Tab)
End With
With wks
.Range("A5").Activate
intRow = .Range("A200").End(xlUp).R ow
Set rngDataSource = Range("A5", "G" & intRow)
rngDataSource.Name = "DataSource"
End With
Workbooks(strWKBName).Clos e SaveChanges:=True
Exit_ErrorHandler:
Set objExcel = Nothing
Exit Sub
ErrorHandler:
objExcel.Visible = False
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description
Resume Exit_ErrorHandler
End Sub
Sandra
Option Explicit
'Daily upload of debits and credits from accounting
Const strPath = "\\Sf1\User1\shared\EFT\AT
Const strTab = "Cumulative Journal Transaction " 'Name of data source tab
Const strWKBName = "ATMUpload.xls"
Public Sub DefineDataRange()
On Error GoTo ErrorHandler
'Late Binding (Needs no reference set)
Dim objExcel As Object
Dim wks As Worksheet
Dim wkb As Workbook
Dim intRow As Integer
Dim rngDataSource As Range
' Create a new Excel instance
Set objExcel = CreateObject("Excel.Applic
' Open it
With objExcel
.Visible = True
.Workbooks.Open (strPath)
.Workbooks(strWKBName).Act
.Workbooks(strWKBName).Wor
'SOMETIMES RUNS SOMETIMES DOESN'T-Subscript out of range error
Set wks = Workbooks(strWKBName).Work
End With
With wks
.Range("A5").Activate
intRow = .Range("A200").End(xlUp).R
Set rngDataSource = Range("A5", "G" & intRow)
rngDataSource.Name = "DataSource"
End With
Workbooks(strWKBName).Clos
Exit_ErrorHandler:
Set objExcel = Nothing
Exit Sub
ErrorHandler:
objExcel.Visible = False
MsgBox "Error Number: " & Err.Number & vbCrLf & _
"Description: " & Err.Description
Resume Exit_ErrorHandler
End Sub
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I got a variations of this to work. Capricon, I did try what you indicated prior to posting the question, but I could not get it to work. Thank you both.
Sandra
Sandra
ASKER
imnoie, this is what I changed, the objWB was not set (typo, happens)
' Open it
With objExcel
.Visible = True
Set objWB = .Workbooks.Open(strPath)
End With
' Open it
With objExcel
.Visible = True
Set objWB = .Workbooks.Open(strPath)
End With
Oops, don't know how I missed that out.
Set objExcel = CreateObject("Excel.Applic
' Open it
objExcel.Workbooks.Open (strPath)
With objExcel
.Visible = True
.Activate
.Worksheets(strTab).Select
Set wks = .Worksheets(strTab)
End With