M
Martin
Forgive me if this is considered a double post. I could not find my post
from a few weeks ago to continue that one.
I posted a question a few weeks ago about why Excel would remain open with
the spreadsheet file in read-only mode. It was suggested then that I fully
qualify all my references to the Excel application and file. I believe I
have done that, but I still end up with Excel running in read-only mode.
The problem occurs regardless of whether Excel is already running or not. I
always get a message box titled "File Now Available" with options to Cancel
or Read/Write. Even if I Cancel here, the spreadsheet still remains open.
The code I am using is posted below. Can anyone offer any suggestions to
stop this?
Thanks.
=========start of code
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open FileName:=HRFileName
'If number of worksheets is not as expected
If xlApp.Worksheets.Count <> HRWkstCount Then
Mess1 = "The number of worksheets in " & UCase(HRFile) & _
" is not " & HRWkstCount
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & vbCrLf & Mess2 & vbCrLf & Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
'Find and compare first data row in HR file.
FirstDataRow = 0
FirstRowCheck = 0
For Y = 1 To HRWkstCount
xlApp.Worksheets(Y).Select
xlApp.Range("B1").Select
For X = 1 To 10
If xlApp.ActiveCell.Value <> "" Then
FirstRowCheck = xlApp.ActiveCell.Row + 1
Exit For
End If
xlApp.ActiveCell.Offset(1, 0).Select
Next X
If Y = 1 Then
FirstDataRow = FirstRowCheck
End If
If FirstRowCheck <> FirstDataRow Then
Mess1 = "The pages of HR data do not start on the same row"
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & vbCrLf & Mess2 & vbCrLf & Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
Next Y
'Check to be sure all SSNs are numbers, not labels
FileBadSSNCount = 0
For Y = 1 To HRWkstCount
SheetBadSSNCount = 0
xlApp.Worksheets(Y).Select
xlApp.Range("B" & FirstRowCheck).Select
xlApp.Selection.End(xlDown).Select
Last_Row = xlApp.ActiveCell.Row
xlApp.Range("A" & FirstRowCheck).Select
For X = FirstRowCheck To Last_Row
If IsNumeric(xlApp.ActiveCell.Value) = vbFalse Then
SheetBadSSNCount = SheetBadSSNCount + 1
FileBadSSNCount = FileBadSSNCount + 1
End If
xlApp.ActiveCell.Offset(1, 0).Select
Next X
BadSSNArray(Y - 1, 0) = xlApp.ActiveSheet.Name
BadSSNArray(Y - 1, 1) = SheetBadSSNCount
Next Y
If FileBadSSNCount <> 0 Then
Mess1A = ""
For Z = 0 To HRWkstCount - 1
Mess1A = Mess1A & " Tab: " & BadSSNArray(Z, 0) & _
" Count: " & BadSSNArray(Z, 1) & vbCrLf
Next Z
Mess1 = "There are " & FileBadSSNCount & " invalid SSN values"
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & Mess1A & vbCrLf & Mess2 & vbCrLf &
Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
For Z = 1 To HRWkstCount
'Select worksheet tab of HR file
xlApp.Worksheets(Z).Select
'Store facility name/tab name for later use
FacName = xlApp.ActiveSheet.Name
xlApp.Range("A" & FirstDataRow).Select
xlApp.Selection.End(xlDown).Select
'Store last row of data
Last_Row = xlApp.ActiveCell.Row
'Create address range of data to import
CellRange = FacName & "!A" & FirstDataRow - 1 & ":G" & _
Last_Row
'Import data from spreadsheet
DoCmd.TransferSpreadsheet acImport, , "W_HRImport", HRFileName, _
True, CellRange
Next Z
'Close HR File
xlApp.Workbooks(HRFile).Close False
CleanUp:
' Close Excel
xlApp.Quit
Set xlApp = Nothing
=========end of code
from a few weeks ago to continue that one.
I posted a question a few weeks ago about why Excel would remain open with
the spreadsheet file in read-only mode. It was suggested then that I fully
qualify all my references to the Excel application and file. I believe I
have done that, but I still end up with Excel running in read-only mode.
The problem occurs regardless of whether Excel is already running or not. I
always get a message box titled "File Now Available" with options to Cancel
or Read/Write. Even if I Cancel here, the spreadsheet still remains open.
The code I am using is posted below. Can anyone offer any suggestions to
stop this?
Thanks.
=========start of code
Dim xlApp As Excel.Application
Set xlApp = CreateObject("Excel.Application")
xlApp.Workbooks.Open FileName:=HRFileName
'If number of worksheets is not as expected
If xlApp.Worksheets.Count <> HRWkstCount Then
Mess1 = "The number of worksheets in " & UCase(HRFile) & _
" is not " & HRWkstCount
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & vbCrLf & Mess2 & vbCrLf & Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
'Find and compare first data row in HR file.
FirstDataRow = 0
FirstRowCheck = 0
For Y = 1 To HRWkstCount
xlApp.Worksheets(Y).Select
xlApp.Range("B1").Select
For X = 1 To 10
If xlApp.ActiveCell.Value <> "" Then
FirstRowCheck = xlApp.ActiveCell.Row + 1
Exit For
End If
xlApp.ActiveCell.Offset(1, 0).Select
Next X
If Y = 1 Then
FirstDataRow = FirstRowCheck
End If
If FirstRowCheck <> FirstDataRow Then
Mess1 = "The pages of HR data do not start on the same row"
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & vbCrLf & Mess2 & vbCrLf & Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
Next Y
'Check to be sure all SSNs are numbers, not labels
FileBadSSNCount = 0
For Y = 1 To HRWkstCount
SheetBadSSNCount = 0
xlApp.Worksheets(Y).Select
xlApp.Range("B" & FirstRowCheck).Select
xlApp.Selection.End(xlDown).Select
Last_Row = xlApp.ActiveCell.Row
xlApp.Range("A" & FirstRowCheck).Select
For X = FirstRowCheck To Last_Row
If IsNumeric(xlApp.ActiveCell.Value) = vbFalse Then
SheetBadSSNCount = SheetBadSSNCount + 1
FileBadSSNCount = FileBadSSNCount + 1
End If
xlApp.ActiveCell.Offset(1, 0).Select
Next X
BadSSNArray(Y - 1, 0) = xlApp.ActiveSheet.Name
BadSSNArray(Y - 1, 1) = SheetBadSSNCount
Next Y
If FileBadSSNCount <> 0 Then
Mess1A = ""
For Z = 0 To HRWkstCount - 1
Mess1A = Mess1A & " Tab: " & BadSSNArray(Z, 0) & _
" Count: " & BadSSNArray(Z, 1) & vbCrLf
Next Z
Mess1 = "There are " & FileBadSSNCount & " invalid SSN values"
Mess2 = "Check the file for proper format"
Mess3 = "Processing will be aborted"
MessAll = Mess1 & vbCrLf & Mess1A & vbCrLf & Mess2 & vbCrLf &
Mess3
MsgBox MessAll, vbOKOnly, "HR FILE FORMAT PROBLEM"
GoTo CleanUp
End If
For Z = 1 To HRWkstCount
'Select worksheet tab of HR file
xlApp.Worksheets(Z).Select
'Store facility name/tab name for later use
FacName = xlApp.ActiveSheet.Name
xlApp.Range("A" & FirstDataRow).Select
xlApp.Selection.End(xlDown).Select
'Store last row of data
Last_Row = xlApp.ActiveCell.Row
'Create address range of data to import
CellRange = FacName & "!A" & FirstDataRow - 1 & ":G" & _
Last_Row
'Import data from spreadsheet
DoCmd.TransferSpreadsheet acImport, , "W_HRImport", HRFileName, _
True, CellRange
Next Z
'Close HR File
xlApp.Workbooks(HRFile).Close False
CleanUp:
' Close Excel
xlApp.Quit
Set xlApp = Nothing
=========end of code