請測試看看,謝謝
Sub test()
Dim Arr, xD, C%, T%, T1%
Application.DisplayAlerts = False
Set xD = CreateObject("Scripting.Dictionary")
C = Cells(5, Columns.Count).End(xlToLeft).Column
Arr = Range([e5], Cells(5, C))
For j = 1 To UBound(Arr, 2)
T = Month(Arr(1, j))
T1 = Split(Arr(1, j), "/")(2)
If T1 = 1 Then
If T = 1 Then
Cells(4, j + 4) = "一月"
ElseIf T = 2 Then
Cells(4, j + 4) = "二月"
ElseIf T = 3 Then
Cells(4, j + 4) = "三月"
ElseIf T = 4 Then
Cells(4, j + 4) = "四月"
ElseIf T = 5 Then
Cells(4, j + 4) = "五月"
ElseIf T = 6 Then
Cells(4, j + 4) = "六月"
ElseIf T = 7 Then
Cells(4, j + 4) = "七月"
ElseIf T = 8 Then
Cells(4, j + 4) = "八月"
ElseIf T = 9 Then
Cells(4, j + 4) = "九月"
ElseIf T = 10 Then
Cells(4, j + 4) = "十月"
ElseIf T = 11 Then
Cells(4, j + 4) = "十一月"
ElseIf T = 12 Then
Cells(4, j + 4) = "十二月"
End If
End If
If xD.Exists(T) Then
Set xD(T) = Union(xD(T), Cells(4, j + 4))
Else
Set xD(T) = Cells(4, j + 4)
End If
Next
For Each ky In xD.keys
xD(ky).Merge
Next
Application.DisplayAlerts = True
End Sub作者: samwang 時間: 2022-11-29 09:19
Option Explicit
Sub Union_test_1()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union(xU, [C1].Resize(2, 1))
Set xU = Union(xU, [E5].Resize(2, 1))
MsgBox xU(15).Address
End Sub
Sub Union_test_2()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union([C1].Resize(2, 1), xU)
Set xU = Union([E5].Resize(2, 1), xU)
MsgBox xU(15).Address
End Sub
Sub Union_test_3()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union(xU, [B1].Resize(2, 1))
Set xU = Union(xU, [E5].Resize(2, 1))
MsgBox xU(15).Address
End Sub
Sub Union_test_4()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union([B1].Resize(2, 1), xU)
Set xU = Union([E5].Resize(2, 1), xU)
MsgBox xU(15).Address
End Sub
Sub Union_test_5()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union([F5].Resize(2, 1), xU)
Set xU = Union([E5].Resize(2, 1), xU)
MsgBox xU(5).Address
End Sub
Sub Union_test_6()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union(xU, [C1].Resize(2, 1))
Set xU = Union(xU, [E5].Resize(2, 1))
MsgBox xU(15, 1).Address
End Sub
Sub Union_test_7()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union(xU, [C1].Resize(2, 1))
Set xU = Union(xU, [E5].Resize(2, 1))
MsgBox xU(1, 15).Address
End Sub
Sub Union_test_8()
Dim xU As Range
Set xU = [A1:A2]
Set xU = Union(xU, [C1].Resize(2, 1))
Set xU = Union(xU, [E5].Resize(2, 1))
MsgBox xU(15, 15).Address
End Sub作者: samwang 時間: 2022-11-30 15:27
工作表模組:
Private Sub Worksheet_Change(ByVal Target As Range)
With Target
'↑以下是關於觸發(儲存格值編輯)後的程序
If .Address <> "$E$5" Then Exit Sub
'↑如果觸發格的位址不是 "$E$5",就結束程式執行
If IsDate(.Value) Then Call 合併月份
'https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/isdate-function
'IsDate()如果運算式是日期或是可辨識為有效的日期或時間,會傳回 True,否則會傳回 True 。否則,它會傳回 False。
'if的條件成立!就執行 合併月份 副程式
'觸發[E5]可以帶起連鎖反應,公式更新,月份也更新了
End With
End Sub
Module1:
Sub 合併月份()
Dim xR As Range, xA As Range, m$, m1$, m2$
'↑宣告變數(xR,xA)是儲存格變數,(m,m1,m2)是字串變數
Application.ScreenUpdating = False
'↑螢幕畫面暫不跟著程序變化執行結果
With Range("e4", Cells(5, Columns.Count).End(xlToLeft)(0))
'↑以下是關於儲存格[E4]到第5列最右邊有內容欄位儲存格的程序
.UnMerge: .ClearContents
'↑取消合併儲存格 :清除儲存格內容
For Each xR In .Cells
'↑設順迴圈!令xR 是這些儲存格之一,從前面輪到後面(左至右)
m1 = Format(xR(2), "m")
'↑令m1是迴圈xR 下方1格儲存格變化為字串(規則是:取日期的不補0月份)
'同m1 = Format(xR.ITEM(2, 1), "m")
'https://learn.microsoft.com/zh-tw/office/vba/language/reference/user-interface-help/format-function-visual-basic-for-applications
m2 = Format(xR(2, 2), "m")
'↑令m2是迴圈xR 下方1列左方1欄儲存格變化為字串(規則是:取日期的不補0月份)
'同m2 = Format(xR.ITEM(2, 2), "m")
If m1 <> m Then
'↑如果m1<>m ,m字串變數的初始值是"",這樣的判斷式在第一格時條件就會成立!
'這是後學要學習的方法!既然都知道要宣告m字串,為什麼又不會讓m跟m1比就會得到結果!!
'需要跳脫字串變數一定要給一個字串才拿來做邏輯判斷的想法!
'勤學習勤練習看看可否跳脫,謝謝前輩指導
m = m1: Set xA = xR
'↑if條件成立!就令m=m1 ,令xA儲存格變數是 xR
xR = Application.Text(xR(2), "[DBNum1]m月")
'↑令xR儲存格變數值是 迴圈xR下方1格儲存格變化為小寫月份字串
'Text()會傳回指定之物件的格式化文字。 唯讀的 String
'[DBNum1]:中文小寫
'[DBNum2]:中文大寫
End If
If m2 <> m Then Range(xR, xA).Merge
'↑如果m2 <> m,就讓儲存格(xR尾格, xA頭格)之間的儲存格合併
Next
.Borders.LineStyle = 1
'令整個儲存格集範圍格線是細實線
End With
End Sub
'↑螢幕畫面在執行結束後自動顯示最後結果作者: 星空乂羽翼 時間: 2022-12-19 16:06