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

时间:2015-01-25 01:16:44

标签: excel vba excel-vba

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

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

编辑:添加图片以供解释。

Edit2:添加了以下代码。它似乎运行检查,但c.Value总是错误的。它还没有完全结束。我测试了CellApp.Select以确认我想要的范围是正确的。循环只是没有检查我认为不正确的值。对于示例图片,假装"机器3的程序列表"在Sheet2上,从A1开始。

Example

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

1 个答案:

答案 0 :(得分:1)

下面列出的一种方法。这假定mc /程序数据按照下面的图像显示,并且“矩阵”按照您的Q显示。调整代码中的工作表名称和数据位置以适应。

enter image description here

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