简体   繁体   English

在另一个工作表中查找多个单元格的范围

[英]Find a range of multiple cells in another sheet

I am trying to enhance my current script. 我正在尝试增强当前的脚本。 Sheet1 and Sheet2 contain only filepath names in column A. If a filepath in Sheet2 isn't found in Sheet1, it is copied over to sheet 3. Sheet1和Sheet2在A列中仅包含文件路径名称。如果在Sheet1中找不到Sheet2中的文件路径,则会将其复制到工作表3中。

'row counter
x = 1
'Initiate Variables
Set wb = ActiveWorkbook
Set ws1 = wb.Sheets("Sheet1")
Set ws2 = wb.Sheets("Sheet2")

'create a new sheet 3, delete old one if it exists
If Sheets.Count > 2 Then
Application.DisplayAlerts = False
    Sheets(3).Delete
Application.DisplayAlerts = False
End If

Sheets.Add After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Name = "Sheet3"

Set ws3 = wb.Sheets("Sheet3")

'Get row count to know how many times to loop
rowCount2 = ws2.Range("A1").End(xlDown).Row

'compare filepaths from sheet2 to sheet1
'if there is a difference, that difference is put on sheet 3
For i = 1 To rowCount2
    FilePath = ws2.Cells(i, 1)
    With Sheets("Sheet1").Range("A:A")
        Set CellId = .Find(What:=FilePath, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not CellId Is Nothing Then
        'do nothing if filepath is found in both sheets
        Else
            'put the filepath from file2 not found in file1, into
            'sheet 3
            ws3.Cells(x, 1) = FilePath
            x = x + 1
        End If
    End With
Next I

What I want to do, is be able to reference a range of cells to compare instead of just from column A. Instead of just file paths in column A, there will be last saved by in column B, Last opened by in column C, etc. So instead of just checking for difference in filepath, I want differences in multiple columns. 我想做的是,能够引用一系列要比较的单元格,而不仅仅是从A列中进行比较。不仅仅是A列中的文件路径,而是最后保存于B列,最后保存于C列,因此,我不仅要检查文件路径中的差异,还希望多列中的差异。 So there might be the same filepaths, but it was opened by someone different on another day. 因此,可能存在相同的文件路径,但是另一天却有人打开了它。 I need to grab that difference. 我需要抓住这种差异。 I don't know how to reference the range of multiple cells. 我不知道如何引用多个单元格的范围。 So I need to fix up this section of code: 所以我需要修正这段代码:

FilePath = ws2.Cells(i, 1)
With Sheets("Sheet1").Range("A:A")

And if there is an easier way to approach this I am open to advice. 如果有更简单的方法可以解决这个问题,我可以征询您的意见。

In the 'do nothing if filepath is found in both sheets section, place something like this: 'do nothing if filepath is found in both sheets部分中,放置类似以下内容的内容:

k = ws2.Cells(1,ws2.Columns.Count).End(xlToleft).Column
For j = 2 to k
    If ws2.Cells(i, j).Value <> CellId.Offset(, j - 1).Value Then 
       CellId.EntireRow.Copy ws.Cells(x,1).EntireRow
       x = x +1
       Exit For
       'or whatever code you need to move to sheet3 
    End If
Next

I use a Dictionary when comparing multiple list. 比较多个列表时使用字典。 This way I only iterate over each list one time. 这样,我只遍历每个列表一次。

Sub CopyMissingFileNames()
    Dim filepath As Range, Target As Range
    Dim dictFilePaths As Object
    Set dictFilePaths = CreateObject("Scripting.Dictionary")

    With Worksheets("Sheet1")

        For Each filepath In .Range("A2", .Range("A" & Rows.Count).End(xlUp))

            If Not dictFilePaths.Exists(filepath.Text) Then dictFilePaths.Add filepath.Text, ""
        Next

    End With


    With Worksheets("Sheet2")

        For Each filepath In .Range("A2", .Range("A" & Rows.Count).End(xlUp))
            If Not dictFilePaths.Exists(filepath.Text) Then
                Set Target = Worksheets("Sheet3").Range("A" & Rows.Count).End(xlUp).Offset(1)

                filepath.EntireRow.Copy Target
            End If
        Next

    End With



End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将单元格范围复制到另一张纸上 - Copying range of cells to another sheet 将单元格范围插入另一张工作表 - Insert range of cells to another sheet 将具有多个非相邻单元格的区域复制到同一单元格上的另一张纸上 - Copy a range with multiple non-adjacent cells to another sheet on the same cells 将单元格范围从一张纸复制到另一张纸 - Copy range of cells from one sheet to another 查找给定范围内的所有非零单元格,并在另一张工作表中列出其地址 - Find all non-zero cells in a given range and list their addresses in another sheet 将一个工作表中一列中的单元格区域复制到另一工作表中一行中的指定单元格中 - Copying a range of cells in a column in one sheet to specified cells in a row in another 复制一系列单元格并仅将同一范围内的值粘贴到另一张工作表中 - Copying a range of cells and paste only values in the same range in another sheet 如何选择多个单元格并粘贴到另一张纸上? - How to select multiple cells and copypaste to another sheet? 根据当前工作表(例如Sheet1)中的值,禁用另一工作表(例如Sheet2)上单元格的范围 - disable the range of cells on another sheet ( say Sheet2) based on the value in the current sheet(say Sheet1) VBA 宏查找具有 Sheet1 数据的单元格范围使用该范围在 Sheet2 的范围内输入公式 - VBA Macro to find range of cells with data on Sheet1 use that range to enter formula in range on Sheet2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM