作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否有一种简单的方法可以通过索引从数组中删除特定数据集?
示例:
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/
我是一名优秀的程序员,十分优秀!