Sub 索引のノンブルを展開()
Application.ScreenUpdating = False
Dim i, j As Long Dim tmp As Variant Dim tmp_0 As Long Dim tmp_1 As Long Dim sepNakaten As String Dim sepTilde As String Dim nombreCol As Variant
sepNakaten = "・" sepTilde = "〜" nombreCol = "B"
For i = Cells(1, nombreCol).SpecialCells(xlLastCell).Row To 2 Step -1 tmp = Split(Cells(i, nombreCol), sepNakaten) If UBound(tmp) > 0 Then For j = UBound(tmp) To 0 Step -1 Rows(i).Copy Rows(i).Insert Shift:=xlDown Cells(i + 1, nombreCol) = tmp(j) Next j Rows(i).Delete End If Next i
For i = Cells(1, nombreCol).SpecialCells(xlLastCell).Row To 2 Step -1 tmp = Split(Cells(i, nombreCol), sepTilde) If UBound(tmp) > 0 Then tmp_0 = Val(tmp(0)) tmp_1 = Val(tmp(1)) For j = tmp_1 - tmp_0 To 0 Step -1 Rows(i).Copy Rows(i).Insert Shift:=xlDown Cells(i + 1, nombreCol) = tmp_0 + j Next j Rows(i).Delete End If Next i
Application.ScreenUpdating = True MsgBox "索引のノンブルを展開しました。"
End Sub
Sub 索引のノンブルを結合()
Application.ScreenUpdating = False
Dim i As Long Dim maxrow As Long Dim sepNakaten As String Dim sepTilde As String Dim itemCol As Variant Dim nombreCol As Variant itemCol = "A" nombreCol = "B" sepNakaten = "・" sepTilde = "〜" If Columns(itemCol).Column >= Columns(nombreCol).Column Then MsgBox "ノンブルは索引項目名より右の列にしてください。" End End If With ActiveSheet.Sort.SortFields .Clear For i = 0 To Columns(nombreCol).Column - Columns(itemCol).Column .Add Key:=Columns(Columns(itemCol).Column + i), Order:=xlAscending Next i End With With ActiveSheet.Sort .SetRange Cells .Header = xlYes .Apply End With Cells.RemoveDuplicates Columns:=Array(Columns(itemCol).Column, Columns(nombreCol).Column), Header:=xlYes Dim sepCol As Long sepCol = Columns(nombreCol).Column + 1 Columns(sepCol).Insert
maxrow = Cells(1, nombreCol).SpecialCells(xlLastCell).Row - 1 For i = 2 To maxrow If Cells(i, itemCol) = Cells(i + 1, itemCol) Then If Cells(i, nombreCol) <> Cells(i + 1, nombreCol) - 1 Then Cells(i, sepCol) = sepNakaten ElseIf Cells(i, nombreCol) = Cells(i + 1, nombreCol) - 1 And (Cells(i - 1, itemCol) <> Cells(i, itemCol) Or Cells(i - 1, nombreCol) <> Cells(i, nombreCol) - 1) And (Cells(i + 1, itemCol) <> Cells(i + 2, itemCol) Or Cells(i + 1, nombreCol) <> Cells(i + 2, nombreCol) - 1) Then Cells(i, sepCol) = sepNakaten Else Cells(i, sepCol) = sepTilde End If End If Next i For i = maxrow To 2 Step -1 If Cells(i, sepCol) = sepTilde And Cells(i - 1, sepCol) = sepTilde Then Rows(i).Delete End If Next i For i = maxrow To 2 Step -1 If Cells(i - 1, sepCol) <> "" Then Cells(i - 1, nombreCol) = Cells(i - 1, nombreCol) & Cells(i - 1, sepCol) & Cells(i, nombreCol) Rows(i).Delete End If Next i Columns(sepCol).Delete Application.ScreenUpdating = True MsgBox "ノンブルを結合しました。" End Sub
|