Vba to merge Years in a row

Liverlee

Board Regular
Joined
Nov 8, 2018
Messages
73
Office Version
  1. 2019
Platform
  1. Windows
Hello, I'm trying and failing to make a gantt chart.

I've linked A1 and b5, and the months increment with the edate(b5,1) dragged across. But the below VBA just doesn't like it nor will update the years and merge them.

11th June 2023.png

Is it possible to amend the code below so it will auto amend & merge the years when cell A1 is updated with new date?

Sub test()

Dim monthsRng As Range
Set monthsRng = Range("B5:AH5")
monthsRng.Cells(1, 1).Offset(-1, 0).Select

For j = 1 To Int((monthsRng.Cells.Count / 12) + 2)
If ActiveCell.Offset(1, 0) <> 0 Then
For i = 1 To 12
ActiveCell.Value = Year(ActiveCell.Offset(1, 0))
If Year(ActiveCell.Offset(1, i)) = ActiveCell Then
Selection.Resize(1, i + 1).Select
Else
Exit For
End If
Next
With Selection
.HorizontalAlignment = xlCenter
.MergeCells = True
End With
Selection.Offset(0, 1).Select
Else
Exit For
End If
Next

End Sub
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Like this maybe?
VBA Code:
Sub test()
  Dim lastCol As Long
  lastCol = Cells(5, Columns.Count).End(xlToLeft).Column
  For i = 2 To 33
    For j = i + 1 To 34
      If Year(Cells(5, j + 1).Value) <> Year(Cells(5, j).Value) Then
        With Cells(4, i).Resize(1, j - i + 1)
          .Merge
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
        End With
        Cells(4, i).Value = Year(Cells(5, i).Value)
        i = j
        Exit For
      End If
    Next
  Next
End Sub
 
Upvote 0
This code will update your table as soon as you update A1:
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
  If Target = Range("A1") Then
    Application.EnableEvents = False
    Dim lastCol As Long
    lastCol = Cells(5, Columns.Count).End(xlToLeft).Column
    Range("B2").Resize(1, lastCol - 1).UnMerge
    For i = 2 To lastCol - 1
      For j = i + 1 To lastCol
        If Year(Cells(5, j + 1).Value) <> Year(Cells(5, j).Value) Then
          With Cells(4, i).Resize(1, j - i + 1)
          .Merge
          .HorizontalAlignment = xlCenter
          .VerticalAlignment = xlCenter
          End With
          Cells(4, i).Value = Year(Cells(5, i).Value)
          i = j
          Exit For
        End If
      Next
    Next
    Application.EnableEvents = True
  End If
End Sub
 
Upvote 0
Try
VBA Code:
Sub MergeCells()
Dim stcel$, Lc&, T&
stcel = "B4"
Lc = Range("b5").End(xlToRight).Column
With Range("B4").EntireRow
.UnMerge
.Clear
End With
For T = 2 To Lc
If Year(Range(stcel).Offset(1, 0)) <> Year(Cells(5, T + 1)) Then
    With Range(stcel & ":" & Cells(4, T).Address)
    .Merge
    .HorizontalAlignment = xlCenter
    .Value = Year(Range(stcel).Offset(1, 0))
    End With
stcel = Cells(4, T + 1).Address

End If
Next T
End Sub
 
Upvote 0
Solution
Thank you for both replying to my query and providing solutions. Much appreciated (y) (y) 👏
 
Upvote 0

Forum statistics

Threads
1,215,335
Messages
6,124,327
Members
449,155
Latest member
ravioli44

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top