【问题标题】:Add all data wthin range then check if it already exists添加范围内的所有数据,然后检查它是否已经存在
【发布时间】:2015-09-13 06:51:26
【问题描述】:

我正在使用 FileDialog 来选择其他工作簿。我想一次选择多个文件。

这是我的做法:

With fd

    .Filters.Clear
    .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsb; *.xltx; *.xltm; *.xlt; *.xml; *.xlam; *.xla; *.xlw", 1
    .AllowMultiSelect = True
    If .Show = -1 Then

    For Each vrtSelectedItem In .SelectedItems

            'Extract the Filename (without its file extension) to the File Path
            nPath = Mid(vrtSelectedItem, InStrRev(vrtSelectedItem, "\") + 1)
            'nPath is Filename with path
            nFilename = Left(nPath, InStrRev(nPath, ".") - 1)

             If IsWorkBookOpen(vrtSelectedItem) = True Then
                    MsgBox "File already open."
             Else

                Set wrkbk = Workbooks.Open("" & vrtSelectedItem)
                Set wrkbk_destination = ThisWorkbook '<--- this where is will add the data from files selected with FD
                Set wrkbk_source = Workbooks("" & nFilename) '<--- this the selected files

         With wrkbk_destination.Sheets("Defect Log")
         .Activate

            ' I want to add the all values within range here but check if data already exist
            ' For example selected files have data within range of D11 : I11 , D12 : I12 and D13 : I13
            ' I want to add these but if data within D12 : I12 already exist It will skip adding data and continue with
            ' D13 : I13

                    End With

我只需要一个示例来说明如何做到这一点,并且我会指出这个添加的数据将在 wrkbk_destination 中显示的位置。

【问题讨论】:

    标签: excel range add filedialog vba


    【解决方案1】:

    几个点

    1. 您可以取消对象wrkbkwrkbk_source 之一
    2. 您不需要检查数据是否已经存在。只需复制数据,因为只有一小部分 D11:I13 需要复制。最后使用.RemoveDuplicates。它会更快。
    3. 您无需继续激活您的工作簿/工作表。您可以直接执行该操作。你可能想看看How to avoid using Select in Excel VBA macros

    这是您正在尝试的吗(未经测试

    Sub Sample()
        '
        '~~> Rest of the code
        '
    
        Dim lRow As Long
    
        Set wrkbk_destination = ThisWorkbook
    
        With fd
            .Filters.Clear
            .Filters.Add "Excel Files", "*.xls; *.xlsx; *.xlsb; *.xltx;" & _
            "*.xltm; *.xlt; *.xml; *.xlam; *.xla; *.xlw", 1
    
            .AllowMultiSelect = True
    
            If .Show = -1 Then
    
            For Each vrtSelectedItem In .SelectedItems
                 If IsWorkBookOpen(vrtSelectedItem) = True Then
                    MsgBox "File already open."
                 Else
                    Set wrkbk_source = Workbooks.Open(vrtSelectedItem)
    
                    With wrkbk_destination.Sheets("Defect Log")
                        lRow = .Range("D" & .Rows.Count).End(xlUp).Row + 1
    
                        .Range("D" & lRow & ":I" & (lRow + 2)).Value = _
                        wrkbk_source.Sheets(1).Range("D11:I13").Value
                    End With
    
                    wrkbk_source.Close (False)
                End If
            Next vrtSelectedItem
        End With
    
        With wrkbk_destination.Sheets("Defect Log")
            lRow = .Range("D" & .Rows.Count).End(xlUp).Row
    
            '~~> Change xlNo to xlYes if the column has headers
            .Columns("D1:I" & lRow).RemoveDuplicates Columns:= _
            Array(1, 2, 3, 4, 5, 6), Header:=xlNo
        End With
    End Sub
    

    【讨论】:

    • 您好,感谢您的快速回复,也感谢您向我介绍了 .RemoveDuplicate 功能,呵呵!编程总是有一种简单的方法。我还没有尝试过,但我有一个问题,如果所有值都是唯一的,这是否会在每一行中添加所有值?第 12 行 .Range D12 到 I12、第 13 行 .Range(D13 到 I13)和第 14 行 .Range(D14 到 I14)中的示例,每个行集的所有值都是唯一的,因此将全部添加。
    • 是的 :) 唯一或重复...它们将被添加,然后.RemoveDuplicate 将删除所有重复项(保留一个条目)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-02-04
    • 2015-07-12
    • 1970-01-01
    • 2022-11-15
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    相关资源
    最近更新 更多