I have vba that I have written to simplify the import process for a project. The code works overly well, but for certain files/worksheets access errors out looking for column headers (named F## - or exp F15). These columns do not exist as part of the data set for that file/worksheet. I have tried to eliminate the error by deleting all columns to the right of my active data range, but that does not seem to stop the issue.
Any ideas why this is happening?
I am importing using:
and I am using the following just prior to the import to alphabetize the fields and to delete the columns right of the data set:
thanks,
rich
Any ideas why this is happening?
I am importing using:
Code:
Private Sub AURD_APPEND_ME(ws As Worksheet, db As Database, strTBLNAME As String)
Dim varI As Variant, varJ As Variant
Dim charCNT As Long
Dim strAPPQ As String
strAPPQ = "TBL_AuRD_"
On Error Resume Next
charCNT = Len(ws.Name) - Len(Replace(ws.Name, "_", ""))
If Not charCNT <= 0 Then
varJ = Right(ws.Name, Len(ws.Name) - InStrRev(ws.Name, "_"))
varI = Left(ws.Name, InStr(1, ws.Name, "_") - 1)
Else
varI = ws.Name
End If
On Error GoTo 0
If varJ = "DCAS" Or varJ = "DAI" Then
varI = strAPPQ & varJ
db.Execute "INSERT INTO " & varI & " SELECT * FROM " & strTBLNAME
Else
varI = strAPPQ & varI
db.Execute "INSERT INTO " & varI & " SELECT * FROM " & strTBLNAME
End If
End Sub
and I am using the following just prior to the import to alphabetize the fields and to delete the columns right of the data set:
Code:
Private Sub SortLTable(ws As Worksheet)
Dim lngCOL As Long, lngROW As Long
Dim rng As Range, rngHEAD As Range
ws.Select
With ws
lngCOL = Cells(1, .Columns.Count).End(xlToLeft).Column
Debug.Print lngCOL
lngROW = Range("A" & .Rows.Count).End(xlUp).Row
Set rngHEAD = Range(.Cells(1, 1), .Cells(1, lngCOL))
Set rng = Range(.Cells(1, 1), .Cells(lngROW, lngCOL))
rng.Sort _
Key1:=rngHEAD, _
Order1:=xlAscending, _
Orientation:=xlLeftToRight
Set rng = Cells(1, lngCOL + 1)
Set rng = Range(.Cells(1, rng.Column), .Cells(1, rng.End(xlToRight).Column))
'rng.EntireColumn.Select
rng.Delete Shift:=xlToLeft
'rng.EntireColumn.Delete
'rng.Columns.Delete
End With
End Sub
thanks,
rich