2016-07-21 2 views
0

Excel 스프레드 시트의 특정 열을 내 Access 데이터베이스로 가져 오는 데 사용하는 VBA 코드가 있습니다.기존 VBA 코드로 Access 데이터베이스에 새 파일을 가져 오는 방법

데이터베이스로 가져 오는 파일은 매 근무일마다 생성되므로 매주 시작할 때 가져 오기 프로세스를 실행할 계획입니다.

내가 직면 한 문제는 현재 코드가 이전 주에 생성 된 새로운 스프레드 시트가 아닌 디렉토리의 모든 스프레드 시트를 가져옵니다. 가져 오는 파일은 "FD Worksheet 01 07 2016"형식으로 이름이 지정되며 날짜 섹션은 생성 된 날짜입니다. 데이터베이스에는 아래 코드에서 볼 수있는 것처럼 "file_date"라는 필드가 있습니다.이 필드는 데이터를 가져온 파일을 식별 할 수 있도록 데이터베이스로 가져올 때 파일의 날짜가 저장되는 곳입니다.

누군가 내 데이터베이스에서 누락 된 스프레드 시트 만 가져 오도록 내 코드를 수정하도록 도와주십시오. 나는. 먼저 파일을 검색하여 파일이 이전에 가져 왔는지 확인하십시오. 그렇다면 무시하십시오.

감사합니다.

Option Compare Database 

Public Function importExcelSheets() 

Dim db As DAO.Database 
Dim qdf As DAO.QueryDef 
Dim astrPieces() As String 
Dim dteFileDate As Date 
Dim strDir As String 
Dim strFile As String 
Dim strInsert As String 
Dim Directory As String 
Dim TableName As String 

Directory = "F:\FD Worksheets\jul 2016" 
TableName = "FD Worksheets" 

Dim strTable As String 
Dim I As Long 
I = 0 

If Right(Directory, 1) <> "\" Then 
    strDir = Directory & "\" 
Else 
    strDir = Directory 
End If 
strFile = '" strFile = Dir(strDir & "*.XLSX")  

While strFile <> "" 
    I = I + 1 
    Debug.Print "importing " & strFile 

If Not strDir Like "*\" Then 
    strDir = strDir & "\" 
End If 
strInsert = "INSERT INTO [FD Worksheets] (file_date, Prod, Average_Cost, WSP)" & vbCrLf & _ 
    "SELECT [which_date] as file_date, xl.Prod, xl.Average_Cost, xl.WSP" & vbCrLf & _ 
    "FROM [Excel 12.0 Xml;HDR=YES;IMEX=2;DATABASE=" & strDir & strFile & "].[Sheet1$] AS xl;" 
Debug.Print strInsert 
astrPieces = Split(Left(strFile, Len(strFile) - 5), " ") 
dteFileDate = DateSerial(Val(astrPieces(4)), astrPieces(3), astrPieces(2)) 
Debug.Print dteFileDate 
Set db = CurrentDb 
Set qdf = db.CreateQueryDef(vbNullString, strInsert) 
qdf.Parameters("which_date").Value = dteFileDate 
qdf.Execute dbFailOnError 

    strFile = Dir() 
Wend 

End Function 
+1

가이 아카이브 폴더로 XLSX를 이동하는 것이 더 간단 할 당신이 추가 한하지 않을까요 후 검증되지 않은입니다 필요 그 데이터? 다음에이 절차를 실행하면 원본 폴더에는 가져 오지 않은 XLSX 파일 만 포함됩니다. – HansUp

+0

당신은 공정한 점을 지적합니다, 나는 이것을 복잡하게 할 가능성이 높습니다. 귀하의 회신에 감사드립니다. – thatguy

답변

0

당신은 사용하는 A 주 번호와 절은, 아래의 코드를하려고하면, 그것은

Option Compare Database 

Public Function importExcelSheets() 

Dim db As DAO.Database 
Dim qdf As DAO.QueryDef 
Dim astrPieces() As String 
Dim dteFileDate As Date 
Dim strDir As String 
Dim strFile As String 
Dim strInsert As String 
Dim Directory As String 
Dim TableName As String 

Directory = "F:\FD Worksheets\jul 2016" 
TableName = "FD Worksheets" 

Dim strTable As String 
Dim I As Long 
I = 0 

If Right(Directory, 1) <> "\" Then 
    strDir = Directory & "\" 
Else 
    strDir = Directory 
End If 
strFile = '" strFile = Dir(strDir & "*.XLSX")  

While strFile <> "" 


    'Add This Line 
    if Format(Mid(strfile, 14, 10), "ww") = format(date(), "ww") -1 then 


    I = I + 1 
    Debug.Print "importing " & strFile 

If Not strDir Like "*\" Then 
    strDir = strDir & "\" 
End If 
strInsert = "INSERT INTO [FD Worksheets] (file_date, Prod, Average_Cost, WSP)" & vbCrLf & _ 
    "SELECT [which_date] as file_date, xl.Prod, xl.Average_Cost, xl.WSP" & vbCrLf & _ 
    "FROM [Excel 12.0 Xml;HDR=YES;IMEX=2;DATABASE=" & strDir & strFile & "].[Sheet1$] AS xl;" 
Debug.Print strInsert 
astrPieces = Split(Left(strFile, Len(strFile) - 5), " ") 
dteFileDate = DateSerial(Val(astrPieces(4)), astrPieces(3), astrPieces(2)) 
Debug.Print dteFileDate 
Set db = CurrentDb 
Set qdf = db.CreateQueryDef(vbNullString, strInsert) 
qdf.Parameters("which_date").Value = dteFileDate 
qdf.Execute dbFailOnError 
'and this one 
end if 
    strFile = Dir() 
Wend 

End Function 
관련 문제