Tuesday, June 14, 2016

Get data from SQL Server and paste in a Activesheet VBA

SQL_Set_Connection

'Debug.Print SQL_Str
Set rst = cnt.Execute("budget.dbo.SP_PIP_Excel") '''' '" & CoFilter & "' ,'*','" & cbo_LT & "'")

If rst.RecordCount = 0 Then
  MsgBox "No Data available - contact Administrator"
  'GoTo Finish
End If



Dim RptName As String
Dim RptShtName As String

RptName = "\\dc01cxbudpv01\budget_software\MCE_Report1.xlsx"
RptShtName = "Sheet1"

Dim objXL
Set objXL = CreateObject("Excel.Application")

'AppActivate objXL
Workbooks.Open (RptName)

Workbooks("MCE_Report1.xlsx").Activate
ActiveWorkbook.Sheets("Sheet1").Activate
Debug.Print ActiveWorkbook.Name
Debug.Print ActiveSheet.Name
'objXL.Calculation = xlCalculationAutomatic
'Set WB = objXL.ActiveWorkbook

'On Error Resume Next
'WB.Sheets("Results").Range("Data1").EntireRow.Delete
'WB.Sheets("Results").Range("Data1").CopyFromRecordset rst

ActiveSheet.Range("A1:A10000").CopyFromRecordset rst

No comments: