|
あー・・ほんとですね。この原因もやはり Subtotal のところでした。
全ての月でしっかりテストするべきでした。再々で申し訳ないですが、
以下のコードでやってみて下さい。
Sub My集計3()
Dim Mth As Long, LstR As Long
Dim Clc As Long, y As Long, CR As Long
Dim x As Variant
Dim MyR As Range, C As Range
Dim MyV As String
Const Pmt As String = _
"集計する月を1〜12の整数で入力して下さい"
With Application
Do
Mth = .InputBox(Pmt, Type:=1)
If Mth = False Then Exit Sub
Loop While Mth < 1 Or Mth > 12
.ScreenUpdating = False
End With
LstR = Range("A65536").End(xlUp).Row - 1
With Range("IV1").End(xlToLeft).Offset(, 1)
.Value = "月"
With .Offset(1).Resize(LstR)
.Formula = "=MONTH($A2)"
.Value = .Value
End With
If IsError(Application.Match(Mth, .EntireColumn, 0)) Then
MsgBox "指定した月のデータはありません", 48
.EntireColumn.ClearContents: GoTo ELine
End If
End With
With Range("A1").CurrentRegion
Clc = .Columns.Count
.Sort Key1:=Range("IV1").End(xlToLeft), _
Order1:=xlAscending, Key2:=Range("F1"), _
Order2:=xlAscending, Header:=xlYes, _
Orientation:=xlSortColumns
.Subtotal Clc, xlCount, Array(Clc), False
.Subtotal 6, xlCount, Array(6)
End With
Range("F2", Range("F65536").End(xlUp)).Offset(, -5) _
.SpecialCells(4).Offset(, 6).ClearContents
x = Application.Match(Mth, Columns(Clc), 0)
y = Columns(Clc).Find(Mth, , xlValues, xlWhole, , xlPrevious).Row
Set MyR = Range(Cells(x, Clc), Cells(y, Clc)).SpecialCells(2)
MyV = "[ " & Mth & "月の集計 ]" & vbLf
If x = y Then
MyV = MyV & Cells(x, 6).Value & " : 1 件"
ElseIf MyR.Areas.Count = 1 Then
MyV = MyV & Cells(x, 6).Value & _
" : " & MyR.Cells.Count & " 件"
Else
For Each C In MyR.Areas
CR = C.Row + C.Cells.Count
MyV = MyV & Cells(CR - 1, 6).Value & " : " & _
C.Cells.Count & " 件" & vbLf
Next
MyV = Left$(MyV, Len(MyV) - 1)
End If
With ActiveSheet.TextBoxes
If .Count = 1 Then
.Item(1).Text = MyV
ElseIf .Count = 0 Then
With .Add(0, 0, 200, 100)
.Text = MyV
.AutoSize = True
.Shadow = True
.Interior.ColorIndex = 20
End With
End If
End With
With Range("A1").CurrentRegion
.RemoveSubtotal
Range("A2", Range("A65536").End(xlUp).Offset(2)) _
.SpecialCells(4).EntireRow.Delete xlShiftUp
.Sort Key1:=Range("A1"), Order1:=xlAscending, _
Header:=xlYes, Orientation:=xlSortColumns
End With
Set MyR = Nothing: Columns(Clc).ClearContents
ELine:
Application.ScreenUpdating = True
End Sub
|
|