【问题标题】:Array to store location of matching poistions of 2 different arrays in vba数组用于存储 vba 中 2 个不同数组的匹配位置的位置
【发布时间】:2017-03-31 14:17:56
【问题描述】:

我在 VBA 中创建了两个相同维度的数组(下面的部分代码,使用两次来创建 2 个数组),我想使用匹配函数/其他一些函数/循环将匹配值位置存储在一个新的vba 中的数组,如果出现错误(非现有值)任何适当的标志(0)。

例如:Array1(美国、英国、新加坡、中国) Array2(印度、美国、英国、新西兰) Array3(0,2,3,0)

我正在寻找如何到达 array3{}

提前致谢!!

Dim wcountryarray() as variant

ReDim wcountryArrayname(1 To maxima5 * Site_countmax * wacountmax) As Variant

counter = 100

lsitetype = 1

For Country_count = 1 To wacountmax

For within_site = 1 To Site_countmax                           

counter = counter + 2           

For Site_types = lsitetype To maxima5 + lsitetype - 1


    wcountryArrayname(Site_types) = WB2.Sheets("Data Input").Cells(Site_types + counter, 2).Value & "|" & _
    WB2.Sheets("Data Input").Cells(Site_types + counter, 3).Value & "|" & _
    WB2.Sheets("Data Input").Cells(Site_types + counter, 4).Value

 Next Site_types

 counter = counter + 30 - maxima5 - (maxima5 - 1)

 columns_Data = columns_Data - 1

 Site_types = Site_types - 1

 lsitetype = Site_types + 1

 Next within_site

 counter = (100 * (Country_count + 1)) - Site_types

 Next Country_count

【问题讨论】:

  • 不确定我是否完全按照您的示例进行操作,但您可以使用application.match 定位另一个数组中一个数组元素的位置。
  • 数组公式可以用来“简化”它。结果会返回到工作表吗?
  • 不,结果保留在数组中,并将用于根据位置进一步比较两个不同的数组。我将使用它来比较数组 1 中的 USA 和数组 2 中的 USA 的元素。
  • 获取类型不匹配错误 13,我正在尝试解决相同的问题,但我们将不胜感激。

标签: arrays vba excel


【解决方案1】:

例如,在 VBA 中,这会导致 Variant(1 To 4) 数组:

a = [IfError(Match({"India","USA","UK","New Zealand"},{"USA","UK","Singapore","China"},0),)]

Debug.Print Join(a) ' "0 1 2 0"

或使用数组:

array1 = Array("USA", "UK", "Singapore", "China")
array2 = Array("India", "USA", "UK", "New Zealand")
array3 = Evaluate("IFERROR(MATCH({""" & Join(array2, """,""") & """},{""" & Join(array1, """,""") & """},0),)")

Debug.Print Join(array3) ' "0 1 2 0"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多