比较一个单元格与一行,如果匹配则将数据输入另一个单元格

Compare One Cell vs. a Row then enter data into another cell if there is a match

我想找出一种方法来比较 Sheet2 中的一个单元格与 Sheet1 中的整行。如果有匹配项,那么我想用 "X" 标记请求的行。标记 "X" 的行需要更改,因为我正在为众多用户进行比较,我想我可以只设置一个字符串用于输入。一旦单个单元格检查了整行,我需要列中的下一个单元格检查整行并相应地标记 "X"。

简而言之,我正在制作一个安装在 50 台计算机上的软件的数据库,并且我有一个所有可能的应用程序列表以及每台计算机上所有已安装的应用程序。并非每台计算机都有所有应用程序,因此我正在尝试自动化电子表格,该电子表格将根据收集的数据标记哪些计算机具有哪些软件。如果这没有意义,请告诉我。我经常了解 Powershell 中的逻辑流程和程序,但我不太熟悉 VBA 命令。谢谢!

编辑:添加图片进行解释。

Edit2:在我已有的下方添加了代码。好像是运行 check 但是c.Value 总是错的。它只是不完全检查出来。我测试了 CellApp.Select 以确认我想要的范围是正确的。循环只是不检查我认为不正确的值。对于示例图片,假设 "List of Machine 3's Programs" 在 Sheet2 上并从 A1 开始。

Option Explicit
Sub check()
Dim wsApplications As Worksheet, wsMachines As Worksheet
Dim CellApp As Range, CellMachine As Range
Dim listStRow As Long, listEndRow As Long, listCol As Long
Dim c As Range
Dim Counter As Integer

Set wsApplications = Sheets("Sheet2")
Set wsMachines = Sheets("Sheet1")
Counter = 3

'data start(row, col)on machines-list sheet
listStRow = 2
listCol = 1

With wsApplications
    'find last machine in list
    listEndRow = .Cells(Rows.Count, listCol).End(xlUp).Row
    'Set CellApp Range
    Set CellApp = Range("A2", Cells(listEndRow, 1))

    For Each c In CellApp.Cells
    'For each cell in the CellApp Range...
        Set CellMachine = Cells(1, Counter)
        Counter = Counter + 1
        'Defines CellMachines as Cell "1,3" then "1,4" then "1,5" etc...

        If c.Value = CellMachine.Value Then
        'If the cell in CellApp is equal to the cell that is currently CellMachine
            wsMachines.Cells(4, CellMachine.Column).Value = "X"
            'Mark an X underneath the column that matches up. Designated Row 4 for a test.
        End If
     Next c

End With

下面概述了一种方法。这假设 mc/program 数据按照下图显示,而您的 'matrix' 根据您的 Q 显示。调整代码中的 sheet 名称和数据位置以适应。

Option Explicit
Sub check()
Dim wsList As Worksheet, wsMatrix As Worksheet
Dim r As Range, c As Range
Dim listStRow As Long, listEndRow As Long, listCol As Long, n As Long
Dim matHdr As Long, matCol As Long
Dim mcNo As String, progNo As String

Set wsList = Sheets("Sheet2")
Set wsMatrix = Sheets("Sheet1")

'data start(row, col)on machines-list sheet
listStRow = 2
listCol = 1

'start position of matrix (row, col) to be filled
matHdr = 1
matCol = 1

    With wsList
        'find last machine in list
        listEndRow = .Cells(Rows.Count, listCol).End(xlUp).Row
            'for each mc in list
            For n = listStRow To listEndRow
                'construct matrix intersect 'headers' for mc and program
                mcNo = "Machine " & CStr(.Cells(n, listCol).Value)
                progNo = "Program " & CStr(.Cells(n, listCol).Offset(0, 1).Value)
                    'populate matrix with "X"
                    With wsMatrix
                        Set r = .Columns(matCol).Find(mcNo, , , xlWhole)
                            If Not r Is Nothing Then
                                Set c = .Rows(matHdr).Find(progNo, , , xlWhole)
                                    If Not c Is Nothing Then
                                        Intersect(r.EntireRow, c.EntireColumn) = "X"
                                    End If
                            End If
                    End With
            Next n
    End With

End Sub