Dim s1(2) As Byte For i=0 to 3 If i<>2 then s1(j)=s(i) j=j+1 next
Dim s() As BytePrivate Sub Command1_Click() Dim s1(2) As Byte For i = 0 To 3 If i <> 2 Then s1(j) = s(i) j = j + 1 End If Next s = s1 For i = 0 To UBound(s) Debug.Print s(i) Next End SubPrivate Sub Form_Load() ReDim s(3) s(0) = 10 s(1) = 20 s(2) = 30 s(3) = 40 End Sub
先复制后调整,比如要去掉第n个元素: dim n n=2 '去掉第2个元素 for i=n to ubound(s)-1 s(i)=s(i+1) next redim preserve s(ubound(s)-1)
试试下面的俺写的代码,可以去除字节数组某个成员或者替换某个成员的值。Private Declare Sub PutMem1 Lib "msvbvm60" (ByVal Ptr As Long, ByVal NewVal As Byte) Private Sub Form_Load() Dim t(5) As Byte, m() As Byte t(0) = 11 t(1) = 44 t(2) = 54 t(3) = 48 t(4) = 15 t(5) = 55 m = GetClearByteIn(t, 3) '从字节数组中去除第三个数据 For i = 0 To UBound(m) Debug.Print m(i) '这里返回新的数据 Next Erase m Debug.Print vbCrLf & vbCrLf m = GetReplaceByteIn(t, 3, 200) '替换字节数组中第三个为200 For i = 0 To UBound(m) Debug.Print m(i) '这里返回新的数据 Next End SubPublic Function GetClearByteIn(ByRef ByteIn() As Byte, ByVal Number As Long) As Byte() Dim i&, Temp() As Byte ReDim Temp(UBound(ByteIn) - 1) For i = 0 To UBound(ByteIn) If i <> Number Then If i > Number Then Temp(i - 1) = ByteIn(i) Else Temp(i) = ByteIn(i) Next GetClearByteIn = Temp End Function Public Function GetReplaceByteIn(ByRef ByteIn() As Byte, ByVal Number As Long, ByVal Rep As Byte) As Byte() PutMem1 VarPtr(ByteIn(Number)), Rep GetReplaceByteIn = ByteIn End Function
Sub s() Dim s(3) As Byte, tmp(2) As Byte, i As Byte s(0) = 10 s(1) = 20 s(2) = 30 s(3) = 40
For i = 0 To UBound(s) - 1 If i > 1 Then s(i) = s(i + 1) tmp(i) = s(i) Next End Sub
不定数量的替换顺序扫描即可 Option ExplicitSub Main() Dim a() As Byte Dim lUBound As Long Dim iScan As Long '扫描的下标' Dim iDone As Long '已完成的下标'
a = NewByteArray(1, 2, 3, 4, &H7F, &HA2, &H7F, 5, &HA2, 6, &H7F) PrintArray a
lUBound = UBound(a) iDone = -1 iScan = 0 While iScan < lUBound If (a(iScan) = &H7F) And (a(iScan + 1) = &HA2) Then '替换' iDone = iDone + 1 a(iDone) = &HDD iScan = iScan + 2 Else '复制' iDone = iDone + 1 If iDone <> iScan Then a(iDone) = a(iScan) End If iScan = iScan + 1 End If Wend If iScan = lUBound Then '复制最后1字节' iDone = iDone + 1 If iDone <> iScan Then a(iDone) = a(iScan) End If End If '截短数组' ReDim Preserve a(iDone)
PrintArray a
End SubFunction NewByteArray(ParamArray Members()) As Byte() Dim a() As Byte Dim i As Long
ReDim a(UBound(Members)) For i = 0 To UBound(a) a(i) = Members(i) Next NewByteArray = a End FunctionSub PrintArray(a() As Byte) Dim i As Long
For i = 0 To UBound(a) If i > 0 Then Debug.Print "-"; Debug.Print Right$("0" & Hex(a(i)), 2); Next Debug.Print End Sub
For i=0 to 3
If i<>2 then
s1(j)=s(i)
j=j+1
next
Dim s1(2) As Byte
For i = 0 To 3
If i <> 2 Then
s1(j) = s(i)
j = j + 1
End If
Next
s = s1
For i = 0 To UBound(s)
Debug.Print s(i)
Next
End SubPrivate Sub Form_Load()
ReDim s(3)
s(0) = 10
s(1) = 20
s(2) = 30
s(3) = 40
End Sub
dim n
n=2 '去掉第2个元素
for i=n to ubound(s)-1
s(i)=s(i+1)
next
redim preserve s(ubound(s)-1)
Private Sub Form_Load()
Dim t(5) As Byte, m() As Byte
t(0) = 11
t(1) = 44
t(2) = 54
t(3) = 48
t(4) = 15
t(5) = 55
m = GetClearByteIn(t, 3) '从字节数组中去除第三个数据
For i = 0 To UBound(m)
Debug.Print m(i) '这里返回新的数据
Next
Erase m
Debug.Print vbCrLf & vbCrLf
m = GetReplaceByteIn(t, 3, 200) '替换字节数组中第三个为200
For i = 0 To UBound(m)
Debug.Print m(i) '这里返回新的数据
Next
End SubPublic Function GetClearByteIn(ByRef ByteIn() As Byte, ByVal Number As Long) As Byte()
Dim i&, Temp() As Byte
ReDim Temp(UBound(ByteIn) - 1)
For i = 0 To UBound(ByteIn)
If i <> Number Then If i > Number Then Temp(i - 1) = ByteIn(i) Else Temp(i) = ByteIn(i)
Next
GetClearByteIn = Temp
End Function
Public Function GetReplaceByteIn(ByRef ByteIn() As Byte, ByVal Number As Long, ByVal Rep As Byte) As Byte()
PutMem1 VarPtr(ByteIn(Number)), Rep
GetReplaceByteIn = ByteIn
End Function
Sub s()
Dim s(3) As Byte, tmp(2) As Byte, i As Byte s(0) = 10
s(1) = 20
s(2) = 30
s(3) = 40
For i = 0 To UBound(s) - 1
If i > 1 Then s(i) = s(i + 1)
tmp(i) = s(i)
Next
End Sub
Option ExplicitSub Main()
Dim a() As Byte
Dim lUBound As Long
Dim iScan As Long '扫描的下标'
Dim iDone As Long '已完成的下标'
a = NewByteArray(1, 2, 3, 4, &H7F, &HA2, &H7F, 5, &HA2, 6, &H7F)
PrintArray a
lUBound = UBound(a)
iDone = -1
iScan = 0
While iScan < lUBound
If (a(iScan) = &H7F) And (a(iScan + 1) = &HA2) Then
'替换'
iDone = iDone + 1
a(iDone) = &HDD
iScan = iScan + 2
Else
'复制'
iDone = iDone + 1
If iDone <> iScan Then
a(iDone) = a(iScan)
End If
iScan = iScan + 1
End If
Wend
If iScan = lUBound Then
'复制最后1字节'
iDone = iDone + 1
If iDone <> iScan Then
a(iDone) = a(iScan)
End If
End If
'截短数组'
ReDim Preserve a(iDone)
PrintArray a
End SubFunction NewByteArray(ParamArray Members()) As Byte()
Dim a() As Byte
Dim i As Long
ReDim a(UBound(Members))
For i = 0 To UBound(a)
a(i) = Members(i)
Next NewByteArray = a
End FunctionSub PrintArray(a() As Byte)
Dim i As Long
For i = 0 To UBound(a)
If i > 0 Then Debug.Print "-";
Debug.Print Right$("0" & Hex(a(i)), 2);
Next
Debug.Print
End Sub