使用匹配返回列号

时间:2018-08-24 13:54:52

标签: excel vba excel-vba

尝试使用.Match标识搜索行的列值。

基本上,我有3个工作表,每个工作表具有不同的标题,并且我希望确定每个工作表中哪些标题是相同的,以便比较数据。

这是我到目前为止的代码(目前仅是PoC):

Sub aaaa()

Dim a As Double
Dim b As Integer
Dim c As Integer
Dim d As Variant
Dim e As Variant
Dim f As Variant
Dim h As Double

Worksheets("Reconciliation").Activate

Columns.Select
Selection.ClearContents

a = 1
h = 2

While Worksheets("Cleanse").Cells(a, 1) <> vbNullString

    b = 1
    c = 1
    d = vbNullString
    e = vbNullString
    f = vbNullString

    While Worksheets("Cleanse").Cells(a, 1) <> vbNullString

        If a = 1 Then
            Cells(h, b) = Worksheets("Cleanse").Cells(a, c)
            Cells(h, b + 1) = Worksheets("Cleanse").Cells(a, c) & "_CUMIS"

            d = WorksheetFunction.Match(Worksheets("Cleanse").Cells(a, c), Worksheets("MAddress").Rows("1:1"), 0)

            If Not IsError(d) Then

                Cells(h - 1, b + 1) = d

            ElseIf IsError(d) Then

                d = WorksheetFunction.Match(Worksheets("Cleanse").Cells(a, c), Worksheets("Member_Details").Rows(1), 0)
                Cells(h - 1, b + 1) = "M" & d

            End If

            b = b + 2
            c = c + 1

        Else
            Cells(h, b) = Worksheets("Cleanse").Cells(a, c)

            If b = 1 Then
                e = WorksheetFunction.Match(Worksheets("Cleanse").Cells(a, c), Worksheets("MAddress").Range("A:A"), 0)


                If Not IsError(e) And Not IsError(d) Then

                    Cells(a, b + 1) = Worksheets("MAddress").Cells(d, e)

                Else
                End If
            End If



        End If

    Wend

    a = a + 1

Wend

End Sub

当我将其挖空时,出现以下错误:

Run-time error '9': Subscription out of range

关于如何在一行中搜索变量值并返回列号的任何建议>

1 个答案:

答案 0 :(得分:1)

如果要使用IsError(d)测试d,则必须使用d = application.match(...),而不是d = worksheetFunction.match(...)。

int totalOrdersCount(datetime &lastOpenTrade)
  {
   int result=0; datetime last=0;
   for(int i=OrdersTotal()-1;i>=0;i--)
     {
      if(!OrderSelect(i,SELECT_BY_POS))continue;
      if(OrderMagic()!=InpMagicNumber)continue;
      if(OrderSymbol()!=_Symbol)continue;
      result++;
      if(OrderOpenTime()>last)last=OrderOpenTime();
     }
   lastOpenTrade=last;
   return(result);
  }