如何从一个工作簿中检查另一个工作簿中的主列表的原始数据

时间:2017-03-22 19:13:00

标签: excel windows excel-vba vba

我是VBA-Excel的新手,需要一些帮助。我有一个工作簿,其中包含可接受的字符串值的主列表。此工作簿定期更新。我有另一个包含所有原始数据的工作簿。我想让我的宏打开主列表,检查原始数据中第二列的每个条目与主列表,并删除我的主列表中没有包含的任何单元格。我需要一些指示来指导我。以下是我到目前为止的情况:

Sub Firstcheck()

Dim wkb As Object
Dim wkbname As Object
Dim masterlist As Range
Dim cell As Range

Set wkbname = ActiveWorkbook
Set wkb = Workbooks.Open(Filename:="T:\Communications and Media\Media\Media    Reporting\media master list.xlsx")
wkb.Activate
Set masterlist = Range("a1", Range("a1").End(xlDown))

wkbname.Activate
Range("a1").Select
For i = 1 To 3
If Len(ActiveCell.Offset(1, 0).Value) > 1 Then
ActiveCell.Offset(1, 0).Select
    For Each cell In masterlist.Cells
        If cell = ActiveCell.Offset(0, 1).Value Then
        ActiveCell.Interior.ColorIndex = 5
        Exit For


        Else: ActiveCell.Interior.ColorIndex = 4

        End If

        Next cell


Else: i = 10
End If
i = i - 1
Next

End Sub

提前感谢您提供任何帮助。

2 个答案:

答案 0 :(得分:0)

这没有Excel部分,但是这样的事情怎么样......

    public class CellValue
    {
        public string theValue { get; set; }
    }

    List<CellValue> masterList = new List<CellValue>(); //Load these values into the list
    List<CellValue> otherList = new List<CellValue>();   //Load these values into the list

    List<string> onesToDelete = otherList.Select(x => !masterList.Any(x2 => x2.theValue == x.theValue));

答案 1 :(得分:0)

在VBA中,您可以尝试:

Sub Firstcheck()
    Dim cel As Range, masterList
    With Workbooks.Open("T:\Communications and Media\Media\Media    Reporting\media master list.xlsx")
        With .Worksheets(1)
            masterList = .Range(.Range("A1"), .Range("A1").End(xlDown)).value2
        End With
        .Close False
    End With

    For Each cel In ThisWorkbook.Worksheets(1).UsedRange.Columns(2).Cells
        If IsError(Application.match(Trim(cel.value2)), masterList) Then
            cel.Interior.ColorIndex = 4
        Else
            cel.Interior.ColorIndex = 5
        End If
    Next
End Sub