我在excel中有一个大数据集,我的第一列是重复的时间段(年)。我想为缺少的年份插入行并移动关联的数据。这是一个更简单的数据表:
Data: I want to look like this:
2007 aaa 2007 aaa
2008 bbb 2008 bbb
2010 ccc 2009
2008 eee 2010 ccc
2010 ddd 2007
2008 eee
2009
2010 ddd
当我尝试使用match和index函数时,得到了以下结果: Year Revenue Table
where the formulas are:
in cell E:
=IFERROR(MATCH(D2;$A$2:$A$9;0);" ")
in cell F:
=IF(E2<>"";INDEX($A$2:$B$9;E2;1); "")
in cell G:
=IF(E2<>"";INDEX($A$2:$B$9;E2;2); "")
如何考虑重复年份并更正相应的值?
答案 0 :(得分:0)
尝试:
Option Explicit
Sub test()
Dim LastRow As Long, i As Long
Dim strYear As String
With ThisWorkbook.Worksheets("Sheet1")
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
For i = LastRow To 3 Step -1
strYear = .Range("A" & i).Value
If .Range("A" & i - 1).Value <> strYear - 1 Then
.Rows(i).EntireRow.Insert
.Range("A" & i).Value = strYear - 1
End If
Next i
End With
End Sub
代码前:
代码后: