Excel VBA Code - OELDB Connection -
I am trying to get data from an Excel file with the code below, and when it comes to the GetData macro Goes through automatically after the rsData.Open command goes out, then it never drops off data from the workbook closed in the open workbook.
as as Public Sub GetData (Sourcefile version, SourceSheet String, _ SourceRange string range as TargetRange, _ Header Boolean, UseHeaderRow Boolean) as dim rsCon As the object dims in the form of RSData, the object dims szConnect string as the slow szSQL string as long as the 'lCount' string make connections as as. If the header = is false, then the application.Version & lt; 12 then szConnect = "provider = Microsoft.Jet.OLEDB.4.0;" & Amp; _ "Data source =" & amp; SourceFile & amp; "," & Amp; _ "Extended Properties =" "Excel 8.0; HDR =" ";" Other szConnect = "Provider = Microsoft.ACE.OLEDB.12.0;" & Amp; _ "Data source =" & amp; SourceFile & amp; "," & Amp; _ "Extended Properties =" "Excel 12.0; HDR =" ";" If otherwise, Wal (Application Edition) & lt; 12 then szConnect = "provider = Microsoft.Jet.OLEDB.4.0;" & Amp; _ "Data source =" & amp; SourceFile & amp; "," & Amp; _ "Extended Properties =" "Excel 8.0; HDR = Yes"; " Other szConnect = "Provider = Microsoft.ACE.OLEDB.12.0;" & Amp; _ "Data source =" & amp; SourceFile & amp; "," & Amp; _ "Extended Properties =" "Excel 12.0; HDR = Yes"; " End if end if sourced source = "" then workbook level name szSQL = "SELECT * FROM" & amp; SourceRange $ & amp; "," Other 'worksheet level name or category szSQL = "SELECT * FROM [" & amp; SourceSheet $ & amp; "$" & Amp; SourceRange $ & amp; To ensure "]," finally on error goto SomethingWrong set rsCon = CreateObject ( "ADODB.Connection") set rsData = CreateObject ( "ADODB.Recordset") rsCon.Open szConnect rsData.Open szSQL, rsCon, 0, 1, 1 ' check for if we make the data and copy data not rsData.EOF and if header = false TargetRange.Cells (1, 1) .CopyFromRecordset rsData End if other MsgBox "did not return any records:" & amp ; SourceFile, vbCritical End If 'Set to clean up our Rikorset object RsData.Close rsData = nothing nothing rsCon.Close set rsCon = something out everything Exit: MsgBox "File name, sheet name or range is invalid : "& Amp; Sourcefile, _ vbExclamation, "error" goto error 0 End Sub Sub Update_DW_BaseCase () Application.Calculation = xlCalculationSemiautomatic Application.DisplayAlerts = false Application.EnableEvents = false Application.ScreenUpdating = false error resume next folder = Application.ActiveWorkbook. Path & amp; "\" Dw_file_name = ActiveWorkbook.Name Model = Dir (folder and "* base case.xls *") erov = 2 erov_descriptives = 2 error_string = "" len len (model)> 0 GetData model, "Dump data" "A7: EL85", sheet ("data") range ('a' in & amp;; irow), false, wrong irow = sheet ("data") range ("A2"). .End (xlDown) .Row + 1 GetData model, "data dump", "B-3: AE3" sheet ( "Descriptives") in the range ( 'A' & amp; _ irow_descriptives), false, false irow_descriptives = irow_descriptives + 1 model = Dir loop application CULCLECTION = XL CLOULD SAMUATIC APPLICATION DisplayLetter = TrueApplication.asabledAvents = True and SubS
Comments
Post a Comment