RawData를 반환하여 바탕화면에 파일을 만드 코드 - Heeyoung-Ahn/Excel_VBA GitHub Wiki
Sub ChurchListtoExcel()
Dim i As Integer
Dim fileNM As String
Dim fileSNM As String
'//엑셀로 자료 내보내기
Optimization
'파일생성
Workbooks.Add
'자료붙여넣기
For i = 0 To rs.Fields.Count - 1
Cells(1, 1).Offset(0, i).Value = rs.Fields(i).Name
Next i
Cells(2, 1).CopyFromRecordset rs
Cells(1.1).CurrentRegion.Columns.AutoFit
'정렬
ActiveSheet.AutoFilterMode = False
Cells(1, 1).AutoFilter
With ActiveSheet.AutoFilter.Sort
.SortFields.Clear
.SortFields.Add Key:=Cells(1, 13), Order:=xlAscending
.Header = xlYes
.Apply
End With
ActiveSheet.AutoFilterMode = False
'저장
fileNM = GetDesktopPath() & "file_name" & "(" & Format(Date, "yyyymmdd") & "_" & Format(Time, "hhmm") & ")" & ".xlsx"
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs Filename:=fileNM
Application.DisplayAlerts = True
Normal
Cells(2, 1).Select
'//결과보고, 마무리
fileSNM = Right(fileNM, Len(fileNM) - InStrRev(fileNM, "\"))
MsgBox "자료 조회 및 바탕화면에 저장이 완료되었습니다." & vbNewLine & vbNewLine & _
" - 파일이름: " & fileSNM, vbInformation, banner
End Sub