Excel 更改第二个和后续副本的值

Excel 更改第二个和后续副本的值,excel,vba,duplicates,Excel,Vba,Duplicates,我有两列数据。如下图所示,第一列有一个重复列表,第二列有一个月的第一天作为日期。第一个副本应保留为01-11-19,而下一个副本应在单元格中添加1,使其成为02-11-19,然后是其他副本。如何在VBA中编写此代码 我尝试了这个函数,但它没有按预期工作,因为它正在修改两个副本,但我只希望修改下一个副本 Dim Rng, cel As range Set Rng = .range(.Cells(firstrow, 1), .Cells(lastrow, 2)) For Each cel In

我有两列数据。如下图所示,第一列有一个重复列表,第二列有一个月的第一天作为日期。第一个副本应保留为01-11-19,而下一个副本应在单元格中添加1,使其成为02-11-19,然后是其他副本。如何在VBA中编写此代码

我尝试了这个函数,但它没有按预期工作,因为它正在修改两个副本,但我只希望修改下一个副本

Dim Rng, cel As range

Set Rng = .range(.Cells(firstrow, 1), .Cells(lastrow, 2))

For Each cel In Rng
    If WorksheetFunction.CountIf(Rng, cel.value) > 1 Then
        WorksheetName.Cells(cel.row, 2).value = WorksheetName.Cells(cel.row, 2).value + 1
    End If 
Next cel
第1列有重复项,第2列将被添加1以使其成为下一个日期


这里是一种公式方法,假设a列是重复数据,B列是日期。第一行包含标题,所需信息从第2行开始

First, sort the data, column B oldest to newest, then Column A smallest to largest

In cell C2, copy back the date from cell B2 (as this is the first data row, it will be the same date)

In cell C3, input the formula =IF(A2=A1,B2+1,B2), and copy it down to the final row of the raw data

通过记录上述步骤并在必要时进行微调,您可以将其脚本化到vba中

请在问题中包含用于此目的的代码。堆栈溢出不是免费的代码编写服务。抱歉,我已编辑。不确定它是否有用..在vba中明确需要它吗?因为它也可以用公式来完成。@Bankar我更喜欢VBA,但我也可以用公式来尝试。