foozball3000
Executive Member
I'm trying to Import spesific data from a Text file into an excel spreadsheet via VBA. The code is copied from another Excel document that does just that... but somehow it's not working at all.
The original:
The New Code
The original:
Code:
Sub RunTest()
crtPivotTable _
ptTable:="DataTable", _
SqlStmnt:="SELECT * FROM TestData.txt WHERE (Test1='Yes')", _
PgFields:=Array("Val_ind", "flag", "Grouping", "ClientName")
FormatPivotTable _
ptTable:="DataTable"
End Sub
Sub crtPivotTable(ptTable As String, SqlStmnt As String, PgFields, ClFields, RwFields, Descr As String)
...
' Import data selection from text file and create PivotTable
With ActiveWorkbook.PivotCaches.Add(xlExternal)
.Connection = "ODBC;DBQ=Data;DefaultDir=;Driver={Microsoft Text Driver (*.txt; *.csv)};"
.CommandType = xlCmdSql
.CommandText = SqlStmnt
.CreatePivotTable "Report!R10C3", ptTable
End With
...
End Sub
Code:
Sub ImportDocuments(SheetNumber As Integer, DocNumber As String)
Range("A1").Select
With ActiveSheet.QueryTables.Add(xlExternal)
.Connection = "ODBC;DBQ=Data;DefaultDir=;Driver={Microsoft Text Driver (*.txt; *.csv)};"
.CommandType = xlCmdSql
.CommandText = "SELECT * FROM Documents.txt WHERE Document = 'New'"
.Destination = ActiveSheet.Range("A1")
End With
End Sub