如何报告2列中重叠的单元格值

时间:2019-02-17 13:31:00

标签: excel vba loops

我有两组时间序列(dd / mm / yyyy),可能在不同的时间段开始和结束。我想在另一个单元格中报告两个列中都存在的所有重叠日期和与之相关的相对数据。

下面的图片准确地解释了我拥有的数据以及我要使用的数据。

enter image description here

选择2列,创建变量并启动“ foreach”循环,我不知道执行此命令的代码结构。

Sub overlap()
Dim c As Range


For Each c In Selection
    If c.Value
Next c


End Sub

1 个答案:

答案 0 :(得分:0)

未经测试,但是应该这样做:

    Option Explicit
    Sub Overlap()

    Dim wb As Workbook
    Dim ws As Worksheet
    Dim LastRow As Long, LastCol As Long
    Dim rng As Range, Rng2 As Range
    Dim Cell As Variant, Double As Variant

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Whatever")


    LastRow = ws.Cells(wsV.Rows.Count, "A").End(xlUp).row
    Lastcol = ws.Cells(1, .Columns.Count).End(xlToLeft).Column
    'Your entire range, here in column A    
    Rng = ws.Range(ws.Cells(1, 1), ws.Cells(LastRow, LastCol))
    ' Loop through each cell of the Range
    For Each Cell in Rng
        'Set a new range, from the beginning of the orginal one to your current row
        Set Rng2 = ws.Range(ws.Cells(1,1), ws.Cells(Cell.Row,1))
        With Rng2
        Set Double = .Find(Cell.Value, LookIn:=xlValues, Lookat:=xlWhole)
        ' If double is not nothing you've found the value of the current cell
        ' Meaning there's more than one
        If Not Double Is Nothing Then
          'Here you found a duplicate value - Do what you need to do
        End If  
      End With
    Next Cell     

End Sub