arrays - 匹配并反射(reflect)最新时间

标签 arrays excel vba

我可以做到
我们有 2 个工作表,(User_1) 和 (User_2)
每个工作表有 3 列,[A] System_ID、[B] 用户评论和 [C] 上次修改时间
1- 逻辑匹配两张表中的 [A] 列,如果它们匹配,则 _
2- 检查列 [C] 中的上次修改时间,column(a).offset(0,2).value然后 _
3- 通过在 [B] 列中获得评论,column(a).offset(0,1).value 获得最大或最晚时间获胜然后 _
4- 覆盖列 [B] 中的其他 User_# 工作表注释
我需要这样做
出于性能考虑,将 ( For each - 循环) 转换为数组。
实际范围比此示例中的要大得多。
转换为数组的代码:

Sub Get_LastModified_Here()

    ' Specify Both Worksheets   
    Dim Location1 As Workbook
    Set Location1 = GetWorkbook("C:\Users\HP\Desktop\User_1.xlsb")

    Dim Location2 As Workbook
    Set Location2 = GetWorkbook("C:\Users\HP\Desktop\User_2.xlsb")

    ' Set User_2 Worksheet 
    Dim SourceCell, SourceRange As Range

    ' This is the primary key (system_id)
    Set SourceRange = Workbooks("User_2.xlsb").Sheets("Data").Range("A2:" & "A4")

    ' Start Loop
    For Each SourceCell In SourceRange ' for each cell in system_id column in user_2 worksheet

        Dim X As String ' get each cell address for later usage
        X = SourceCell.Address

        ' Set User_1 Worksheet  (this worksheet)
        Dim TargetCell As Excel.Range
        Set TargetCell = Workbooks("User_1.xlsb").Worksheets("Data").Range(X)    

        ' If column A in both sheets match (System_Unique_ID)
        If SourceCell = TargetCell Then
                
            ' If user 2 (source) modified date in col (C) is (later than >) user1 (target) modified date in col (C) then user 1 comment in col (b) is overwritten by user 2 comment in col (b)   
            If SourceCell.Offset(0, 2).Value > TargetCell.Offset(0, 2).Value Then
                TargetCell.Offset(0, 1).Value = SourceCell.Offset(0, 1).Value
                    
            ' Else if user 1 modified last then his/her comment wins and overwrite user 2 comment
            ElseIf SourceCell.Offset(0, 2).Value < TargetCell.Offset(0, 2).Value Then
                SourceCell.Offset(0, 1).Value = TargetCell.Offset(0, 1).Value

            End If
        End If
    Next SourceCell

End Sub
功能模块/不相关
Public Function GetWorkbook(ByVal sFullName As String) As Workbook
    
    Dim sFile As String
    Dim wbReturn As Workbook
    
    sFile = Dir(sFullName)
    
    On Error Resume Next
    Set wbReturn = Workbooks(sFile)
    
    If wbReturn Is Nothing Then
        Set wbReturn = Workbooks.Open(sFullName)
    End If
    On Error GoTo 0
    
    Set GetWorkbook = wbReturn
    
End Function

最佳答案

尝试,

Sub Get_LastModified_Here()

    ' Specify Both Worksheets
    Dim Location1 As Workbook
    Set Location1 = GetWorkbook("C:\Users\HP\Desktop\User_1.xlsb")

    Dim Location2 As Workbook
    Set Location2 = GetWorkbook("C:\Users\HP\Desktop\User_2.xlsb")

    ' Set User_2 Worksheet
    Dim SourceCell As Range, SourceRange As Range
    Dim rngTarget As Range
    Dim strAdr As String
    Dim vSource As Variant, vTarget As Variant
    Dim i As Long
    ' This is the primary key (system_id)
    Set SourceRange = Workbooks("User_2.xlsb").Sheets("Data").Range("A2:" & "A4")
    
    'The range is expanded by two columns.
    With SourceRange
        Set SourceRange = .Resize(.Rows.Count, .Columns.Count + 2)
    End With
    
    strAdr = SourceRange.Address
    Set rngTarget = Workbooks("User_1.xlsb").Worksheets("Data").Range(strAdr)
    
    'Bring the range as a two-dimensional array.
    vSource = SourceRange
    vTarget = rngTarget
    
    ' Start Loop
   'For Each SourceCell In SourceRange ' for each cell in system_id column in user_2 worksheet
    For i = 1 To UBound(vSource, 1)
        'Dim X As String ' get each cell address for later usage
        'X = SourceCell.Address

    
        ' Set User_1 Worksheet  (this worksheet)
        'Dim TargetCell As Excel.Range
       ' Set TargetCell = Workbooks("User_1.xlsb").Worksheets("Data").Range(X)

        ' If column A in both sheets match (System_Unique_ID)
        'If SourceCell = TargetCell Then
         If vSource(i, 1) = vTarget(i, 1) Then
            ' If user 2 (source) modified date in col (C) is (later than >) user1 (target) modified date in col (C) then user 1 comment in col (b) is overwritten by user 2 comment in col (b)
            'If SourceCell.Offset(0, 2).Value > TargetCell.Offset(0, 2).Value Then
             If vSource(i, 3) > vTarget(i, 3) Then
                    'TargetCell.Offset(0, 1).Value = SourceCell.Offset(0, 1).Value
                    vTarget(i, 2) = vSource(i, 2)
            'Else if user 1 modified last then his/her comment wins and overwrite user 2 comment
            'ElseIf SourceCell.Offset(0, 2).Value < TargetCell.Offset(0, 2).Value Then
            ElseIf vSource(i, 3) < vTarget(i, 3) Then
                'SourceCell.Offset(0, 1).Value = TargetCell.Offset(0, 1).Value
                vSource(i, 2) = vTarget(i, 2)
            End If
        End If
    'Next SourceCell
    Next i
    
    'Assign the values of the array to the range.
    SourceRange = vSource
    rngTarget = vTarget
End Sub

关于arrays - 匹配并反射(reflect)最新时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65151814/

相关文章:

javascript - 两个带有对象的数组成为具有相同长度的新数组,但所有数据都来自原始数组

excel - 在 VBA 中将 300 个字符的字符串转换为唯一可识别的 8 个字符的字符串

excel - 从计时器子调用时,ThisWorkbook.RefreshAll 不起作用

vba - 在 VBA 中将单词转换为数字

Java返回数组

java - 从文件中读取整数并将值存储到数组中

php数组递归

Python:Pandas read_excel 无法打开 .xls 文件,不支持 xlrd

excel - VBA Excel-如何根据三列删除重复项

c - BSD getmicrotime() 输出到 Excel 日期