MW211 EXIT

devlog
ExcelVBA/ExcelのRange型からCells型への変換
2019年09月07日
ということでExcelVBA版を作った。いい感じ。
┌──────────────────────────────────────┐
│Public Sub セル範囲変換Range→Cells()                                       │
│    Dim theCell As Range                                                    │
│    Dim theMatches As Object, theMatch  As Object                           │
│    Dim REG As Object                                                       │
│    Set REG = CreateObject("VBScript.RegExp")                               │
│    REG.Global = False                                                      │
│    For Each theCell In Selection                                           │
│        ' Range("A1")形式を置換                                             │
│        REG.Pattern = "Range\(""([A-Z]+)(\d+)""\)"                          │
│        Do While REG.Test(theCell.Value)                                    │
│            Set theMatches = REG.Execute(theCell.Value)                     │
│            For Each theMatch In theMatches                                 │
│                theCell.Value = REG.Replace( _                              │
│                    theCell.Value, _                                        │
│               "Cells($2, " & Columns(theMatch.SubMatches(0)).Column & ")" _│
│                )                                                           │
│            Next theMatch                                                   │
│        Loop                                                                │
│        ' Range("A1:B2")形式を置換                                          │
│        REG.Pattern = "Range\(""([A-Z]+)(\d+):([A-Z]+)(\d+)""\)"            │
│        Do While REG.Test(theCell.Value)                                    │
│            Set theMatches = REG.Execute(theCell.Value)                     │
│            For Each theMatch In theMatches                                 │
│                theCell.Value = REG.Replace( _                              │
│                    theCell.Value, _                                        │
│                    "Range(" _                                              │
│      & "Cells($2, " & Columns(theMatch.SubMatches(0)).Column & ")" & ", " _│
│      & "Cells($4, " & Columns(theMatch.SubMatches(2)).Column & ")" & ")" _ │
│                )                                                           │
│            Next theMatch                                                   │
│        Loop                                                                │
│    Next theCell                                                            │
│End Sub                                                                     │
└──────────────────────────────────────┘
分類:ExcelVBA