vbscript and .QueryTables.Add

Robert Evans

New Member
Joined
Jun 14, 2011
Messages
3
I am writing a generic vbscript that takes delimited files and makes Excel files out of them.

Using
With objWorksheet1.QueryTables.Add("TEXT;C:\Sample.TXT", objWorksheet1.Range("$A$1"))
.Name = "Sample"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = 1
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 1252
.TextFileStartRow = 1
.TextFileParseType = 1
.TextFileTextQualifier = 1
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = False
.TextFileSpaceDelimiter = False
.TextFileOtherDelimiter = "§"
.TextFileColumnDataTypes = Array(2,2,2)
.TextFileTrailingMinusNumbers = True
.Refresh False
End With

This works just fine, however I NEED to have TextFileColumnDataTypes to be variable as each incoming file has a different number of columns and data types.

But if I do
dim ColDataTypes(3)
ColDataTypes(0) = 2
ColDataTypes(1) = 2
ColDataTypes(2) = 2
and change .QueryTables.Add to now say

.TextFileColumnDataTypes = ColDataTypes
or
.TextFileColumnDataTypes = Array(ColDataTypes)

I get "Invalid procedure call or argument"

There must be a way to do this....please help
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
dim ColDataTypes(3)
ColDataTypes(0) = 2
ColDataTypes(1) = 2
ColDataTypes(2) = 2
and change .QueryTables.Add to now say

.TextFileColumnDataTypes = ColDataTypes
VBScript arrays are zero-based, so Dim ColDataTypes(3) declares an array of 4 elements, but you define only 3 elements - (0), (1) and (2). Try changing the Dim to:

Dim ColDataTypes(2)
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top