Dim Archives() As String
ReDim Archives(0 To 3)Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
我想删除其中一条,变成如下,应该怎么做.
Archives(0) = "abc"
Archives(1) = "asdfsd"
Archives(2) = "xxx"
我想添加一条数据变成如下,应该怎么做.
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "加入的数据"
Archives(3) = "asdfsd"
Archives(4) = "xxx"对vb的数组不是很熟悉,我现在要实现这个功能就是用一个临时数组.不过好像很笨,特请教vb的DX
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
MsgBox Join(Archives, vbCrLf)
ReDim Preserve Archives(0 To 2)
MsgBox Join(Archives, vbCrLf)
End Sub
Private Sub Command2_Click()
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
MsgBox Join(Archives, vbCrLf)ReDim Preserve Archives(0 To 4)
For i = 4 To 3 Step -1
Archives(i) = Archives(i - 1)
Next
Archives(2) = "加入的数据"
MsgBox Join(Archives, vbCrLf)
End Sub
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Dim a() As BytePrivate Sub Command1_Click()
CopyMemory a(51), a(52), 49
ReDim Preserve a(99)
End SubPrivate Sub Form_Load()
ReDim a(100)
Dim i As Long
For i = 0 To 100
a(i) = i
Next
End Sub
不过你删除的是数组的最后一条.我想删除中间的一条.
Option Explicit
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (dest As Any, source As Any, ByVal numBytes As Long)
Private Sub DeleteStringItem(strArr() As String, ByVal index As Long)
Dim lastItem As Long, saveAddr As Long
lastItem = UBound(strArr)
saveAddr = StrPtr(strArr(index))
CopyMemory ByVal VarPtr(strArr(index)), ByVal VarPtr(strArr(index + 1)), (lastItem - index) * 4
CopyMemory ByVal VarPtr(strArr(lastItem)), saveAddr, 4
strArr(lastItem) = vbNullString
End SubPrivate Sub Command1_Click()
Dim Archives() As String
ReDim Archives(0 To 3)
Archives(0) = "abc"
Archives(1) = "xxx"
Archives(2) = "asdfsd"
Archives(3) = "xxx"
DeleteStringItem Archives, 1 '删除Archives(1)
'测试输出
Dim i As Long
For i = 0 To UBound(Archives)
Debug.Print Archives(i)
Next
End Sub