Excel VBA) DAO를 이용하여 엑셀문서 열지않고 데이타 가져오기
strSQL = "Select * FROM [장애관리.csv] where 출동업체 = '로케트밧데리' "
"Select * FROM [레코드명(csv파일명 적어줌)] where 필드명 = '추출할내용' "
Microsoft Visual Basic 코드입력창에서 [도구] -> [참조] 선택하면
참조 - VBAProject 창이 나타납니다.
Microsoft DAO 3.6 Object Library 항목 체크표시를 선택해야 합니다.
참고) 확장자가 CSV파일은 불러오지 못한다?
Sub XLSReadValue()
Dim strSQL As String
Dim dbMyDB As DAO.Database
Dim rstRST As DAO.Recordset
Dim lngStart As Long
Dim i As Integer
Const SourceFile = "Book31.xls"
lngStart = Timer
strSQL = "Select 아우, 미초, 일처리 "
'strSQL = strSQL & " RQD등급, BRMR, RMR, RMR등급, Q, Q등급, SMR, SMR등급, SCR, 추가1, 추가2, 추가3 "
strSQL = strSQL & "FROM [Book3$] "
Set dbMyDB = OpenDatabase(ThisWorkbook.Path & Application.PathSeparator & SourceFile, False, True, "excel 8.0;")
Set rstRST = dbMyDB.OpenRecordset(strSQL)
Application.ScreenUpdating = False
'Worksheets.Add after:=Worksheets("Sheet2")
With ActiveSheet
.UsedRange.Clear
For i = 0 To rstRST.Fields.Count - 1
Cells(1, i + 1).Value = rstRST.Fields(i).Name
Next
.Range("A2").CopyFromRecordset rstRST
.UsedRange.Columns.AutoFit
End With
Application.ScreenUpdating = True
MsgBox "불과 " & Format(Timer - lngStart, "00:00:00") & "초 소요되었습니다!", , "www.iExcellr.com"
rstRST.Close
dbMyDB.Close
Set rstRST = Nothing
Set dbMyDB = Nothing
End Sub