首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何报告2列中重叠的单元格值

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

Stack Overflow用户
提问于 2019-02-17 21:31:00
回答 1查看 84关注 0票数 0

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

下面的图片准确地解释了我拥有的数据以及我想要用它做什么。

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

代码语言:javascript
复制
Sub overlap()
Dim c As Range


For Each c In Selection
    If c.Value
Next c


End Sub
EN

回答 1

Stack Overflow用户

发布于 2019-02-17 21:43:54

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

代码语言:javascript
复制
    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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54733757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档