首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >VBA::整合阵列

VBA::整合阵列
EN

Stack Overflow用户
提问于 2011-06-23 20:00:40
回答 1查看 615关注 0票数 0

我正在创建一个搜索功能,允许用户在数据库(prop1,2和3)中同时搜索3个不同的属性,我已经在VBA中通过将搜索到的道具的结果放入一个数组中来创建此子属性。但是,现在我最多有3个数组,我需要合并这些数组,以便只在结果中显示在数组中重复的数据。对于如何1)只查看用户正在搜索的属性的数组,以及2)只获取重复到最终数组中的数据,以便在结果范围中显示它,有什么建议吗?任何帮助都是非常感谢的!谢谢!

EN

回答 1

Stack Overflow用户

发布于 2011-06-24 17:57:50

假设您的条目直接来自数据库,因此对于一个属性是唯一的,我可以为一个简单的解决方案考虑以下步骤:

将数组合并在一起(对于每个元素,temp)

  • Count prop1prop2prop3 >prop1)(在本例中,根据有关出现的知识编写tempCount)

  • Based代码,创建最终的数组(此处称为result<>E217)

Dim prop1() As Variant Dim prop2() As Variant Dim prop3() As Variant Dim temp() As Variant Dim tempCount() As Integer Dim result() As Variant ReDim temp(UBound(prop1) + UBound(prop2) + UBound(prop3) + 1)‘合并数组Dim i As整数出错后恢复下一个For i=0 To UBound(temp) temp(i * 3) = prop1(i) temp(i *3+ 1) = prop2(i) temp(i *3+ 2) = prop3(i)接下来i‘计数出现ReDim tempCount(UBound(temp) + 1) Dim j As Integer For i=0 To UBound(temp) tempCount(i) =1 For j=0 To i-1’元素比较If temp(i) = temp(j) Then tempCount(i) = tempCount(i) +1End If Next j ReDim i ReDim result(UBound(temp) + 1)‘如果一个元素出现3次,如果tempCount(i) =3,则result (UBound)=3则result( count ) = temp(i) count = count +1 End If Next i

为了检查一些示例,我将此代码添加到代码中。它只需将数组temp、result和tempCount打印到A、B和C列。

代码语言:javascript
复制
'some sample arrays
prop1 = Array("a", "b", "c", "d", "e")
prop2 = Array("b", "c", "f")
prop3 = Array("b", "c", "d", "g")

'some sample Output

'temp
Cells(1, 1).Value = "temp:"
For i = 0 To UBound(temp)
    Cells(i + 2, 1).Value = temp(i)
Next i

'result
Cells(1, 2).Value = "result:"
For i = 0 To UBound(result)
    Cells(i + 2, 2).Value = result(i)
Next i

'count:
Cells(1, 3).Value = "count:"
For i = 0 To UBound(tempCount)
    Cells(i + 2, 3).Value = tempCount(i)
Next i

注意:tempCount只保存元素被监视时的累计出现次数。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6453638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档