代码之家  ›  专栏  ›  技术社区  ›  Clauric

数组在For循环中不返回应答

  •  0
  • Clauric  · 技术社区  · 6 年前

    我有一个数组,从电子表格的一行(1行185列)中获取值。然后我想遍历数组,寻找数组中的值与特定单元格中的值之间的匹配。

    但是,每次我运行代码时,它都会说找到了匹配项,但不会向单元格返回值。

    代码的相关行是:

    
    Dim qCountry()
    
    Worksheets("Data").Activate
    
        qCountry = Range("A1:GC1").Value
    
        For i = 1 To 185
              If Cells(aRow, bCol) <> vbNullString Then
                  Exit For
              ElseIf InStr(1, Cells(aRow, 4), "*" & qCountry(i) & "*") = 1 Then
                  Cells(aRow, bCol) = qCountry(i)
              End If
        Next i
    

    我的阵型截图:

    Array

    0 回复  |  直到 6 年前
        1
  •  2
  •   Louis    6 年前

    定义一下 qCountry 正常情况下 Variant 变量,如下所示:

    Dim qCountry as Variant
    

    这将去掉一个额外的维度,但仍然有一个多维数组。


    如果你想处理 一维数组 ,您可以使用 Application.Transpose() 功能 :

    qCountry = Application.Transpose(Range("A1:GC1").Value)
    

    但既然你有数据 ,您需要执行两次:

    qCountry = Application.Transpose(Application.Transpose(Range("A1:GC1").Value))
    

    此时,您的代码将工作:

    Dim qCountry
    Worksheets("Data").Activate
    qCountry = Application.Transpose(Application.Transpose(Range("A1:GC1").Value))
    
    For i = 1 To 185
          If Cells(aRow, bCol) <> vbNullString Then
              Exit For
          ElseIf InStr(1, Cells(aRow, 4), "*" & qCountry(i) & "*") = 1 Then
              Cells(aRow, bCol) = qCountry(i)
          End If
    Next i
    

    希望这有帮助。

        2
  •  1
  •   Slai    6 年前

    可以访问值,如屏幕截图所示:

    Cells(aRow, bCol) = qCountry(1, i)
    
    推荐文章