gpt4 book ai didi

arrays - Excel VBA 数组 : Is there a simple way to delete a dataset by the index?

转载 作者:行者123 更新时间:2023-12-03 01:27:54 30 4
gpt4 key购买 nike

是否有一种简单的方法可以通过索引从数组中删除特定数据集?

示例:

Dim array() as string

ReDim array(2,2)

array(0,0) = "abc"
array(0,1) = "Peter"
array(0,2) = "New York"

array(1,0) = "xyz"
array(1,1) = "Bob"
array(1,2) = "Los Angeles"

array(2,0) = "klm" ' edited (enumeration error in OP)
array(2,1) = "Stacey"
array(2,2) = "Seattle"

所以我的数组显示在
0:abc,彼得,纽约
1:xyz,鲍勃,洛杉矶
2:荷航、史黛西、西雅图

现在,根据之前的计算,我知道我不再需要 索引 1 处的 Bob,并且我想删除他的记录

有没有像下面这样简单的东西?

ReDim Preserve array(UBound(array) - 1)
array.delete(1)

最佳答案

尝试一维数组

Sub test()
Dim strr As String
strr = "0|1|2|3|5"
wArr = Split(strr, "|")
d = DeleteElementAt(2, strr)
End Sub


Function DeleteElementAt(ByVal index As Integer, ByRef prLsts, strDelimeter) As String
Dim i As Integer
Dim newLst
' Move all element back one position
prLst = Split(prLsts, strDelimeter)
If UBound(prLst) > 0 Then
ReDim newLst(UBound(prLst) - 1)
For i = 0 To UBound(prLst)
If i <> index Then newLst(y) = prLst(i): y = y + 1
Next
DeleteElementAt = Join(newLst, strDelimeter)
Else
DeleteElementAt = prLsts
End If

End Function

对于二维数组

Function Delete2dElementAt(ByVal index As Integer, ByRef prLsts) As Variant
Dim i As Integer
Dim newLst
' Move all element back one position
prLst = prLsts
If index > UBound(prLst) Then MsgBox "overcome index": Exit Function
If UBound(prLst) > 0 Then
ReDim newLst(UBound(prLst) - 1, UBound(prLst, 2))
For i = 0 To UBound(prLst)
If i <> index Then
For Z = LBound(prLst, 2) To UBound(prLst, 2)
newLst(y, Z) = prLst(i, Z)
Next Z
y = y + 1
End If
Next
Delete2dElementAt = newLst
Else
Delete2dElementAt = prLsts
End If

End Function

关于arrays - Excel VBA 数组 : Is there a simple way to delete a dataset by the index?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56461354/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com