My first question was answered so quickly that I thought i’d ask another question! 🙂
It’s probably really simple for you guys, but I can’t seem to get it.
I’m trying to convert a simple integer array to a string array because, with the problem i have, it easy to crossover a string rather than a int array (or so I think!)
But here is the code so far:
Dim Parent1s() As String For i = 1 To ChromoLength.Value y = y + Val(MyArray(rnd2, i)) q = Val(MyArray(rnd2, i)) Parents1(i) = q Next i For i = 0 To ChromoLength.Value Parent1s(i) = Val(Parents1(i)) Next i 'Cells(8, 1) = Parent1s
I can’t sem to get Parent1s to produce a string, keep getting a mismatch error
Everything else has been dim’d correctly cause the rest of the code works when i hash out the parent1s statements.
You can use the
CStr() function to convert values to
Sub toString() Dim i As Integer Dim iStr As String i = 5 iStr = CStr(i) MsgBox (iStr) End Sub
However, I suspect your problems may be because you have not used
Option Explicit and are running into problems with variables being named
Parent1s as well as
Parents1. If you intended for these to be so similar I would strongly encourage a better naming convention. Otherwise, the following seems like it should work:
For i = 0 To ChromoLength.Value Parent1s(i) = CStr(Parents1(i)) Next i
If you are attempting to use
Parent1s(i) = Val(Parents1(i)) to convert
Parents1 to strings, this is not the right way to do it.
Val attempts to convert a string, that may contain numbers and letters, to a numeric double. Actually, assuming Parents1 is a standard data type it should implicitly convert to a string with no problems. But if it doesn’t, you can always use
This may not be your issue, but you never dimension Parent1s().
Parent1s() add this line:
Redim Parent1s(0 to ChromoLength.Value)
Btw, if you’ve actually dimmed all of your variables and chosen your names deliberately, you should try to use variable names that will be clear to someone else (which incidentally might mean yourself in a year).
In the future and also to help us help you better, you should either include the lines where you dimension your variables in your sample code or specifically tell us what data types you are using for each variable.