I have coded an insert row into Excel that works 99% of the time. The submitter for the spreadsheet know enough about Excel to be dangerous. Every so often I get a spreadsheet that generates a 1004 error on the insert row statement and the description from Access 2003 is that it can't insert the row as it will bump off data. I even delete a row to prevent the error. I manually delete all rows under the data and get the same error. The only way I can get it to work is to copy/paste used rows on a new sheet in the same workbook. I would like to find a work around other than manually or programatically copy pasting to a new sheet. I also get a 1004 error occasinally and the description is that it can't unlock the cells. I'm assuming then that error 1004 is a generic for an Excell error?
Here is the start of my sub:
Public Sub ImportProcessExportAssetCenterOutput_Spreadsheets()
'import excel from HACMFTP_TO_DO
Dim xlapp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
' Dim strCurrentWorkbook As String
Dim strNewWorkbookName As String
Dim lngLastRow As Long
Dim strRange As String
Dim fs1 As Object
Dim strWorkingDir As String
Dim strFileName As String
Dim varCellCount As Variant
Dim blnNoFilesFound As Boolean
Dim blnAnyFileFound As Boolean
Dim strColumnName As String
Dim strNewName As String
Dim lngCountOfHACM_Action As Long
Dim lngCountofDupSN As Long
Dim intCellcount As Integer
On Error GoTo ErrorHandler
'start with clean tables
CleanUpTables
'remove rows from HACM_Exports_Combined older than 12 hrs
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from HACM_Exports_Combined where DateDiff('h',[ProcessingTimeStamp],Now()) >= 12"
DoCmd.SetWarnings True
DoCmd.Hourglass True
strWorkingDir = "D:\FTPData\HACMFTP_TO_DO"
strFileName = "*.xls"
blnNoFilesFound = False
blnAnyFileFound = False
Do Until blnNoFilesFound = True
Set fs1 = Application.FileSearch
With fs1
.NewSearch
.LookIn = strWorkingDir
.FileName = strFileName
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then
blnAnyFileFound = True
'open workbooks and transfer info to table of same name
Set xlapp = CreateObject("Excel.Application")
xlapp.DisplayAlerts = False
Set xlBook = xlapp.Workbooks.Open(.FoundFiles(1))
Set xlSheet = xlBook.Sheets(1)
'set variables for file copy and delete
strCurrentWorkbook = xlBook.Name
xlSheet.Activate
'edited 9-25-2008 to set lngLastRow value to 506, processing after import will remove invalid and empty rows. JFH
''' lngLastRow = xlSheet.UsedRange.Rows.Count + 1
'updated process to find last record 8/25/2008 JFH
''' varCellCount = 1
''' intCellcount = 1
''' For Each varCellCount In xlSheet.Range("A2:A501").Cells
''' If IsNull(varCellCount.Value) = True Or Trim(varCellCount.Value) = "" And intCellcount > 5 Then
''' lngLastRow = intCellcount + 1
''' Exit For
''' End If
''' intCellcount = intCellcount + 1
''' Next
'''
''' If lngLastRow > 501 Or lngLastRow = 0 Then 'lngLastRow = 0 means there are more than 501 records
xlSheet.Rows(506).Delete
lngLastRow = 506 'add one row to account for row added in next step, it will be deleted
''' End If
xlSheet.Range("B1:GO506").Select
xlSheet.Range("B1:GO506").Locked = False
xlSheet.Rows("2:2").Select
'insert row for Alpha data so Excel imports columns as text.
'the row will be deleted later in processing
xlapp.Selection.Insert Shift:=xlShiftDown
'format all columns to text
xlSheet.Columns("B:GO").NumberFormat = "@"
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(8), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(9), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(10), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(13), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(34), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Added 10/17/2008 JFH
'replace HACM_Action if it exists with Action
xlSheet.Range("A1").Replace What:="HACM_Action", Replacement:="Action", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Added 10/17/2008 JFH
'replace Prvider/Lessor if it exists with ProviderLesser
xlSheet.Range("BS1").Replace What:="Provider/Lessor", Replacement:="ProviderLessor", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Remove "'" Removed 10-22-2007 JFH due to conflicts on some entries
''' xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(39), Replacement:="", LookAt:=xlPart, _
''' SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
''' ReplaceFormat:=False
'trim asset tag and SN
''' varCellCount = 1
''' For Each varCellCount In xlSheet.Range("B1:GO" & lngLastRow).Cells
''' varCellCount.Value = Trim(varCellCount.Value)
''' Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("B2:B" & lngLastRow).Cells
varCellCount.Value = Trim(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = Trim(varCellCount.Value)
Next
'insert an alpha character into cells of first row so they import as text
'the row will be deleted later in processing
varCellCount = 1
intCellcount = 1
For Each varCellCount In xlSheet.Range("B2:GO2").Cells
Select Case intCellcount
Case 1, 30, 58, 60, 65, 66, 67, 74, 153
varCellCount.Value = Chr(65) & varCellCount.Value
End Select
intCellcount = intCellcount + 1
Next
'add # to columns that do not import in correct format
'The # will be removed later in processing
varCellCount = 1
For Each varCellCount In xlSheet.Range("B2:B" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BG2:BG" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BI2:BI" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BN2:BN" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BO2:BO" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BP2:BP" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("EX2:EX" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
'netid and SN to ucase
varCellCount = 1
For Each varCellCount In xlSheet.Range("AC2:AC" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("CQ2:CQ" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("DD2D" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("DZ2Z" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
'set variable for rowcount
strRange = "A1:GO" & lngLastRow
'set varible for Add/Mod
strAddModIndicater = xlSheet.Cells(3, 1).Value
'close instance of Excel
xlBook.Close savechanges:=True
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Working_HACM", strWorkingDir & "\" & strCurrentWorkbook, True, strRange
Any ideas?
Thanks Joel
Here is the start of my sub:
Public Sub ImportProcessExportAssetCenterOutput_Spreadsheets()
'import excel from HACMFTP_TO_DO
Dim xlapp As Excel.Application
Dim xlBook As Excel.Workbook
Dim xlSheet As Excel.Worksheet
' Dim strCurrentWorkbook As String
Dim strNewWorkbookName As String
Dim lngLastRow As Long
Dim strRange As String
Dim fs1 As Object
Dim strWorkingDir As String
Dim strFileName As String
Dim varCellCount As Variant
Dim blnNoFilesFound As Boolean
Dim blnAnyFileFound As Boolean
Dim strColumnName As String
Dim strNewName As String
Dim lngCountOfHACM_Action As Long
Dim lngCountofDupSN As Long
Dim intCellcount As Integer
On Error GoTo ErrorHandler
'start with clean tables
CleanUpTables
'remove rows from HACM_Exports_Combined older than 12 hrs
DoCmd.SetWarnings False
DoCmd.RunSQL "Delete * from HACM_Exports_Combined where DateDiff('h',[ProcessingTimeStamp],Now()) >= 12"
DoCmd.SetWarnings True
DoCmd.Hourglass True
strWorkingDir = "D:\FTPData\HACMFTP_TO_DO"
strFileName = "*.xls"
blnNoFilesFound = False
blnAnyFileFound = False
Do Until blnNoFilesFound = True
Set fs1 = Application.FileSearch
With fs1
.NewSearch
.LookIn = strWorkingDir
.FileName = strFileName
.FileType = msoFileTypeExcelWorkbooks
If .Execute > 0 Then
blnAnyFileFound = True
'open workbooks and transfer info to table of same name
Set xlapp = CreateObject("Excel.Application")
xlapp.DisplayAlerts = False
Set xlBook = xlapp.Workbooks.Open(.FoundFiles(1))
Set xlSheet = xlBook.Sheets(1)
'set variables for file copy and delete
strCurrentWorkbook = xlBook.Name
xlSheet.Activate
'edited 9-25-2008 to set lngLastRow value to 506, processing after import will remove invalid and empty rows. JFH
''' lngLastRow = xlSheet.UsedRange.Rows.Count + 1
'updated process to find last record 8/25/2008 JFH
''' varCellCount = 1
''' intCellcount = 1
''' For Each varCellCount In xlSheet.Range("A2:A501").Cells
''' If IsNull(varCellCount.Value) = True Or Trim(varCellCount.Value) = "" And intCellcount > 5 Then
''' lngLastRow = intCellcount + 1
''' Exit For
''' End If
''' intCellcount = intCellcount + 1
''' Next
'''
''' If lngLastRow > 501 Or lngLastRow = 0 Then 'lngLastRow = 0 means there are more than 501 records
xlSheet.Rows(506).Delete
lngLastRow = 506 'add one row to account for row added in next step, it will be deleted
''' End If
xlSheet.Range("B1:GO506").Select
xlSheet.Range("B1:GO506").Locked = False
xlSheet.Rows("2:2").Select
'insert row for Alpha data so Excel imports columns as text.
'the row will be deleted later in processing
xlapp.Selection.Insert Shift:=xlShiftDown
'format all columns to text
xlSheet.Columns("B:GO").NumberFormat = "@"
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(8), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(9), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(10), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(13), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(34), Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Added 10/17/2008 JFH
'replace HACM_Action if it exists with Action
xlSheet.Range("A1").Replace What:="HACM_Action", Replacement:="Action", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Added 10/17/2008 JFH
'replace Prvider/Lessor if it exists with ProviderLesser
xlSheet.Range("BS1").Replace What:="Provider/Lessor", Replacement:="ProviderLessor", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
'Remove "'" Removed 10-22-2007 JFH due to conflicts on some entries
''' xlSheet.Range("B1:GO" & lngLastRow).Replace What:=Chr(39), Replacement:="", LookAt:=xlPart, _
''' SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
''' ReplaceFormat:=False
'trim asset tag and SN
''' varCellCount = 1
''' For Each varCellCount In xlSheet.Range("B1:GO" & lngLastRow).Cells
''' varCellCount.Value = Trim(varCellCount.Value)
''' Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("B2:B" & lngLastRow).Cells
varCellCount.Value = Trim(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = Trim(varCellCount.Value)
Next
'insert an alpha character into cells of first row so they import as text
'the row will be deleted later in processing
varCellCount = 1
intCellcount = 1
For Each varCellCount In xlSheet.Range("B2:GO2").Cells
Select Case intCellcount
Case 1, 30, 58, 60, 65, 66, 67, 74, 153
varCellCount.Value = Chr(65) & varCellCount.Value
End Select
intCellcount = intCellcount + 1
Next
'add # to columns that do not import in correct format
'The # will be removed later in processing
varCellCount = 1
For Each varCellCount In xlSheet.Range("B2:B" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BG2:BG" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BI2:BI" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BN2:BN" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BO2:BO" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BP2:BP" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("EX2:EX" & lngLastRow).Cells
varCellCount.Value = Chr(35) & (varCellCount.Value)
Next
'netid and SN to ucase
varCellCount = 1
For Each varCellCount In xlSheet.Range("AC2:AC" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("BW2:BW" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("CQ2:CQ" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("DD2D" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
varCellCount = 1
For Each varCellCount In xlSheet.Range("DZ2Z" & lngLastRow).Cells
varCellCount.Value = UCase(varCellCount.Value)
Next
'set variable for rowcount
strRange = "A1:GO" & lngLastRow
'set varible for Add/Mod
strAddModIndicater = xlSheet.Cells(3, 1).Value
'close instance of Excel
xlBook.Close savechanges:=True
DoCmd.TransferSpreadsheet acImport, acSpreadsheetTypeExcel9, "Working_HACM", strWorkingDir & "\" & strCurrentWorkbook, True, strRange
Any ideas?
Thanks Joel