Write to EXCEL from SQL DB using VBA scriptMS Access VBA code to compare records in a table and combine data...
Write to EXCEL from SQL DB using VBA script
How to compensate for height when using a ranged attack
Just like there is a Shiva Purana and Vishnu Purana, is there a Purana praising Brahma?
How to creep the reader out with what seems like a normal person?
Can a creature tell when it has been affected by a Divination wizard's Portent?
How do I tell my manager that he's wrong?
How to efficiently calculate prefix sum of frequencies of characters in a string?
Binary Numbers Magic Trick
What happened to Rhaegal?
Hang 20lb projector screen on hardieplank
Pressure to defend the relevance of one's area of mathematics
How did Arya manage the sneak attack?
Is it the same airport YUL and YMQ in Canada?
Why do computer-science majors learn calculus?
Why is the SNP putting so much emphasis on currency plans?
Printing a string when grep does not get a match
Declining lunch invitation from new work because I will be fasting
Sower of Discord, Gideon's Sacrifice and Stuffy Doll
Feels like I am getting dragged in office politics
CRT Oscilloscope - part of the plot is missing
Transfer over $10k
Can fracking help reduce CO2?
How to get SEEK accessing converted ID via view
Different output when alias
Write to EXCEL from SQL DB using VBA script
MS Access VBA code to compare records in a table and combine data - revisedCleaning up and reformatting imported data in an Excel sheetVBA script to format an Excel sheetExcel VBA highlighting macroSearching across text filesMerging rows in excel using VBAExcel VBA script – concatenates multiple values using LoopFind all identical data in a Column and filter it to another sheetGeneral function to test for empty/no-value controlsExecuting arbitrary SQL statements using VBA in Excel
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
$begingroup$
I have a vba code which takes data from SQL database and dumps it into excel. I see that my query should extract approximately total of 120k records. I monitored this activity and learnt that even after 8 hours of my office time, the query is successful in extracting barely 70k records.
This is frustrating me as I am totally new to VBA. Can you guys help me here by modifying my code?
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = True
Dim Conn
Dim RS
Dim SQL
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Set RS = Conn.Execute(SQL)
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
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.Close
Conn.Close
Application.DisplayAlerts = False
'Release memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
ActiveWorkbook.Save
'objWorkbook.SaveAs Filename:="C:\UserskursekarDocumentsWorkDailytasksJanuaryReferralStrAppStdztnRefRepTrial.xlsx", FileFormat:=51
Application.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
Workbooks.Close
Set objExcel = Nothing
MsgBox ("Saved")
End Sub
performance vba excel
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
I have a vba code which takes data from SQL database and dumps it into excel. I see that my query should extract approximately total of 120k records. I monitored this activity and learnt that even after 8 hours of my office time, the query is successful in extracting barely 70k records.
This is frustrating me as I am totally new to VBA. Can you guys help me here by modifying my code?
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = True
Dim Conn
Dim RS
Dim SQL
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Set RS = Conn.Execute(SQL)
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
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.Close
Conn.Close
Application.DisplayAlerts = False
'Release memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
ActiveWorkbook.Save
'objWorkbook.SaveAs Filename:="C:\UserskursekarDocumentsWorkDailytasksJanuaryReferralStrAppStdztnRefRepTrial.xlsx", FileFormat:=51
Application.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
Workbooks.Close
Set objExcel = Nothing
MsgBox ("Saved")
End Sub
performance vba excel
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
2
$begingroup$
ConsiderRange.CopyFromRecordsetinstead of writing one single cell at a time.
$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
1
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago
add a comment |
$begingroup$
I have a vba code which takes data from SQL database and dumps it into excel. I see that my query should extract approximately total of 120k records. I monitored this activity and learnt that even after 8 hours of my office time, the query is successful in extracting barely 70k records.
This is frustrating me as I am totally new to VBA. Can you guys help me here by modifying my code?
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = True
Dim Conn
Dim RS
Dim SQL
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Set RS = Conn.Execute(SQL)
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
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.Close
Conn.Close
Application.DisplayAlerts = False
'Release memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
ActiveWorkbook.Save
'objWorkbook.SaveAs Filename:="C:\UserskursekarDocumentsWorkDailytasksJanuaryReferralStrAppStdztnRefRepTrial.xlsx", FileFormat:=51
Application.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
Workbooks.Close
Set objExcel = Nothing
MsgBox ("Saved")
End Sub
performance vba excel
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
I have a vba code which takes data from SQL database and dumps it into excel. I see that my query should extract approximately total of 120k records. I monitored this activity and learnt that even after 8 hours of my office time, the query is successful in extracting barely 70k records.
This is frustrating me as I am totally new to VBA. Can you guys help me here by modifying my code?
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = True
Dim Conn
Dim RS
Dim SQL
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Conn = CreateObject("ADODB.Connection")
Conn.Open = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Set RS = Conn.Execute(SQL)
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
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.Close
Conn.Close
Application.DisplayAlerts = False
'Release memory
Set objFSO = Nothing
Set objFolder = Nothing
Set objFile = Nothing
ActiveWorkbook.Save
'objWorkbook.SaveAs Filename:="C:\UserskursekarDocumentsWorkDailytasksJanuaryReferralStrAppStdztnRefRepTrial.xlsx", FileFormat:=51
Application.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
Workbooks.Close
Set objExcel = Nothing
MsgBox ("Saved")
End Sub
performance vba excel
performance vba excel
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited 2 hours ago
Toby Speight
27.9k742120
27.9k742120
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked 3 hours ago
Kaustubh UrsekarKaustubh Ursekar
364
364
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
2
$begingroup$
ConsiderRange.CopyFromRecordsetinstead of writing one single cell at a time.
$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
1
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago
add a comment |
2
$begingroup$
ConsiderRange.CopyFromRecordsetinstead of writing one single cell at a time.
$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
1
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago
2
2
$begingroup$
Consider
Range.CopyFromRecordset instead of writing one single cell at a time.$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Consider
Range.CopyFromRecordset instead of writing one single cell at a time.$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
1
1
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
Range.CopyFromRecordset only addresses the [massive] performance issue of traversing an entire recordset row by agonizing row and writing it to a worksheet cell by agonizing cell - all while Excel painstakingly repaints itself every time, fires Worksheet.Change events, and evaluates whether or not recalculations should be happening... between every single worksheet write.
Whenever you programmatically interact with a worksheet, it's a good idea to turn off screen updating, event firing, and make calculations manual to avoid this overhead:
With objExcel
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
And then, don't forget to toggle this Application state back on, and handle runtime errors to make sure it's toggled back on regardless of whether an error occurs or not. Note that any code that involves I/O or a database connection, should handle run-time errors. Right now if the connection times out or if there's a syntax error in that SQL statement, the error is unhandled. I'd recommend something like this:
Public Sub DoSomething()
On Error GoTo CleanFail
'...do stuff...
CleanExit:
'...clean up: restore state, close open connections, etc...
Exit Sub
CleanFail:
'log error, warn user, etc.
Resume CleanExit
End Sub
You are not consistently declaring your variables: the fact that the code can even compile & run with undeclared variables, means you haven't specified Option Explicit at the top of the module. This is a very common beginner trap: VBA is very permissive and lets you do this - doesn't mean you should though. By specifying Option Explicit at the top of every module, you force yourself to declare all variables - which turns a typo into a compile error instead of a very hard-to-diagnose run-time bug.
Activating the active sheet is redundant:
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
Rule of thumb, you pretty much never need to Activate anything - especially if you mean to work "in the background" with a hidden application instance. Speaking of which...
Set objExcel = CreateObject("Excel.Application")
You're hosted in Excel: the Excel type library has to be referenced. There is no reason whatsoever to use CreateObject for this. The New keyword is used for creating objects for which the type is known at compile-time:
Set objExcel = New Excel.Application
Avoid CreateObject whenever possible: it's hitting the Windows Registry, looking up the provided ProgID string, then finds the corresponding class, loads the type from the library, creates an instance, and returns it. Between this:
Set RS = Conn.Execute(SQL)
And this:
Set RS = CreateObject("ADODB.Recordset")
RS.Open SQL, Conn
I take Conn.Execute any day. So you're also using late binding for ADODB:
Dim Conn
Dim RS
Dim SQL
Conn and RS should be declared As Object, and SQL should be As String. As it stands, all 3 are implicit Variant. But ideally, you would be referencing the ADODB library, and declare Conn As ADODB.Connection and RS As ADODB.Recordset, creating the connection with Set Conn = New ADODB.Connection.
Note that While...Wend loops were made obsolete when Do While...Loop was introduced, a long time ago: avoid While...Wend - these loops can't be exited without a GoTo jump, but you can early-exit a Do loop with Exit Do.
Watch out for implicit ByVal expressions here:
MsgBox ("Saved")
This takes the "Saved" string literal, evaluates it as an expression (yielding... a string literal), and passes the result ByVal to the MsgBox function. The parentheses are redundant and harmful!
MsgBox "Saved"
Note that this wouldn't compile:
MsgBox ("Saved", vbOkOnly)
Because ("Saved", vbOkOnly) isn't a legal expression that can be evaluated.
Lastly, note that a lot of everything mentioned above (and more) would have been picked up by the Code Inspections of Rubberduck, a VBIDE add-in open-source project I contribute to (along with a merry bunch of fellow VBA reviewers - star us on GitHub if you like!) - I'm obviously biased, but I can't recommend it enough. The project's blog is also a valuable resource for various VBA topics, from late binding to object-oriented programming and modern best-practices.
$endgroup$
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
add a comment |
$begingroup$
As per the comment from Mathieu, I modified the code. The code is given below; Works like a charm !!! barely 3 minutes and entire process is done. Thank you for your all help. This is being pasted for information purpose to others. I am new to VBA so its for other beginners like me. Take Care all. BYE !!!!
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = False
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Dim SQL
Dim Sconnect
Sconnect = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Conn.Open Sconnect
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
RS.Open SQL, Conn
Sheet.Range("A2").CopyFromRecordset RS
RS.Close
Conn.Close
objExcel.DisplayAlerts = False
'Release memory
'Set objFSO = Nothing
'Set objFolder = Nothing
'Set objFile = Nothing
objWorkbook.Save
objExcel.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
'Set objExcel = Nothing
MsgBox ("Saved")
End Sub
```
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "196"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Kaustubh Ursekar is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219371%2fwrite-to-excel-from-sql-db-using-vba-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
$begingroup$
Range.CopyFromRecordset only addresses the [massive] performance issue of traversing an entire recordset row by agonizing row and writing it to a worksheet cell by agonizing cell - all while Excel painstakingly repaints itself every time, fires Worksheet.Change events, and evaluates whether or not recalculations should be happening... between every single worksheet write.
Whenever you programmatically interact with a worksheet, it's a good idea to turn off screen updating, event firing, and make calculations manual to avoid this overhead:
With objExcel
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
And then, don't forget to toggle this Application state back on, and handle runtime errors to make sure it's toggled back on regardless of whether an error occurs or not. Note that any code that involves I/O or a database connection, should handle run-time errors. Right now if the connection times out or if there's a syntax error in that SQL statement, the error is unhandled. I'd recommend something like this:
Public Sub DoSomething()
On Error GoTo CleanFail
'...do stuff...
CleanExit:
'...clean up: restore state, close open connections, etc...
Exit Sub
CleanFail:
'log error, warn user, etc.
Resume CleanExit
End Sub
You are not consistently declaring your variables: the fact that the code can even compile & run with undeclared variables, means you haven't specified Option Explicit at the top of the module. This is a very common beginner trap: VBA is very permissive and lets you do this - doesn't mean you should though. By specifying Option Explicit at the top of every module, you force yourself to declare all variables - which turns a typo into a compile error instead of a very hard-to-diagnose run-time bug.
Activating the active sheet is redundant:
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
Rule of thumb, you pretty much never need to Activate anything - especially if you mean to work "in the background" with a hidden application instance. Speaking of which...
Set objExcel = CreateObject("Excel.Application")
You're hosted in Excel: the Excel type library has to be referenced. There is no reason whatsoever to use CreateObject for this. The New keyword is used for creating objects for which the type is known at compile-time:
Set objExcel = New Excel.Application
Avoid CreateObject whenever possible: it's hitting the Windows Registry, looking up the provided ProgID string, then finds the corresponding class, loads the type from the library, creates an instance, and returns it. Between this:
Set RS = Conn.Execute(SQL)
And this:
Set RS = CreateObject("ADODB.Recordset")
RS.Open SQL, Conn
I take Conn.Execute any day. So you're also using late binding for ADODB:
Dim Conn
Dim RS
Dim SQL
Conn and RS should be declared As Object, and SQL should be As String. As it stands, all 3 are implicit Variant. But ideally, you would be referencing the ADODB library, and declare Conn As ADODB.Connection and RS As ADODB.Recordset, creating the connection with Set Conn = New ADODB.Connection.
Note that While...Wend loops were made obsolete when Do While...Loop was introduced, a long time ago: avoid While...Wend - these loops can't be exited without a GoTo jump, but you can early-exit a Do loop with Exit Do.
Watch out for implicit ByVal expressions here:
MsgBox ("Saved")
This takes the "Saved" string literal, evaluates it as an expression (yielding... a string literal), and passes the result ByVal to the MsgBox function. The parentheses are redundant and harmful!
MsgBox "Saved"
Note that this wouldn't compile:
MsgBox ("Saved", vbOkOnly)
Because ("Saved", vbOkOnly) isn't a legal expression that can be evaluated.
Lastly, note that a lot of everything mentioned above (and more) would have been picked up by the Code Inspections of Rubberduck, a VBIDE add-in open-source project I contribute to (along with a merry bunch of fellow VBA reviewers - star us on GitHub if you like!) - I'm obviously biased, but I can't recommend it enough. The project's blog is also a valuable resource for various VBA topics, from late binding to object-oriented programming and modern best-practices.
$endgroup$
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
add a comment |
$begingroup$
Range.CopyFromRecordset only addresses the [massive] performance issue of traversing an entire recordset row by agonizing row and writing it to a worksheet cell by agonizing cell - all while Excel painstakingly repaints itself every time, fires Worksheet.Change events, and evaluates whether or not recalculations should be happening... between every single worksheet write.
Whenever you programmatically interact with a worksheet, it's a good idea to turn off screen updating, event firing, and make calculations manual to avoid this overhead:
With objExcel
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
And then, don't forget to toggle this Application state back on, and handle runtime errors to make sure it's toggled back on regardless of whether an error occurs or not. Note that any code that involves I/O or a database connection, should handle run-time errors. Right now if the connection times out or if there's a syntax error in that SQL statement, the error is unhandled. I'd recommend something like this:
Public Sub DoSomething()
On Error GoTo CleanFail
'...do stuff...
CleanExit:
'...clean up: restore state, close open connections, etc...
Exit Sub
CleanFail:
'log error, warn user, etc.
Resume CleanExit
End Sub
You are not consistently declaring your variables: the fact that the code can even compile & run with undeclared variables, means you haven't specified Option Explicit at the top of the module. This is a very common beginner trap: VBA is very permissive and lets you do this - doesn't mean you should though. By specifying Option Explicit at the top of every module, you force yourself to declare all variables - which turns a typo into a compile error instead of a very hard-to-diagnose run-time bug.
Activating the active sheet is redundant:
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
Rule of thumb, you pretty much never need to Activate anything - especially if you mean to work "in the background" with a hidden application instance. Speaking of which...
Set objExcel = CreateObject("Excel.Application")
You're hosted in Excel: the Excel type library has to be referenced. There is no reason whatsoever to use CreateObject for this. The New keyword is used for creating objects for which the type is known at compile-time:
Set objExcel = New Excel.Application
Avoid CreateObject whenever possible: it's hitting the Windows Registry, looking up the provided ProgID string, then finds the corresponding class, loads the type from the library, creates an instance, and returns it. Between this:
Set RS = Conn.Execute(SQL)
And this:
Set RS = CreateObject("ADODB.Recordset")
RS.Open SQL, Conn
I take Conn.Execute any day. So you're also using late binding for ADODB:
Dim Conn
Dim RS
Dim SQL
Conn and RS should be declared As Object, and SQL should be As String. As it stands, all 3 are implicit Variant. But ideally, you would be referencing the ADODB library, and declare Conn As ADODB.Connection and RS As ADODB.Recordset, creating the connection with Set Conn = New ADODB.Connection.
Note that While...Wend loops were made obsolete when Do While...Loop was introduced, a long time ago: avoid While...Wend - these loops can't be exited without a GoTo jump, but you can early-exit a Do loop with Exit Do.
Watch out for implicit ByVal expressions here:
MsgBox ("Saved")
This takes the "Saved" string literal, evaluates it as an expression (yielding... a string literal), and passes the result ByVal to the MsgBox function. The parentheses are redundant and harmful!
MsgBox "Saved"
Note that this wouldn't compile:
MsgBox ("Saved", vbOkOnly)
Because ("Saved", vbOkOnly) isn't a legal expression that can be evaluated.
Lastly, note that a lot of everything mentioned above (and more) would have been picked up by the Code Inspections of Rubberduck, a VBIDE add-in open-source project I contribute to (along with a merry bunch of fellow VBA reviewers - star us on GitHub if you like!) - I'm obviously biased, but I can't recommend it enough. The project's blog is also a valuable resource for various VBA topics, from late binding to object-oriented programming and modern best-practices.
$endgroup$
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
add a comment |
$begingroup$
Range.CopyFromRecordset only addresses the [massive] performance issue of traversing an entire recordset row by agonizing row and writing it to a worksheet cell by agonizing cell - all while Excel painstakingly repaints itself every time, fires Worksheet.Change events, and evaluates whether or not recalculations should be happening... between every single worksheet write.
Whenever you programmatically interact with a worksheet, it's a good idea to turn off screen updating, event firing, and make calculations manual to avoid this overhead:
With objExcel
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
And then, don't forget to toggle this Application state back on, and handle runtime errors to make sure it's toggled back on regardless of whether an error occurs or not. Note that any code that involves I/O or a database connection, should handle run-time errors. Right now if the connection times out or if there's a syntax error in that SQL statement, the error is unhandled. I'd recommend something like this:
Public Sub DoSomething()
On Error GoTo CleanFail
'...do stuff...
CleanExit:
'...clean up: restore state, close open connections, etc...
Exit Sub
CleanFail:
'log error, warn user, etc.
Resume CleanExit
End Sub
You are not consistently declaring your variables: the fact that the code can even compile & run with undeclared variables, means you haven't specified Option Explicit at the top of the module. This is a very common beginner trap: VBA is very permissive and lets you do this - doesn't mean you should though. By specifying Option Explicit at the top of every module, you force yourself to declare all variables - which turns a typo into a compile error instead of a very hard-to-diagnose run-time bug.
Activating the active sheet is redundant:
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
Rule of thumb, you pretty much never need to Activate anything - especially if you mean to work "in the background" with a hidden application instance. Speaking of which...
Set objExcel = CreateObject("Excel.Application")
You're hosted in Excel: the Excel type library has to be referenced. There is no reason whatsoever to use CreateObject for this. The New keyword is used for creating objects for which the type is known at compile-time:
Set objExcel = New Excel.Application
Avoid CreateObject whenever possible: it's hitting the Windows Registry, looking up the provided ProgID string, then finds the corresponding class, loads the type from the library, creates an instance, and returns it. Between this:
Set RS = Conn.Execute(SQL)
And this:
Set RS = CreateObject("ADODB.Recordset")
RS.Open SQL, Conn
I take Conn.Execute any day. So you're also using late binding for ADODB:
Dim Conn
Dim RS
Dim SQL
Conn and RS should be declared As Object, and SQL should be As String. As it stands, all 3 are implicit Variant. But ideally, you would be referencing the ADODB library, and declare Conn As ADODB.Connection and RS As ADODB.Recordset, creating the connection with Set Conn = New ADODB.Connection.
Note that While...Wend loops were made obsolete when Do While...Loop was introduced, a long time ago: avoid While...Wend - these loops can't be exited without a GoTo jump, but you can early-exit a Do loop with Exit Do.
Watch out for implicit ByVal expressions here:
MsgBox ("Saved")
This takes the "Saved" string literal, evaluates it as an expression (yielding... a string literal), and passes the result ByVal to the MsgBox function. The parentheses are redundant and harmful!
MsgBox "Saved"
Note that this wouldn't compile:
MsgBox ("Saved", vbOkOnly)
Because ("Saved", vbOkOnly) isn't a legal expression that can be evaluated.
Lastly, note that a lot of everything mentioned above (and more) would have been picked up by the Code Inspections of Rubberduck, a VBIDE add-in open-source project I contribute to (along with a merry bunch of fellow VBA reviewers - star us on GitHub if you like!) - I'm obviously biased, but I can't recommend it enough. The project's blog is also a valuable resource for various VBA topics, from late binding to object-oriented programming and modern best-practices.
$endgroup$
Range.CopyFromRecordset only addresses the [massive] performance issue of traversing an entire recordset row by agonizing row and writing it to a worksheet cell by agonizing cell - all while Excel painstakingly repaints itself every time, fires Worksheet.Change events, and evaluates whether or not recalculations should be happening... between every single worksheet write.
Whenever you programmatically interact with a worksheet, it's a good idea to turn off screen updating, event firing, and make calculations manual to avoid this overhead:
With objExcel
.ScreenUpdating = False
.EnableEvents = False
.Calculation = xlCalculationManual
End With
And then, don't forget to toggle this Application state back on, and handle runtime errors to make sure it's toggled back on regardless of whether an error occurs or not. Note that any code that involves I/O or a database connection, should handle run-time errors. Right now if the connection times out or if there's a syntax error in that SQL statement, the error is unhandled. I'd recommend something like this:
Public Sub DoSomething()
On Error GoTo CleanFail
'...do stuff...
CleanExit:
'...clean up: restore state, close open connections, etc...
Exit Sub
CleanFail:
'log error, warn user, etc.
Resume CleanExit
End Sub
You are not consistently declaring your variables: the fact that the code can even compile & run with undeclared variables, means you haven't specified Option Explicit at the top of the module. This is a very common beginner trap: VBA is very permissive and lets you do this - doesn't mean you should though. By specifying Option Explicit at the top of every module, you force yourself to declare all variables - which turns a typo into a compile error instead of a very hard-to-diagnose run-time bug.
Activating the active sheet is redundant:
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
Rule of thumb, you pretty much never need to Activate anything - especially if you mean to work "in the background" with a hidden application instance. Speaking of which...
Set objExcel = CreateObject("Excel.Application")
You're hosted in Excel: the Excel type library has to be referenced. There is no reason whatsoever to use CreateObject for this. The New keyword is used for creating objects for which the type is known at compile-time:
Set objExcel = New Excel.Application
Avoid CreateObject whenever possible: it's hitting the Windows Registry, looking up the provided ProgID string, then finds the corresponding class, loads the type from the library, creates an instance, and returns it. Between this:
Set RS = Conn.Execute(SQL)
And this:
Set RS = CreateObject("ADODB.Recordset")
RS.Open SQL, Conn
I take Conn.Execute any day. So you're also using late binding for ADODB:
Dim Conn
Dim RS
Dim SQL
Conn and RS should be declared As Object, and SQL should be As String. As it stands, all 3 are implicit Variant. But ideally, you would be referencing the ADODB library, and declare Conn As ADODB.Connection and RS As ADODB.Recordset, creating the connection with Set Conn = New ADODB.Connection.
Note that While...Wend loops were made obsolete when Do While...Loop was introduced, a long time ago: avoid While...Wend - these loops can't be exited without a GoTo jump, but you can early-exit a Do loop with Exit Do.
Watch out for implicit ByVal expressions here:
MsgBox ("Saved")
This takes the "Saved" string literal, evaluates it as an expression (yielding... a string literal), and passes the result ByVal to the MsgBox function. The parentheses are redundant and harmful!
MsgBox "Saved"
Note that this wouldn't compile:
MsgBox ("Saved", vbOkOnly)
Because ("Saved", vbOkOnly) isn't a legal expression that can be evaluated.
Lastly, note that a lot of everything mentioned above (and more) would have been picked up by the Code Inspections of Rubberduck, a VBIDE add-in open-source project I contribute to (along with a merry bunch of fellow VBA reviewers - star us on GitHub if you like!) - I'm obviously biased, but I can't recommend it enough. The project's blog is also a valuable resource for various VBA topics, from late binding to object-oriented programming and modern best-practices.
answered 1 hour ago
Mathieu GuindonMathieu Guindon
61.2k14150420
61.2k14150420
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
add a comment |
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
Hi Mathieu. Thanks very much for such detailed post. i shall research this and write a new code and post it here for you guys to review it and then probably you can criticise it further for me to come up with a more sophisticated work. I need to deploy this in production so I wish to be as fine tuned as I could be. Thanks once again. Really appreciate all the details you mentioned here for me !!!
$endgroup$
– Kaustubh Ursekar
49 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
@KaustubhUrsekar that's what this site is all about! =) don't hesitate to post a follow-up with the updated code, in a new "question" (I'd recommend reviewing Rubberduck inspections first though).
$endgroup$
– Mathieu Guindon
47 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
Sure !!. I shall take your description as an answer on this post brother. I shall surely research the stuff you told me and get back to you in a new question. Feeling Happy !! hehehe.
$endgroup$
– Kaustubh Ursekar
40 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
BTW Rubberduck is written in C#, if you're ever looking for a fun & challenging OSS project to contribute to - we need all the help we can get!
$endgroup$
– Mathieu Guindon
21 mins ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
$begingroup$
Yes sure mahn. I have developed webpages and websites with C# and .net to be precise. So I would really love to be a "strong" no matter how small or big part of something new and more. Would surely hone my skills with you guys. I wish to learn as much i could. I stepped into computer science field just 4 months back and I see so much in here !!!
$endgroup$
– Kaustubh Ursekar
1 min ago
add a comment |
$begingroup$
As per the comment from Mathieu, I modified the code. The code is given below; Works like a charm !!! barely 3 minutes and entire process is done. Thank you for your all help. This is being pasted for information purpose to others. I am new to VBA so its for other beginners like me. Take Care all. BYE !!!!
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = False
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Dim SQL
Dim Sconnect
Sconnect = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Conn.Open Sconnect
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
RS.Open SQL, Conn
Sheet.Range("A2").CopyFromRecordset RS
RS.Close
Conn.Close
objExcel.DisplayAlerts = False
'Release memory
'Set objFSO = Nothing
'Set objFolder = Nothing
'Set objFile = Nothing
objWorkbook.Save
objExcel.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
'Set objExcel = Nothing
MsgBox ("Saved")
End Sub
```
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
As per the comment from Mathieu, I modified the code. The code is given below; Works like a charm !!! barely 3 minutes and entire process is done. Thank you for your all help. This is being pasted for information purpose to others. I am new to VBA so its for other beginners like me. Take Care all. BYE !!!!
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = False
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Dim SQL
Dim Sconnect
Sconnect = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Conn.Open Sconnect
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
RS.Open SQL, Conn
Sheet.Range("A2").CopyFromRecordset RS
RS.Close
Conn.Close
objExcel.DisplayAlerts = False
'Release memory
'Set objFSO = Nothing
'Set objFolder = Nothing
'Set objFile = Nothing
objWorkbook.Save
objExcel.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
'Set objExcel = Nothing
MsgBox ("Saved")
End Sub
```
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
add a comment |
$begingroup$
As per the comment from Mathieu, I modified the code. The code is given below; Works like a charm !!! barely 3 minutes and entire process is done. Thank you for your all help. This is being pasted for information purpose to others. I am new to VBA so its for other beginners like me. Take Care all. BYE !!!!
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = False
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Dim SQL
Dim Sconnect
Sconnect = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Conn.Open Sconnect
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
RS.Open SQL, Conn
Sheet.Range("A2").CopyFromRecordset RS
RS.Close
Conn.Close
objExcel.DisplayAlerts = False
'Release memory
'Set objFSO = Nothing
'Set objFolder = Nothing
'Set objFile = Nothing
objWorkbook.Save
objExcel.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
'Set objExcel = Nothing
MsgBox ("Saved")
End Sub
```
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
$endgroup$
As per the comment from Mathieu, I modified the code. The code is given below; Works like a charm !!! barely 3 minutes and entire process is done. Thank you for your all help. This is being pasted for information purpose to others. I am new to VBA so its for other beginners like me. Take Care all. BYE !!!!
Macro1
Private Sub Macro1()
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:UserskursekarDocumentsWorkAppsReferralStrAppStdztnRefRepTrial.xlsx")
objExcel.Visible = False
Set Conn = CreateObject("ADODB.Connection")
Set RS = CreateObject("ADODB.Recordset")
Dim SQL
Dim Sconnect
Sconnect = "Provider=SQLOLEDB.1;Password='25LaurelRoad';User ID='CPSMDITkursekar';Data Source='analyzer';Initial Catalog='analyzer_str';Integrated Security=SSPI; Persist Security Info=True;"
Conn.Open Sconnect
SQL = "WITH cte_REFERRALS_REPORTS(referralnum, refer_from, refer_from_name, refer_from_id, refer_to, refer_to_name, refer_to_id) AS (SELECT referralnum, refer_from, CASE WHEN refer_from_id = 'R' THEN RdicF.refname WHEN refer_from_id = 'P' THEN PdicF.provname END AS refer_from_name, refer_from_id, refer_to, "
SQL = SQL & "CASE WHEN refer_to_id = 'R' THEN RdicT.refname WHEN refer_to_id = 'P' THEN PdicT.provname END AS refer_to_name, refer_to_id FROM referral_t r Left Join refcode_t RdicF ON r.refer_from = CASE WHEN r.refer_from_id='R' THEN RdicF.refcode ELSE NULL END Left Join refcode_t RdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'R' THEN RdicT.refcode ELSE NULL END "
SQL = SQL & "Left Join provcode_t PdicF ON r.refer_from = CASE WHEN r.refer_from_id = 'P' THEN PdicF.provcode ELSE NULL END Left Join provcode_t PdicT ON r.refer_to = CASE WHEN r.refer_to_id = 'P' THEN PdicT.provcode ELSE NULL END ) SELECT chgslipno , a.acctno, patfname, patlname, appt_date, a.enccode, pr.provname "
SQL = SQL & ",a.provcode, rfc.refname, a.refcode, r1.refer_from as r1_ref_from, r1.refer_from_id as r1_ref_from_id, r1.refer_from_name as r1_ref_from_name, a.referral1 as r1_refnum, r2.refer_from as r2_ref_from, r2.refer_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 pr.provcode = a.provcode LEFT JOIN refcode_t rfc ON a.refcode = rfc.refcode LEFT JOIN (SELECT*FROM cte_REFERRALS_REPORTS) r1 ON 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 JOIN specialty_t s on pr.speccode = s.speccode LEFT 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','IMC','NP30') AND UPPER(a.appt_status)='ARR' AND appt_date >= '2017-01-01' "
SQL = SQL & "ORDER BY a.acctno"
Set Sheet = objWorkbook.ActiveSheet
Sheet.Activate
RS.Open SQL, Conn
Sheet.Range("A2").CopyFromRecordset RS
RS.Close
Conn.Close
objExcel.DisplayAlerts = False
'Release memory
'Set objFSO = Nothing
'Set objFolder = Nothing
'Set objFile = Nothing
objWorkbook.Save
objExcel.DisplayAlerts = True
objWorkbook.Close
objExcel.Workbooks.Close
objExcel.Quit
'Set objExcel = Nothing
MsgBox ("Saved")
End Sub
```
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 1 hour ago
Kaustubh UrsekarKaustubh Ursekar
364
364
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Kaustubh Ursekar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
Kaustubh Ursekar is a new contributor. Be nice, and check out our Code of Conduct.
Kaustubh Ursekar is a new contributor. Be nice, and check out our Code of Conduct.
Kaustubh Ursekar is a new contributor. Be nice, and check out our Code of Conduct.
Kaustubh Ursekar is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f219371%2fwrite-to-excel-from-sql-db-using-vba-script%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
$begingroup$
Consider
Range.CopyFromRecordsetinstead of writing one single cell at a time.$endgroup$
– Mathieu Guindon
2 hours ago
$begingroup$
Hi brother !!! Thank you very much for your help. It worked like a magic. Here is my new pasted code which works !!!!!! Thank you so muach mahn. Saved the day !!! hehehe. Take Care. God Bless You.
$endgroup$
– Kaustubh Ursekar
1 hour ago
1
$begingroup$
For a VBA new user I recommend one of the excel programming books by John Walkenbach at spreadsheetpage.com. That is how I learned to program VBA.
$endgroup$
– pacmaninbw
1 hour ago
$begingroup$
Yes. I have started VBA 3 weeks back by reading documentations. My internship is requiring this skill which I was totally not even asked in interview. lol. I know some C#. So any recommendations for books and websites are warmly and cheerfully welcomed. Please help me know more bros.
$endgroup$
– Kaustubh Ursekar
1 hour ago