在此先感谢您的帮助,我有一个精神障碍!
关于为什么这是一个很长的故事,但是我有一个类似以下的表格:
Start Date End Date Reference
02/07/2017 01/07/2018 2331851720712
01/04/2017 31/12/2017 1200025264898
01/02/2017 31/12/2017 1200025264912
08/05/2017 07/06/2017 2000054765267
10/06/2017 10/07/2017 2000054765267
底部两行具有相同的参考号,并且第一次出现的结束日期与第二次出现的开始日期在同一月份。发生这种情况时,我需要将首次出现的结束日期更改为上个月的31号。
任何想法都将不胜感激!
答案 0 :(得分:1)
Sub test()
Dim first_occ, second_occ As String
Dim lrow As Integer
Dim pre_month, first_occ_month, second_occ_month As String
lrow = ThisWorkbook.ActiveSheet.Cells(Rows.Count, 3).End(xlUp).Row
For x = 2 To lrow
first_occ = Cells(x, 3).Value
second_occ = Cells((x + 1), 3).Value
If (first_occ = second_occ) Then
first_occ_date = Format(Cells(x, 2).Value, "d/m/yyyy")
first_occ_month = Month(Format(Cells(x, 2).Value, "d/m/yyyy"))
second_occ_month = Month(Format(Cells((x + 1), 1).Value, "d/m/yyyy"))
If first_occ_month = second_occ_month Then
Lastday = DateSerial(Year(first_occ_date), Month(first_occ_date), 0)
Cells(x, 2).Value = Lastday
Cells(x, 2).NumberFormat = "d/m/yyyy"
End If
End If
Next
End Sub
答案 1 :(得分:1)
排序,然后调整日期。
Sub fixEndDate()
Dim i As Long, j As Long, arr As Variant
With Worksheets("sheet2")
With .Range(.Cells(1, "A"), .Cells(.Rows.Count, "C").End(xlUp))
.Cells.Sort Key1:=.Columns(3), Order1:=xlAscending, _
Key2:=.Columns(1), Order2:=xlAscending, _
Orientation:=xlTopToBottom, Header:=xlYes
End With
For i = 2 To .Cells(.Rows.Count, "C").End(xlUp).Row
If .Cells(i, "C").Value2 = .Cells(i + 1, "C").Value2 And _
Format(.Cells(i, "B").Value2, "mmyy") = _
Format(.Cells(i + 1, "A").Value2, "mmyy") Then
.Cells(i, "B") = DateSerial(Year(.Cells(i, "B").Value2), Month(.Cells(i, "B").Value2), 0)
End If
Next i
End With
End Sub
答案 2 :(得分:1)