如何使用VBA EXCEL共享两列

时间:2018-02-21 10:07:48

标签: excel vba excel-vba

我需要一个宏VBA,它将在“B”列中获取代码并在列“E”中逐行查找它,直到结束并且如果它存在=>在“F”栏中确定,否则“FALSE” B列和E列中有如此多的代码。

我尝试了这段代码,但它不起作用

2

3 个答案:

答案 0 :(得分:1)

只是猜测你想要将每个单元格与每个单元格进行比较,因此你需要一个像这样的嵌套循环:

Sub TestMe()

    Dim ws1         As Worksheet
    Dim LastRow     As Long
    Dim LastRowE    As Long
    Dim i           As Long
    Dim ii          As Long

    Set ws1 = Worksheets(1)
    With ws1
        LastRow = .Cells(.Rows.Count, "B").End(xlUp).Row
        LastRowE = .Cells(.Rows.Count, "E").End(xlUp).Row
    End With

    For i = 1 To LastRow
        Dim codeFound As Boolean
        codeFound = False
        For ii = 1 To LastRowE
            If ws1.Cells(i, 2) = ws1.Cells(ii, 5) Then codeFound = True
        Next ii

        If codeFound Then
            ws1.Cells(i, 6) = "OK"
        Else
            ws1.Cells(i, 6) = "FALSE"
        End If
    Next i

End Sub

答案 1 :(得分:1)

不需要循环甚至VBA。

<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
    <a class="navbar-brand" href="#">EVENT</a>
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav mr-auto">
        <li class="nav-item active">
          <a class="nav-link" href="#">Home<span class="sr-only">(current)</span></a>
        </li>
        <li class="nav-item dropdown">
          <a class="nav-link dropdown-toggle" href="#" id="navbarDropdownMenuLink" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
            <?=$_SESSION['name'];?>
          </a>
          <div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
            <a class="dropdown-item" href="#">Action</a>
            <a class="dropdown-item" href="#">Another action</a>
            <a class="dropdown-item" href="#">Something else here</a>
            <div class="dropdown-divider"></div>
            <a class="dropdown-item" href="login/logout.php">Logout</a>
          </div>
        </li>
      </ul>
    </div>
    <div class="nav-item" style="float:top">
      <input class="btn btn-danger" type="submit" value="Create">
      <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
        <span class="navbar-toggler-icon"></span>
      </button>
    </div>
  </nav>

答案 2 :(得分:1)

你可以试试这两种方式。第一个VBA:

Public Sub ComparerCP()
    Dim rng As Range
    Dim arr As Variant
    Dim c

    ' Update to your sheet name
    With ActiveSheet
        ' Set the range that we want to check
        Set rng = .Range(.Cells(1, 2), .Cells(.Cells(.Rows.Count, 2).End(xlUp).Row, 2))
        ' This array contains the range to check against
        ' We transpose the array to get a 1D array from the range
        arr = Application.Transpose(.Range(.Cells(1, 5), .Cells(.Cells(.Rows.Count, 5).End(xlUp).Row, 5)))

        ' Loop through check range and test if each value is in the range to check against
        For Each c In rng
            If IsInArray(c.Value2, arr) Then
                c.Offset(0, 1).Value2 = "Ok"
            End If
        Next c
    End With
End Sub


Public Function IsInArray(v As Variant, FilterArray As Variant) As Boolean
    IsInArray = (UBound(Filter(FilterArray, v)) > -1)
End Function

其次,您可以使用Excel公式。在专栏C中,我已输入:

=IF(COUNT(MATCH(B1,$E:$E,0)),"Ok","")

哪个输出相同的东西