PDA

View Full Version : Getting on track with array's



svmseric
09-25-2010, 12:36 PM
Hello,

I'm fairly new to VBA and am stuck on getting data from a text box into an array. After I get the data into an array I need to build a string of integers to separate each digit and do some math calculations then swap the digits from 1 to 3 and 2 to 4. Here is what I have so far. Im not looking for a solution, I just need some help getting started.

Thanks

Private Sub cmdEncrypt_Click()
Dim i As Integer
Dim dataArray(4) As TextBox
For i = 0 To Val(data.Text) + 1
Next i


End Sub

Bob Phillips
09-25-2010, 02:41 PM
Why have you dimmed dataArray as TextBox?

What is data where you get the Text property?

If you get all of the data from one textbox, why not just extract it a digit at a time, no need to swap later?

svmseric
09-25-2010, 03:34 PM
Like I said I am new to arrays, The data in the text box is 4 integers. I am trying to figure out how to extract each integer one by one and manipulate it. After its manipulated change the first digit with the third and the second with the fourth. Yes all the data comes from one text box.

Kenneth Hobs
09-25-2010, 04:27 PM
Sub test()
Dim i As Integer, s As String
Dim dataArray() As String
s = "1234"
i = Len(s)
MsgBox i, , "s length."
ReDim dataArray(i)
For i = 1 To Len(s)
dataArray(i) = Mid(s, i, 1)
Next i
s = Join(dataArray, vbCrLf)
MsgBox s, , "dataArray()"
End Sub

svmseric
09-25-2010, 04:56 PM
Sub test()
Dim i As Integer, s As String
Dim dataArray() As String
s = "1234"
i = Len(s)
MsgBox i, , "s length."
ReDim dataArray(i)
For i = 1 To Len(s)
dataArray(i) = Mid(s, i, 1)
Next i
s = Join(dataArray, vbCrLf)
MsgBox s, , "dataArray()"
End Sub

Thanks a bunch, that helped a lot. I hope I can get the rest of it done now.

Bob Phillips
09-26-2010, 02:09 AM
Like I said I am new to arrays, The data in the text box is 4 integers. I am trying to figure out how to extract each integer one by one and manipulate it. After its manipulated change the first digit with the third and the second with the fourth. Yes all the data comes from one text box.

So as I said, I would do it direct, no loops



Dim s As String
Dim dataArray(1 To 4) As String
s = "1234"
dataArray(1) = Mid(s, 1, 1)
dataArray(2) = Mid(s, 4, 1)
dataArray(3) = Mid(s, 3, 1)
dataArray(4) = Mid(s, 2, 1)
s = Join(dataArray, vbCrLf)
MsgBox s, , "dataArray()"