-
Here is an example of some code that I am working on, it is a very basic example:
Dim test1(8) As Integer
Dim test2(8) As Integer
Dim a As Integer
dim j As Integer
test1(1) = 1
test1(2) = 2
test1(3) = 1
test1(4) = 3
test1(5) = 8
test1(6) = 2
test1(7) = 8
test1(8) = 3
For j = 1 To 8
a = test1(j)
test2(j) = a
Next j
End Sub
This is my dillema
How can I add to the second array the values in the first array, here is the tricky part. I do not want duplicates.
so at the end the second array will have the values: 1, 2, 8 and 3. (I know I will have empty slots in the array, but I just want see all the values withough duplicates) Can this be done, thanks
-
You can do something like this:
Code:
Private Function HasDuplicates(pArr() As Integer, pValue As Integer) As Boolean
Dim i As Integer
On Error GoTo ErrHnd
For i = LBound(pArr) To UBound(pArr)
If pArr(i) = pValue Then
HasDuplicates = True
Exit Function
End If
Next
Exit Function
ErrHnd:
'Don't do anything here, the function will return False
End Function
Private Sub Command1_Click()
Dim arrOne(1 To 8) As Integer
Dim arrTwo() As Integer
Dim i As Integer
Dim intIndex As Integer
arrOne(1) = 1
arrOne(2) = 2
arrOne(3) = 1
arrOne(4) = 3
arrOne(5) = 8
arrOne(6) = 2
arrOne(7) = 8
arrOne(8) = 3
On Error Resume Next
For i = LBound(arrOne) To UBound(arrOne)
If Not HasDuplicates(arrTwo, arrOne(i)) Then
ReDim Preserve arrTwo(intIndex)
arrTwo(intIndex) = arrOne(i)
intIndex = intIndex + 1
End If
Next
End Sub
Also, pay attention how I declared the array (arrOne(1 To 8)). This way you can use the way you did.
-
Dim test1(8) As Integer
Dim test2(8) As Integer
Dim a As Integer
dim j As Integer, i as Integer
Dim bolFound as Boolean
Dim intTest2Counter as Integer
test1(1) = 1
test1(2) = 2
test1(3) = 1
test1(4) = 3
test1(5) = 8
test1(6) = 2
test1(7) = 8
test1(8) = 3
intTest2Counter = 0
For j = 1 To 8
bolFound = False
for i = 1 to 8
if test2(i) = test1(j) then
bolFound = True
exit for
end if
next i
if not found then
test2(intTest2Counter) = test1(j)
intTest2Counter = intTest2Counter + 1
end if
Next j
End Sub
-
He he he.....Steel has my signature.....that is so funny...lol
-
You can add the code I put in the For loop. This will create a stringand search the string for the exsistence of your previous number. You still need to follow Serge's suggestions about your Dim. Also you could run into limitations of the text string if you are using a large array.
Code:
Dim test1(8) As Integer
Dim test2(8) As Integer
Dim A As Integer
Dim j As Integer
Dim strTemp As String
test1(1) = 1
test1(2) = 2
test1(3) = 1
test1(4) = 3
test1(5) = 8
test1(6) = 2
test1(7) = 8
test1(8) = 3
For j = 1 To 8
If InStr(1, strTemp, "'" & test1(j) & "',", vbBinaryCompare) = 0 Then
strTemp = strTemp & "'" & test1(j) & "',"
test2(j) = test1(j)
End If
Next j
MsgBox strTemp