我需要找到最好的方法来复制标题行到一个新的标题每次“比赛日期”列的变化。本周有2场比赛在周六和他们的休息日,在这种情况下,我需要的标题复制一次。但如果有其他日期,它必须相应地复制。
数据框是用Python panda构建的,但它需要在Excel中打开,因此我可以在Python中更改代码,或者我可以尝试向Excel工作表添加一些VBA。
Output:
Game Date Game Time Visit Home Roof
Saturday, January 7, 2023 1/7/2023 13:30 Kansas City Las Vegas Fixed
Saturday, January 7, 2023 1/7/2023 17:15 Tennessee Jacksonville Open
Sunday, January 8, 2023 1/8/2023 9:00 Tampa Bay Atlanta Retractable
Sunday, January 8, 2023 1/8/2023 9:00 New England Buffalo Open
Sunday, January 8, 2023 1/8/2023 9:00 Minnesota Chicago Open
Sunday, January 8, 2023 1/8/2023 9:00 Baltimore Cincinnati Open
预期输出:
Game Date Game Time Visit Home Roof
Saturday, January 7, 2023 1/7/2023 13:30 Kansas City Las Vegas Fixed
Saturday, January 7, 2023 1/7/2023 17:15 Tennessee Jacksonville Open
Game Date Game Time Visit Home Roof
Sunday, January 8, 2023 1/8/2023 9:00 Tampa Bay Atlanta Retractable
Sunday, January 8, 2023 1/8/2023 9:00 New England Buffalo Open
Sunday, January 8, 2023 1/8/2023 9:00 Minnesota Chicago Open
Sunday, January 8, 2023 1/8/2023 9:00 Baltimore Cincinnati Open
Sunday, January 8, 2023 1/8/2023 9:00 Los Angeles Denver Open
Sunday, January 8, 2023 1/8/2023 9:00 Detroit Green Bay Open
这是我目前掌握的情况:
Sub InsertHeaderRow()
Dim cell As Range
For Each cell In Range("A1:A" & Cells(Rows.Count, 1).End(xlUp).Row)
If cell.Value <> cell.Offset(1, 0).Value Then
Rows(1).Copy
cell.Offset(1, 0).Insert Shift:=xlDown
End If
Next cell
End Sub
这会创建一个包含正确信息的新行,但会将新行放置在错误的位置。
2条答案
按热度按时间mzsu5hc01#
既然您已经展示了代码尝试,那么帮助您就更容易了
这是代码的修订版,其中包含注解的更改
您可以通过以下方法改进上面的代码:首先收集需要新标题行的所有行,然后一次性插入新标题
最后,为了便于记录,下面是我发布的第一段代码
如果你不介意在数据的右边使用一个helper列,试试这个:
pes8fvy92#
标题行重复