Data disappears in merged cells

When I merge cells together, the value disappears. Why does it do this and how do I fix it?

Adding the values:

list_of_numbers = list(range(2023, 2030))
x=2023

for col in range(16, ws.max_column+1, 4):
    mdate = ws.cell(2, column=col)
    mdate.value = x
    mdate.alignment = Alignment(horizontal = 'center', vertical = 'center')
    mdate.font = Font(bold=True)
    x=x+1

Merging cells:

for col_num in range(16, ws.max_column+1):
    ws.merge_cells(start_row=2, start_column=col_num, end_row=2, end_column=col_num+3)

Do you see range(16, ws.max_column+1) in the second example? It looks like you’re merging the cells in columns 16-19, then the cells in columns 17-20, etc. In other words, you’re merging the later cells into earlier cells that have already been merged.

I changed it to range(16, ws.max_column+1,4) and that worked. Thanks!