Querytables.add with sql query text stored in separate text file
first an excel cell can contain up to 32,536 characters. You can only see
the 1st 256 characters. You simply need to open the text fiel with the sql
and read the data.
If you data is on more than one line in the file this may be a problem. You
may have to remove the CR (carraiage return) and LF (LineFeed) from the string
Sub GetQuery()
ReadFile = Application _
.GetOpenFilename(FileFilter:="Text Files (*.txt), *.txt", _
Title:="Select Read File")
If ReadFile = False Then
MsgBox ("No file Selected - Exiting Macro")
End If
Set fs = CreateObject("Scripting.FileSystemObject")
Set fin = fs.OpenTextFile(ReadFile, _
ForReading, TristateTrue)
Sql = fin.readall
Sql = Replace(Sql, vbCrLf, "")
fin.Close
With ActiveSheet.QueryTables.Add(Connection:=ConnText, _
Destination:=Range("B28"))
.CommandText = Sql
.Name = "Dimensions 1 EDI BellsMills TB"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = True
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = True
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False
End With
End Sub
|