1
我需要添加表頭以包含在我的VBA代碼中,該代碼已連接到SQL。代碼正在工作(因爲它正在將新的工作表上的SQL結果吐出),但它並沒有抓住頭部。VBA包含SQL查詢標題
有人可以幫忙嗎?
Sub SpectrumADGroupMapping()
'Start Declarations'
Dim Cn As ADODB.Connection
Dim rs As ADODB.Recordset
'Connection string to SQL server/DB (Windows Auth)
Set Cn = New ADODB.Connection
Cn.ConnectionString = "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=True;Data Source=TEST;Use Procedure for Prepare=1;Auto Translate=True;Packet Size=4096;Workstation ID=TESTPC;Use Encryption for Data=False;Tag with column collation when possible=False;Initial Catalog=TESTDBNAME"
Cn.Open
Set and Execute SQL Command
Set rs = New ADODB.Recordset
rs.ActiveConnection = Cn
rs.Open "Select top 50* from TestTable order by creationDate desc"
'Copy Data to Excel
Worksheets.Add(After:=Worksheets(1)).Name = "TestWorkSheet"
Range("A2").CopyFromRecordset rs
Cn.Close
End Sub