【问题标题】:VBA Excel, loop through 2 columns if when same value then calculate value from another columVBA Excel,如果相同的值然后从另一列计算值,则循环遍历2列
【发布时间】:2019-02-09 12:07:58
【问题描述】:

我有以下问题需要帮助:

我有这张桌子

A------------B------------C-----------开始日期---结束日期

Phase1.1--Phase1----分析师--1/1/2018--1/3/2019

Phase1.1--Phase1----分析师--1/2/2018--1/4/2020

Phase1.1--Phase1----分析师--1/3/2018--1/5/2019

Phase1.1--Phase1----经理--1/2/2018--1/7/2019

Phase1.1--Phase1----经理--1/1/2018--1/5/2019

Phase1.1--Phase2----分析师--1/1/2018--1/3/2019

……..

我想遍历此表并检查 B 列的值是否相同(重复)移动到 C 列,对于 C 列中的相同值,计算“开始日期”列的最小值和“结束日期”列的最大值日期”。
然后在新的excel表中写入行 因此,对于 C 列中的每个条目,我都有一行,其开始日期和结束日期取决于 B 列。 此外,A 列始终相同,应该复制而不是更改。 结果应该是这样的:

A--------B------------C------------开始日期-----结束日期

Phase1.1---Phase1----分析师----1/1/2018--1/4/2020

Phase1.1---Phase1----Manager----1/1/2018-----1/7/2019

Phase1.1---Phase2----分析师----1/1/2018--1/3/2019

…………..

这就是我的real data 的样子:

感谢您的支持

【问题讨论】:

  • 您好 Youssef,欢迎来到 StackOverflow。如果您编辑它以更清晰地布置表格,您可以提高您的问题质量,并让我们知道您迄今为止尝试自己解决这个问题的方法;我们是来帮助你的,而不是为你做你的工作! ;)
  • 既然可以使用公式,为什么还要使用 VBA?
  • @OurManinBananas 或只是一个数据透视表
  • @Luuklag 在数据透视表中,如果有可能的值,您将看不到开始或结束日期,因此您必须始终折叠它们以查看开始日期,例如
  • 你为什么这么相信?您可以准确地制作您想要的表格。

标签: excel vba loops duplicates


【解决方案1】:

这是通过 VBA 实现的一种方法。

Sub rowLoop()
Dim lRow As Long, lRow2 As Long
Dim ws1 As Worksheet, ws2 As Worksheet
Dim startDate As String, endDate As String

Set ws1 = Sheets("Data") 'set this to be the worksheet with data
Set ws2 = Sheets("Results") 'set this to the worksheet you want the results to go

lRow = ws1.Cells(Rows.Count, "A").End(xlUp).Row 'find last used row

ws2.Range("B2:C" & lRow).Value = ws1.Range("B2:C" & lRow).Value 'copy over Column B values
ws2.Range("$B$2:$C$" & lRow).RemoveDuplicates Columns:=Array(1, 2), Header:=xlNo 'remove duplicates


lRow2 = ws2.Cells(Rows.Count, "B").End(xlUp).Row 'find last used row
For i = 2 To lRow2 'loop through rows to determin start and end date
    ws2.Range("A" & i).Value = i - 1

    For j = 2 To lRow
        If ws2.Range("B" & i).Value = ws1.Range("B" & j).Value And ws2.Range("C" & i).Value = ws1.Range("C" & j).Value Then
            If startDate = "" Then
                startDate = ws1.Range("D" & j).Value
            Else
                If ws1.Range("D" & j).Value < startDate Then
                    startDate = ws1.Range("D" & j).Value
                End If
            End If

            If endDate = "" Then
                endDate = ws1.Range("E" & j).Value
            Else
                If ws1.Range("E" & j).Value > endDate Then
                    endDate = ws1.Range("E" & j).Value
                End If
            End If
        End If
    Next j
    ws2.Range("D" & i).Value = startDate
    ws2.Range("E" & i).Value = endDate
    startDate = ""
    endDate = ""
Next i

End Sub

【讨论】:

  • 0 否决票 非常感谢您的回答 我正在测试它现在有一个小问题:在新工作表中,B 列和 C 列被正确复制,但开始和结束日期不是正确的。错误在于新工作表中的开始日期和结束日期,该值是范围中的随机值,例如因为分析师是中间的,但不是最小值或最大值。你知道为什么吗?
  • 该代码非常适合我。这可能是因为任一工作表上的格式。您可以单步执行代码并查看日期是如何变化的,以查看它没有捕获正确日期的地方
  • 没有看到你的实际数据我不能肯定地说。但是您在问题中提供的示例数据适用于此代码。
  • 其实一种思路是在计算lRow之后放置一个break。检查它是否被设置为您实际的最后一行。可能是因为行中有一些空白,代码不会到工作表的末尾
  • 如何向您发送我的数据? @VBA_SQL_Programmer
猜你喜欢
  • 2022-01-01
  • 2015-10-14
  • 1970-01-01
  • 2021-11-24
  • 1970-01-01
  • 2017-05-14
  • 2013-02-20
  • 1970-01-01
  • 2017-10-19
相关资源
最近更新 更多