简体   繁体   中英

VBA : QueryTables : how to NOT take into account TextFileOtherDelimiter

I got a troubleshooting with VBA for Excel : When I use

With ActiveSheet.QueryTables.Add(Connection:=ConnString, Destination:=SomeRange)
    .Name = _
    " "
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 65001
    .TextFileStartRow = 1
    .TextFileParseType = xlDelimited
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1)
    .TextFileDecimalSeparator = ","
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With

the import request automatically carries in this import the "other delimiter" that I requested on a manual import.

ie I manually imported a file using "-" as delimiter and the macro is going to behave as if it included the line :

.TextFileOtherDelimiter = "-"

I tried to use

.TextFileOtherDelimiter = False

but it doesn't work, because it behaves as

.TextFileOtherDelimiter = "F"

My question : how can I specify that this import should only be parsed with "," delimiter and not ANY other one (regardless of any other import that can have occured on the machine before?)

For future reference, I did not find the way to force no other delimiter, but the two following workaround seem to work :

  • forcing the otherdelimiter to the same value as the first one (won't work if you have no first delimiter)

     .TextFileOtherDelimiter = "," 
  • using a delimiter that never appears in the file (won't work if any char but the first delimiter could appear in the file)

     .TextFileOtherDelimiter = "|" 

As none of these two workarounds solves the problem in the general case, I don't mark it as suitable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM