Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting a string to an array of integers 1

Status
Not open for further replies.

DeathSurfer

Programmer
Mar 7, 2007
7
US
Duuuudes:

Is there an easy way in vba to convert a string to an array of integers. For example:

I have this particular array of numbers in the element <column_data_types> in a xml file:

<column_data_types>1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 1, 1, 1</column_data_types>

I have a function that will grab the array of numbers but the function returns a string. ("1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 1, 1, 1") For this particular situation I actually need this list of numbers to be numbers, not a string, and pass them to an array in excel. Below is the situation that I'm talking about, I am importing a query from a database and need to fill out the .TextFileColumnDataTypes argument which takes an array of integers. I need this to be dynamic so I put these columndatatypes in an xml file.

With WorkSheet("x").QueryTables.Add(Connection:="TEXT;" & ExcelFilePath & "\FABRICREQ", Destination:=Range("FD_StartData"))
.Name = "FabricData"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = False
.RefreshPeriod = 0
.TextFilePromptOnRefresh = False
.TextFilePlatform = 437
.TextFileStartRow = 1
.TextFileParseType = xlDelimited
.TextFileTextQualifier = xlTextQualifierDoubleQuote
.TextFileConsecutiveDelimiter = False
.TextFileTabDelimiter = False
.TextFileSemicolonDelimiter = False
.TextFileCommaDelimiter = True
.TextFileSpaceDelimiter = False
.TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 1, 1, 1)
.TextFileTrailingMinusNumbers = True
.Refresh BackgroundQuery:=False
End With

Instead of looking like the above: .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5, 1, 1, 1)

it will look like this: .TextFileColumnDataTypes = Array(getXmlValue) except getXmlValue returns a string which is the problem. Any ideas on how I should handle this?

Any help would be appreciated.

Thanks

 



Hi,

One way...
Code:
    a = Split(getXmlValue, ",")
    ReDim b(UBound(a))
    For i = 0 To UBound(a)
        b(i) = Int(a(i))
    Next


Skip,

[glasses] [red][/red]
[tongue]
 
Skip

If Array() returns a Variant array, can't you just
Code:
    a = Split(getXmlValue, ",")
    For i = 0 To UBound(a)
        a(i) = Int(a(i))
    Next
(Untested - on a Linux box at the moment). Or doesn't it work that way?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top