performance – Write in EXCEL from SQL DB with the help of a VBA script

I have a vba code that takes data from the SQL database and empties it into Excel. I see that my query has to extract about 120,000 records in total. I monitored this activity and learned that even after 8 hours of work in the office, the query was able to extract just 70,000 records.

It frustrates me because I am totally new to VBA. Can you help me here by changing my code?

                Macro1
Private sub-macro1 ()
Set objExcel = CreateObject ("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open ("C:  Users  kursekar  Documents  Work  Apps  ReferralStrApp  StdztnRefRepTrial.xlsx")
objExcel.Visible = True
Dim Conn
Dim RS
Dim SQL
SQL = "WITH cte_REFERRALS_REPORTS (referralnum, reference_from, reference_from_name, reference_frome_id, reference_to, reference_to_name, refer_to_id) as refer_from_name, refer_from_id, refer_to,"
SQL = SQL & "WHEN CASE" refer_to_id = "R" WHEN RdicT.refname WHEN refer_to_id = "THEN PdicT.provname END AS name_to_name, refer_id_id r Left Join refcode_t RdicF SUR r.refer_from = CASE WHEN r.refer_d R & # 39; THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = WHEN CASE r.refer_to_id = R & # 39; THEN RdicT.refcode SUB NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = P # pdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id_to_id = "P" THEN .provcode ELSE NULL END SELECT chgslipno, a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = "&", a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_rr reference_from_id as r2_ref_from_id, r2.refer_from_name as r2_ref_from_name, a.referral2, prgrc.provgrpdesc , s.specdesc, a.prov_dept, pos.posdesc, pr.cred "
SQL = SQL & "FROM apptmt_t a Left JOIN patdemo_t p ON a.acctno = p.acctno LEFT JOIN - provcode_t pr ON code.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT * FROM cte_REFERRALS_REPORTS) r1 OVER a.referral1 = r1.referralnum LEFT JOIN (SELECT * FROM cte_REFERRALS_REPORTS) r2 "
SQL = SQL & "on a .referral2 = r2.referralnum LEFT JOIN: provgrpprov_t prgrpr on a.provcode = prgrpr.provcode LEFT JOIN, provgrpcode_t prgrc on prgrpr.provgrpcode = prgrc.provgrpcode LEFT, JOINT_EXTERIOR, on pr.speccode = s. speccode = s JOIN poscode_t pos on a.poscode = pos.poscode "
SQL = SQL & "WHERE UPPER (a.enccode) in (" CON "," APE "," COB "," CONZ "," HAC "," HFUI "," MMN "," NCG "," NCHF ", "NCPF", "NHFU", "NMC", "NOB", "NP", "NP15", "NPE", "NPI", "NPOV", "NPS", "NPSV", "NPV", "OVN" "& # 39;, & # 39; BMI & # 39;, & # 39;) NP30 & # 39;) AND UPPER (a.appt_status) = & # 39; ARR & # 39; AND appt_date> = & # 39; ; 2017-01-01 & # 39; "
SQL = SQL & "ORDER BY a.acctno"
Set Conn = CreateObject ("ADODB.Connection")
Conn.Open = "Provider = SQLOLEDB.1; Password =" 25LaurelRoad "; User ID =" CPSMDIT  kursekar "; Data Source =" Analyzer & # 39; Initial Catalog = "analyzer_str" Integrated Security = SSPI; Persistent Security Information = True; "
Set RS = Conn.Execute (SQL)
Define the sheet = objWorkbook.ActiveSheet
Feuille.Activer
Dim R
R = 2
While RS.EOF = False
Sheet.Cells (R, 1) .Value = RS.Fields (0)
Sheet.Cells (R, 2) .Value = RS.Fields (1)
Sheet.Cells (R, 3) .Value = RS.Fields (2)
Sheet.Cells (R, 4) .Value = RS.Fields (3)
Sheet.Cells (R, 5) .Value = RS.Fields (4)
Sheet.Cells (R, 6) .Value = RS.Fields (5)
Sheet.Cells (R, 7) .Value = RS.Fields (6)
Sheet.Cells (R, 8) .Value = RS.Fields (7)
Sheet.Cells (R, 9) .Value = RS.Fields (8)
Sheet.Cells (R, 10) .Value = RS.Fields (9)
Sheet.Cells (R, 11) .Value = RS.Fields (10)
Sheet.Cells (R, 12) .Value = RS.Fields (11)
Sheet.Cells (R, 13) .Value = RS.Fields (12)
Sheet.Cells (R, 14) .Value = RS.Fields (13)
Sheet.Cells (R, 15) .Value = RS.Fields (14)
Sheet.Cells (R, 16) .Value = RS.Fields (15)
Sheet.Cells (R, 17) .Value = RS.Fields (16)
Sheet.Cells (R, 18) .Value = RS.Fields (17)
Sheet.Cells (R, 19) .Value = RS.Fields (18)
Sheet.Cells (R, 20) .Value = RS.Fields (19)
Sheet.Cells (R, 21) .Value = RS.Fields (20)
Sheet.Cells (R, 22) .Value = RS.Fields (21)
Sheet.Cells (R, 23) .Value = RS.Fields (22)
rs.MoveNext
R = R + 1
Wend
RS.Fermer
Conn.Fermer
Application.DisplayAlerts = False
& # 39; Free memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
ActiveWorkbook.Save
& # 39; objWorkbook.SaveAs File Name: = "C: \ Users  kursekar  Documents  Work  Dailytasks  January  ReferralStrApp  StdztnRefRepTrial.xlsx", FileFormat: = 51
Application.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
Classeurs.Fermer
Set objExcel = Nothing
MsgBox ("Registered")
End Sub