PC Review


Reply
Thread Tools Rate Thread

Converting a string to an array of integers

 
 
=?Utf-8?B?RGVhdGhTdXJmZXI=?=
Guest
Posts: n/a
 
      21st Mar 2007
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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      21st Mar 2007
Looks like the Split function is what you're looking for. Split will take a
string and delimiter as input and return an array. Search for "Split
Function" in the Microsoft Visual Basic Help.


--
Hope that helps.

Vergel Adriano


"DeathSurfer" wrote:

> 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
>
>

 
Reply With Quote
 
=?Utf-8?B?RGVhdGhTdXJmZXI=?=
Guest
Posts: n/a
 
      21st Mar 2007
From what I can tell the split function returns the array in a string unless
I'm doing something wrong. This is what I done:

..TextFileColumnDataTypes = Array(Split (getXmlValue, ",", 1,))

The above returned this:

"1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5,
1, 1, 1"

the numbers are in a string again. The function will not accept this.
I'm I doing something wrong?

"Vergel Adriano" wrote:

> Looks like the Split function is what you're looking for. Split will take a
> string and delimiter as input and return an array. Search for "Split
> Function" in the Microsoft Visual Basic Help.
>
>
> --
> Hope that helps.
>
> Vergel Adriano
>
>
> "DeathSurfer" wrote:
>
> > 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
> >
> >

 
Reply With Quote
 
=?Utf-8?B?VmVyZ2VsIEFkcmlhbm8=?=
Guest
Posts: n/a
 
      21st Mar 2007
Hi,

The Split function already returns an array. Try it this way:

..TextFileColumnDataTypes = Split (getXmlValue, ",", 1,)

Hopefully, Excel would be able to do the data conversion automatically. If
that still gives you problems, then, you can take the output from Split and
put the values in an integer array.. here's an example:

Sub test()
Dim strarray As Variant
Dim iarray() As Integer
Dim i As Integer

strarray = Split("1,1,2", ",")
ReDim iarray(UBound(strarray))

For i = 0 To UBound(strarray)
iarray(i) = strarray(i)
Next i

End Sub

In the above code, strarray will be an array of string values ("1", "1",
"2"). iarray will have integer values (1, 1, 2)


--
Hope that helps.

Vergel Adriano


"DeathSurfer" wrote:

> From what I can tell the split function returns the array in a string unless
> I'm doing something wrong. This is what I done:
>
> .TextFileColumnDataTypes = Array(Split (getXmlValue, ",", 1,))
>
> The above returned this:
>
> "1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 5, 5,
> 1, 1, 1"
>
> the numbers are in a string again. The function will not accept this.
> I'm I doing something wrong?
>
> "Vergel Adriano" wrote:
>
> > Looks like the Split function is what you're looking for. Split will take a
> > string and delimiter as input and return an array. Search for "Split
> > Function" in the Microsoft Visual Basic Help.
> >
> >
> > --
> > Hope that helps.
> >
> > Vergel Adriano
> >
> >
> > "DeathSurfer" wrote:
> >
> > > 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
> > >
> > >

 
Reply With Quote
 
=?Utf-8?B?RGVhdGhTdXJmZXI=?=
Guest
Posts: n/a
 
      21st Mar 2007
Duuude:

That works, sorry you had to hold my hand through that.

later.

"DeathSurfer" wrote:

> 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
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Casting an arraylist of integers to a string array Earl Microsoft C# .NET 2 18th Jul 2006 10:40 PM
converting an array to a string =?Utf-8?B?QW5kcmV3?= Microsoft ASP .NET 0 2nd Sep 2005 03:37 PM
Re: Converting a string array Ignacio Machin \( .NET/ C# MVP \) Microsoft C# .NET 2 18th Sep 2004 07:54 AM
Converting a string array =?Utf-8?B?Q2hyaXN0aW5l?= Microsoft C# .NET 0 17th Sep 2004 08:33 PM
Converting a string of integers to an array/arraylist Graeme Downes Microsoft Dot NET 1 12th Jul 2003 08:41 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:40 AM.