我正在创建一个用于项目管理的excel,而在甘特部分,最好将日期和周数都作为信息。
从图片上可以看到,顶部的滚动条使表格发生变化,因此合并操作也必须随着数字的移动而发生变化。
我没有做太多尝试,因为我想不出一种自动执行此操作的方法。我希望单元格合并和取消合并,因为结果将更改。
这是我期望发生的事情:
如果需要,我可以显示滚动条的工作方式,以使其在视觉上更易于理解。
答案 0 :(得分:0)
您可以尝试:
Option Explicit
Sub test()
Dim LastColumn As Long, StartPoint As Long, EndPoint As Long, i As Long, y As Long
With ThisWorkbook.Worksheets("Sheet1")
'Find the last column of row 1
LastColumn = .Cells(1, .Columns.Count).End(xlToLeft).Column
'Set StartPoint
StartPoint = 1
'Loop row 1
For i = 1 To LastColumn
If i = StartPoint Then
For y = i + 1 To LastColumn
If .Cells(1, i).Value <> .Cells(1, y).Value Then
EndPoint = y - 1
Exit For
End If
Next y
Application.DisplayAlerts = False
With .Range(.Cells(1, StartPoint), Cells(1, EndPoint))
.Merge
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
Application.DisplayAlerts = True
StartPoint = y
EndPoint = 0
End If
Next i
End With
End Sub
答案 1 :(得分:0)
这是我用的那个。
Sub MergeSame()
Dim rng As Range
Dim First As Range
Dim Last As Range
Set rng = Selection
If rng.Rows.Count > 1 Then Exit Sub
For i = 1 To rng.Columns.Count + 1
If rng.Cells(1, i).Value <> PreviousValue Then
If Not (First Is Nothing) Then
Set Last = rng.Cells(1, i - 1)
Range(First, Last).Merge
End If
Set First = rng.Cells(1, i)
PreviousValue = rng.Cells(1, i).Value
Else
rng.Cells(1, i).Clear
End If
rng.Cells(1, i).HorizontalAlignment = xlCenter
Next i
End Sub
答案 2 :(得分:0)
谢谢你们!
您帮助我创建了自己的解决方案。
它是由很多ifs和elses(超过350行)组成的::P
但是有效!