EXCEL のワークシートに挿入されたテーブルにはデフォルトでオートフィルターが設定されています.このテーブルに対してオートフィルターをかけた結果を VBA で取得する方法は難解で,従来の考え方とは少し異なります.
より抽象度の高い考え方をする必要があります.リレーショナルデータベースの概念である集合論を理解する必要があります.
with Database, Statistics and Nutrition
EXCEL のワークシートに挿入されたテーブルにはデフォルトでオートフィルターが設定されています.このテーブルに対してオートフィルターをかけた結果を VBA で取得する方法は難解で,従来の考え方とは少し異なります.
より抽象度の高い考え方をする必要があります.リレーショナルデータベースの概念である集合論を理解する必要があります.
森林を構成する樹木の分布と積算温度には対応が見られます.暖かい地方では冬の寒さが,寒い地方では夏の暑さが植物の分布を制約するためです.吉良竜夫はこの点に注目し,暖かさの指数warmth indexおよび寒さの指数coldness indexという温量指数を考案しました.
暖かさの指数とは『月平均気温が5℃を越す月の平均気温から5℃を引いた値の合計』です.寒さの指数とは『月平均気温が5℃未満の月について,月の平均気温と5℃との差の合計』でマイナスをつけて表現します.温量指数と言う場合,普通は暖かさの指数を指します.
日本の植生帯を特徴づける樹木の分布帯と暖かさの指数との関係をみると,180, 85, 45, 15のところにそれぞれの植生帯の上端(すなわち低温側の分布限界)が集中していると言われます.それに基づいて日本の気候は次のように分類されます.
気象庁のサイトでは日本全国の過去の気象データを蓄積しており,それらをダウンロードすることができます.今回は EXCEL VBA を用いて Web Query によりデータをダウンロードし,温量指数を計算する方法を述べます.
方法はこちらのページ(EXCEL VBAで気象庁ホームページから風向風速,降水量,気温,日照時間のデータをダウンロードする)に詳しいですが,同ページのコードが動かなかったため,再度マクロの記録から接続文字列などを検証しました.
まずは手動でダウンロードする場合です.気象庁の過去の気象データ検索ページを開きます.地点,年月日,データの種類で絞り込みます.任意の地点を選択します.ここで必要なのは月平均気温ですので,年月日は年を指定し,月と日は指定せずにおきます.データの種類は『****年の月ごとの値を表示』です.
ここでブラウザのURLを取得します.
https://www.data.jma.go.jp/obd/stats/etrn/view/monthly_a1.php?prec_no=55&block_no=0552&year=1977&month=&day=&view=p1
year= で年を指定しており,ここを変数化してループすればEXCELでデータを自動抽出できます.以下コードです.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
Option Explicit Sub WarmthIndex() Dim mySht As Worksheet Dim myWeather(1048574, 15) As Variant Dim myURL As String Dim myYear As Integer Dim myMonth As Integer Dim myDate As Date Dim myRng As Range Dim myPlace As String Dim i As Integer Dim j As Long Dim myTime As Single Application.ScreenUpdating = False j = 0 myDate = Date myPlace = "**" myTime = Timer For myYear = 1977 To Year(myDate) - 1 myURL = "URL;https://www.data.jma.go.jp/obd/stats/etrn/view/monthly_a1.php?prec_no=55&block_no=0552&year=" & myYear & "&month=&day=&view=p1" Set mySht = Worksheets.Add Set myRng = mySht.Range("$A$1") With mySht.QueryTables.Add(Connection:=myURL, Destination:=myRng) .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = """tablefix1""" .Refresh BackgroundQuery:=False End With Set myRng = myRng.CurrentRegion With myRng .Replace What:=" ", Replacement:="" .Replace What:="]", Replacement:="" .Replace What:=")", Replacement:="" .Replace What:="/", Replacement:="" End With For i = 0 To myRng.Rows.Count - 4 myWeather(j, 0) = myPlace myWeather(j, 1) = myYear myWeather(j, 2) = myRng(i + 4, 1) myWeather(j, 3) = myRng(i + 4, 2) myWeather(j, 4) = myRng(i + 4, 3) myWeather(j, 5) = myRng(i + 4, 4) myWeather(j, 6) = myRng(i + 4, 6) myWeather(j, 7) = myRng(i + 4, 7) myWeather(j, 8) = myRng(i + 4, 8) myWeather(j, 9) = myRng(i + 4, 9) myWeather(j, 10) = myRng(i + 4, 10) myWeather(j, 11) = myRng(i + 4, 11) myWeather(j, 12) = myRng(i + 4, 12) myWeather(j, 13) = myRng(i + 4, 13) myWeather(j, 14) = myRng(i + 4, 16) Select Case True Case myRng(i + 4, 6) - 5 <= 0 myWeather(j, 15) = 0 Case Else myWeather(j, 15) = myRng(i + 4, 6) - 5 End Select j = j + 1 Next i Application.DisplayAlerts = False mySht.Delete Application.DisplayAlerts = True Next myYear Set mySht = Worksheets.Add With mySht .Name = myYear & myPlace & "温量指数" .Range("$A$1") = "地点" .Range("$B$1") = "年" .Range("$C$1") = "月" .Range("$D$1") = "降水量合計" .Range("$E$1") = "日最大降水量" .Range("$F$1") = "1時間最大降水量" .Range("$G$1") = "日平均気温" .Range("$H$1") = "日最高気温" .Range("$I$1") = "日最低気温" .Range("$J$1") = "最高気温" .Range("$K$1") = "最低気温" .Range("$L$1") = "平均風速" .Range("$M$1") = "最大風速" .Range("$N$1") = "最大風向" .Range("$O$1") = "日照時間" .Range("$P$1") = "温量指数" .Range("$A$2:$P$1048576") = myWeather End With Debug.Print Round(Timer - myTime, 2) Set myRng = Nothing Set mySht = Nothing Application.ScreenUpdating = True End Sub |
ピボットテーブルを挿入します.設定はデフォルトで結構です.『行』には年をドラッグします.『値』には温量指数をドラッグします.『集計方法』はデータの合計です.これで温量指数が算出されます.
グラフを追加するなら折れ線グラフが良いでしょう.下図は当地の温量指数の推移ですが,1987年までは95から105の範囲を推移していたのが,1988年から2004年までは95から115までの範囲を乱高下を繰り返し,2005年以降は110近辺の範囲に収まってきているように見えます.全体として温暖化傾向が見られます.
次に,夏枯れ指数を算出します.夏枯れ指数とは『日最高気温が25℃を超えた日について25℃との差を年間で合計した値』(武井和久)のことです.基準とした25℃は寒地型芝草が弱り始める温度です.コードのみ載せます.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
Option Explicit Sub SummerSlumpIndex() Dim mySht As Worksheet Dim myWeather(1048574, 13) As Variant Dim myURL As String Dim myYear As Integer Dim myMonth As Integer Dim myDate As Date Dim myRng As Range Dim myPlace As String Dim i As Integer Dim j As Long Dim myTime As Single Application.ScreenUpdating = False j = 0 myDate = Date myPlace = "**" myTime = Timer For myYear = 1977 To Year(myDate) - 1 On Error Resume Next If DateSerial(myYear, myMonth, 1) - DateSerial(Year(myDate), Month(myDate), 1) >= 0 Then Exit For End If On Error GoTo 0 For myMonth = 1 To 12 myURL = "URL;https://www.data.jma.go.jp/obd/stats/etrn/view/daily_a1.php?prec_no=55&block_no=0552&year=" & myYear & "&month=" & myMonth & "&day=&view=p1" Set mySht = Worksheets.Add Set myRng = mySht.Range("$A$1") With mySht.QueryTables.Add(Connection:=myURL, Destination:=myRng) .WebSelectionType = xlSpecifiedTables .WebFormatting = xlWebFormattingNone .WebTables = """tablefix1""" .Refresh BackgroundQuery:=False End With Set myRng = myRng.CurrentRegion With myRng .Replace What:=" ", Replacement:="" .Replace What:="]", Replacement:="" .Replace What:=")", Replacement:="" .Replace What:="/", Replacement:="" End With For i = 0 To myRng.Rows.Count - 4 myWeather(j, 0) = myPlace myWeather(j, 1) = DateSerial(myYear, myMonth, myRng(i + 4, 1)) myWeather(j, 2) = myRng(i + 4, 2) myWeather(j, 3) = myRng(i + 4, 3) myWeather(j, 4) = myRng(i + 4, 5) myWeather(j, 5) = myRng(i + 4, 6) myWeather(j, 6) = myRng(i + 4, 7) myWeather(j, 7) = myRng(i + 4, 8) myWeather(j, 8) = myRng(i + 4, 9) myWeather(j, 9) = myRng(i + 4, 10) myWeather(j, 10) = myRng(i + 4, 13) myWeather(j, 11) = myRng(i + 4, 14) Select Case True Case myRng(i + 4, 5) - 5 <= 0 myWeather(j, 12) = 0 Case Else myWeather(j, 12) = myRng(i + 4, 5) - 5 End Select Select Case True Case myRng(i + 4, 6) - 25 <= 0 myWeather(j, 13) = 0 Case Else myWeather(j, 13) = myRng(i + 4, 6) - 25 End Select j = j + 1 Next i Application.DisplayAlerts = False mySht.Delete Application.DisplayAlerts = True Next myMonth Next myYear Set mySht = Worksheets.Add With mySht .Name = myYear & myPlace .Range("$A$1") = "地点" .Range("$B$1") = "年月日" .Range("$C$1") = "降水量合計" .Range("$D$1") = "1時間最大降水量" .Range("$E$1") = "平均気温" .Range("$F$1") = "最高気温" .Range("$G$1") = "最低気温" .Range("$H$1") = "平均風速" .Range("$I$1") = "最大風速" .Range("$J$1") = "最大風向" .Range("$K$1") = "最多風向" .Range("$L$1") = "日照時間" .Range("$M$1") = "温量指数" .Range("$N$1") = "夏枯れ指数" .Range("$A$2:$N$1048576") = myWeather End With Debug.Print Round(Timer - myTime, 2) Set myRng = Nothing Set mySht = Nothing Application.ScreenUpdating = True End Sub |
ピボットテーブルを挿入します.『行』には年月日をドラッグします.『値』には夏枯れ指数をドラッグします.『集計方法』はデータの合計です.更に『分析』タブの『グループの選択』コマンドをクリックし,『グループ化』ウィンドウで『年』のみを選択して年月日を集約します.これで夏枯れ指数が算出されます.同様に温暖化傾向が見られます.
In this article, I’d like to describe how to fix the text file of the food composition of the diet recipes, which a facility have provided for one year, to the first normal form in order to insert into database.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
Option Explicit Sub LoopProcedure() Dim Sh As Worksheet For Each Sh In Worksheets If Sh.Name Like "Sheet" & "*" Then Call TransportFromTxtToCSV(Sh) End If Next Sh End Sub Sub TransportFromTxtToCSV() Dim mySht As Worksheet Dim myRng As Range Dim myAr As Variant Dim RecAr() As String Dim i As Long Dim j As Long Dim k As Long Dim Date_Serving_Meal As Date Dim Menu_Name As String Dim tmpStart As Long Dim Meal_Time As String Dim Dish As String Dim RecordNumber As Long set mysht = sh RecordNumber = Count_Record(mySht) ReDim RecAr(RecordNumber - 1, 51) Set myRng = mySht.UsedRange myAr = myRng k = 0 Date_Serving_Meal = "2011/1/1" Menu_Name = myAr(1, 11) & myAr(1, 12) & myAr(1, 13) tmpStart = InStr(Menu_Name, ")") Menu_Name = Mid(Menu_Name, tmpStart + 1) Meal_Time = "朝食" For i = LBound(myAr) To UBound(myAr) Select Case True Case myAr(i, 2) = "合 計" Date_Serving_Meal = DateAdd("d", 1, Date_Serving_Meal) Case myAr(i, 2) = "《朝食》" Meal_Time = "朝食" Case myAr(i, 2) = "《昼食》" Meal_Time = "昼食" Case myAr(i, 2) = "《夕食》" Meal_Time = "夕食" Case myAr(i, 2) = "小 計" Case myAr(i, 2) = "^e12【献立" Case myAr(i, 2) Like "動蛋比" & "*" Case myAr(i, 2) = "・・・・・・・・・・" Case myAr(i, 2) = "料理名" Case myAr(i, 2) = "" Case Else Dish = myAr(i, 2) End Select Select Case True Case myAr(i, 3) = "・・・・・・・・・・・" Case myAr(i, 3) Like "EN比" & "*" Case myAr(i, 3) = "食品名" Case myAr(i, 3) Like "一覧表】 ^e11" & "*" Case myAr(i, 3) = "" Case Else RecAr(k, 0) = Date_Serving_Meal RecAr(k, 1) = Menu_Name RecAr(k, 2) = Meal_Time RecAr(k, 3) = Dish RecAr(k, 4) = myAr(i, 3) For j = 5 To 22 RecAr(k, j) = myAr(i, j - 1) Next j For j = 23 To 39 RecAr(k, j) = myAr(i + 1, j - 18) Next j For j = 40 To 51 RecAr(k, j) = myAr(i + 2, j - 35) Next j k = k + 1 End Select Next i Set mySht = Worksheets.Add With mySht .Name = Menu_Name .Range(Cells(1, 1), Cells(RecordNumber, 52)) = RecAr End With Set mySht = Nothing Set myRng = Nothing Erase RecAr End Sub Function Count_Record(ByRef Sh As Worksheet) As Long Dim mySht As Worksheet Dim myAr As Variant Dim i As Long Dim j As Long Dim k As Long Set mySht = Sh myAr = mySht.UsedRange j = 0 k = 0 For i = LBound(myAr) To UBound(myAr) Select Case True Case myAr(i, 2) = "合 計" Case myAr(i, 2) = "小 計" Case myAr(i, 2) = "《朝食》" Case myAr(i, 2) = "《昼食》" Case myAr(i, 2) = "《夕食》" Case myAr(i, 2) = "^e12【献立" Case myAr(i, 2) Like "動蛋比" & "*" Case myAr(i, 2) = "・・・・・・・・・・" Case myAr(i, 2) = "料理名" Case myAr(i, 2) = "" Case Else j = j + 1 End Select Select Case True Case myAr(i, 3) = "・・・・・・・・・・・" Case myAr(i, 3) Like "EN比" & "*" Case myAr(i, 3) = "食品名" Case myAr(i, 3) Like "一覧表】 ^e11" & "*" Case myAr(i, 3) = "" Case Else k = k + 1 End Select Next i Count_Record = k End Function |
ある施設の1年間で提供した食事のレシピの食品成分のテキストファイルから,データベースに取り込むための前処置としてデータを第1正規形に整形する EXCEL VBA コードです.個人的な備忘録です.
94行目以降の関数 Count_Record は,アクティブシートをループして必要なレコード数を計測する関数です.104行目以降で料理名の数(B列),119行目以降で食品名の数(C列)に注目しています.サブルーチン TransportFromTxtToCSV から呼び出して,動的配列の要素数を後で決定するのに用います.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
Option Explicit Sub LoopProcedure() Dim Sh As Worksheet For Each Sh In Worksheets If Sh.Name Like "Sheet" & "*" Then Call TransportFromTxtToCSV(Sh) End If Next Sh End Sub Sub TransportFromTxtToCSV() Dim mySht As Worksheet Dim myRng As Range Dim myAr As Variant Dim RecAr() As String Dim i As Long Dim j As Long Dim k As Long Dim Date_Serving_Meal As Date Dim Menu_Name As String Dim tmpStart As Long Dim Meal_Time As String Dim Dish As String Dim RecordNumber As Long set mysht = sh RecordNumber = Count_Record(mySht) ReDim RecAr(RecordNumber - 1, 51) Set myRng = mySht.UsedRange myAr = myRng k = 0 Date_Serving_Meal = "2011/1/1" Menu_Name = myAr(1, 11) & myAr(1, 12) & myAr(1, 13) tmpStart = InStr(Menu_Name, ")") Menu_Name = Mid(Menu_Name, tmpStart + 1) Meal_Time = "朝食" For i = LBound(myAr) To UBound(myAr) Select Case True Case myAr(i, 2) = "合 計" Date_Serving_Meal = DateAdd("d", 1, Date_Serving_Meal) Case myAr(i, 2) = "《朝食》" Meal_Time = "朝食" Case myAr(i, 2) = "《昼食》" Meal_Time = "昼食" Case myAr(i, 2) = "《夕食》" Meal_Time = "夕食" Case myAr(i, 2) = "小 計" Case myAr(i, 2) = "^e12【献立" Case myAr(i, 2) Like "動蛋比" & "*" Case myAr(i, 2) = "・・・・・・・・・・" Case myAr(i, 2) = "料理名" Case myAr(i, 2) = "" Case Else Dish = myAr(i, 2) End Select Select Case True Case myAr(i, 3) = "・・・・・・・・・・・" Case myAr(i, 3) Like "EN比" & "*" Case myAr(i, 3) = "食品名" Case myAr(i, 3) Like "一覧表】 ^e11" & "*" Case myAr(i, 3) = "" Case Else RecAr(k, 0) = Date_Serving_Meal RecAr(k, 1) = Menu_Name RecAr(k, 2) = Meal_Time RecAr(k, 3) = Dish RecAr(k, 4) = myAr(i, 3) For j = 5 To 22 RecAr(k, j) = myAr(i, j - 1) Next j For j = 23 To 39 RecAr(k, j) = myAr(i + 1, j - 18) Next j For j = 40 To 51 RecAr(k, j) = myAr(i + 2, j - 35) Next j k = k + 1 End Select Next i Set mySht = Worksheets.Add With mySht .Name = Menu_Name .Range(Cells(1, 1), Cells(RecordNumber, 52)) = RecAr End With Set mySht = Nothing Set myRng = Nothing Erase RecAr End Sub Function Count_Record(ByRef Sh As Worksheet) As Long Dim mySht As Worksheet Dim myAr As Variant Dim i As Long Dim j As Long Dim k As Long Set mySht = Sh myAr = mySht.UsedRange j = 0 k = 0 For i = LBound(myAr) To UBound(myAr) Select Case True Case myAr(i, 2) = "合 計" Case myAr(i, 2) = "小 計" Case myAr(i, 2) = "《朝食》" Case myAr(i, 2) = "《昼食》" Case myAr(i, 2) = "《夕食》" Case myAr(i, 2) = "^e12【献立" Case myAr(i, 2) Like "動蛋比" & "*" Case myAr(i, 2) = "・・・・・・・・・・" Case myAr(i, 2) = "料理名" Case myAr(i, 2) = "" Case Else j = j + 1 End Select Select Case True Case myAr(i, 3) = "・・・・・・・・・・・" Case myAr(i, 3) Like "EN比" & "*" Case myAr(i, 3) = "食品名" Case myAr(i, 3) Like "一覧表】 ^e11" & "*" Case myAr(i, 3) = "" Case Else k = k + 1 End Select Next i Count_Record = k End Function |
EXCEL VBA において,2 つの配列が等しいか否か調べたいことはよくあります.更に2つの配列の差分,つまり互いに重複しない要素を取り出したいという需要もあります.今回は配列の差分を求めるコードを紹介します.
2つの配列が等しいかをまず調べ,等しくない場合に次に一方が他方の真部分集合であるか否かを調べます.最後に配列の差分を求めます.この順になっているのは全く同じ2つの配列の差分は空集合となって解が求まらないためであり,引き算される配列が引き算する配列の真部分集合の場合にも結果が空集合となり解が求まらないためです.
Array1 の配列要素全体を A, Array2 の配列要素全体を B とすると Array1 から Array2 を差し引いた差分は下式で表現されます.
二重ループによりそれぞれの要素を比較して一致した数を求めていますが,これは積集合を取っていることに他なりません.EXCELワークシートにおいて数式の参照元の最初のセルおよび参照先の最後のセルを取得するでも述べましたが,2つの集合の和集合と積集合の要素数が等しければ元の2つの集合は全く同じです.ここでのロジックは積集合の要素数と元の集合の要素数が等しければ元の2つの集合は等しい筈であるとの前提に立っています.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Option Explicit Function COMPARE_ARRAY(ByRef Array1() As String, ByRef Array2() As String) As Boolean Dim i As Long Dim j As Long Dim k As Long k = 0 For i = LBound(Array1) To UBound(Array1) For j = LBound(Array2) To UBound(Array2) If Array1(i) = Array2(j) Then k = k + 1 End If Next j Next i If i = j And i = k Then COMPARE_ARRAY = True Else COMPARE_ARRAY = False End If End Function Function PROPERSUBSET_ARRAY(ByRef Array1() As String, ByRef Array2() As String) As Boolean Dim i As Long Dim j As Long Dim k As Long k = 0 For i = LBound(Array1) To UBound(Array1) For j = LBound(Array2) To UBound(Array2) If Array1(i) = Array2(j) Then k = k + 1 End If Next j Next i If k = i Then PROPERSUBSET_ARRAY = True Else PROPERSUBSET_ARRAY = False End If End Function |
下記関数では第 3 引数の Compare の指定により Array1 から Array2 の差分を求めるか,Array2 から Array1 の差分を求めるかを指定しています.但し,配列内の要素にもともと空白文字列があった場合にはうまく動作しません.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Function EXCEPT_ARRAY(ByRef Array1() As String, ByRef Array2() As String, ByVal Compare As Boolean) As String() Dim i As Long Dim j As Long Dim k As Long Dim InternalAr1() As String Dim InternalAr2() As String Dim Ar1Ar() As String Dim Ar2Ar() As String InternalAr1 = Array1 InternalAr2 = Array2 For i = LBound(InternalAr1) To UBound(InternalAr1) For j = LBound(InternalAr2) To UBound(InternalAr2) If InternalAr1(i) = InternalAr2(j) Then InternalAr1(i) = "" InternalAr2(j) = "" k = k + 1 End If Next j Next i k = 0 For i = LBound(InternalAr1) To UBound(InternalAr1) If InternalAr1(i) = "" Then Else ReDim Preserve Ar1Ar(k) Ar1Ar(k) = InternalAr1(i) k = k + 1 End If Next i k = 0 For j = LBound(InternalAr2) To UBound(InternalAr2) If InternalAr2(j) = "" Then Else ReDim Preserve Ar2Ar(k) Ar2Ar(k) = InternalAr2(j) k = k + 1 End If Next j If Compare Then EXCEPT_ARRAY = Ar1Ar Else EXCEPT_ARRAY = Ar2Ar End If End Function |
参照:
EXCELワークシートにおいて数式の参照元の最初のセルおよび参照先の最後のセルを取得する
When you’d like to compare 2 arrays in EXCEL VBA whether they are equal or not, what would you do? Furthermore, you might get different elements that don’t overlap each other. I’d like to describe the code that how to get differences of 2 arrays.
At first, you would have to check whether they are equal each other or not because the difference of equal arrays is empty set. Next, you would have to check whether an array is proper subset of another array or not. At last, you could get difference between 2 arrays.
See formula as below, it is shown all elements of Array1 as “A”, all elements of Array2 as “B” and differences elements from Array1 and Array2 as “A And (not B)”.
The following code compares elements between two set and get number of equal elements with double loop, that means to get intersection of 2 sets.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
Option Explicit Function COMPARE_ARRAY(ByRef Array1() As String, ByRef Array2() As String) As Boolean Dim i As Long Dim j As Long Dim k As Long k = 0 For i = LBound(Array1) To UBound(Array1) For j = LBound(Array2) To UBound(Array2) If Array1(i) = Array2(j) Then k = k + 1 End If Next j Next i If i = j And i = k Then COMPARE_ARRAY = True Else COMPARE_ARRAY = False End If End Function Function PROPERSUBSET_ARRAY(ByRef Array1() As String, ByRef Array2() As String) As Boolean Dim i As Long Dim j As Long Dim k As Long k = 0 For i = LBound(Array1) To UBound(Array1) For j = LBound(Array2) To UBound(Array2) If Array1(i) = Array2(j) Then k = k + 1 End If Next j Next i If k = i Then PROPERSUBSET_ARRAY = True Else PROPERSUBSET_ARRAY = False End If End Function |
Configuration of the 3rd argument “Compare” specifies which array should be excepted, when it was TRUE the function would except the latter from the former, when it was FALSE then it would except the former from the latter, respectively. It’s assumed that the elements of arrays has no empty string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
Function EXCEPT_ARRAY(ByRef Array1() As String, ByRef Array2() As String, ByVal Compare As Boolean) As String() Dim i As Long Dim j As Long Dim k As Long Dim InternalAr1() As String Dim InternalAr2() As String Dim Ar1Ar() As String Dim Ar2Ar() As String InternalAr1 = Array1 InternalAr2 = Array2 For i = LBound(InternalAr1) To UBound(InternalAr1) For j = LBound(InternalAr2) To UBound(InternalAr2) If InternalAr1(i) = InternalAr2(j) Then InternalAr1(i) = "" InternalAr2(j) = "" k = k + 1 End If Next j Next i k = 0 For i = LBound(InternalAr1) To UBound(InternalAr1) If InternalAr1(i) = "" Then Else ReDim Preserve Ar1Ar(k) Ar1Ar(k) = InternalAr1(i) k = k + 1 End If Next i k = 0 For j = LBound(InternalAr2) To UBound(InternalAr2) If InternalAr2(j) = "" Then Else ReDim Preserve Ar2Ar(k) Ar2Ar(k) = InternalAr2(j) k = k + 1 End If Next j If Compare Then EXCEPT_ARRAY = Ar1Ar Else EXCEPT_ARRAY = Ar2Ar End If End Function |
It’s needed to enter password which requires a character, a number and a symbol at least, respectively. I’d like to describe how to validate password with VBScript®. It’s assumed that the length of password is 8 or greater.
In 23 line, the constraint is shown. In 22 line with comment, it’s shown that constrains password to require both a single-byte character and number at least and 8 or greater character length.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Option Explicit Private Sub CommandButton1_Click() With TextBox1 If Not CheckPassword(.Text) Then .SetFocus .SelStart = 0 .SelLength = Len(.Text) Exit Sub Else End If End With Unload Me End Sub Function CheckPassword(InputString As String) As Boolean Dim myReg As Object CheckPassword = False Set myReg = CreateObject("VBScript.RegExp") With myReg '.Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,})$" .Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)(?!^[!-/:-@[-`{-~]*$)(?!^[a-zA-Z0-9]*$)(?!^[!-@[-`{-~]*$)(?!^[!-/:-~]*$)^([!-~]{8,})$" .IgnoreCase = False .Global = True End With If myReg.Test(InputString) Then CheckPassword = True End If Set myReg = Nothing End Function Private Sub UserForm_Initialize() With TextBox1 .IMEMode = fmIMEModeDisable .PasswordChar = "*" End With End Sub |
You might need description here. The pattern (?!pattern) means such negative lookahead as EXCEPT operator effects in SQL. To seek area three circles overlap, it’s needed to remove areas around. After filtering out not required patterns with negative lookahead, it validates length of the password. The number of required pattern, which verifies n types of letter, is 2n – 2.
Not Needed | Negative Lookahead Pattern |
Number | (?!^[0-9]*$) |
Character | (?!^[a-zA-Z]*$) |
Symbol | (?!^[!-/:-@[-`{-~]*$) |
Character and number | (?!^[a-zA-Z0-9]*$) |
Number and symbol | (?!^[!-@[-`{-~]*$) |
Character and symbol | (?!^[!-/:-~]*$) |
References:
Regular Expression Language – Quick Reference
How To: Use Regular Expressions to Constrain Input in ASP.NET
ASCII character code list (0-127)
Userform of Excel VBA as user interface
パスワード設定の際に半角数字,半角英字,半角記号をそれぞれ最低でも 1 文字使用するよう求められるケースは多いと思います.今回は VBScript の正規表現を用いてパスワードをチェックする方法を紹介します.
制約条件を半角英数字,半角記号を最低でも 1 文字用いることとし,文字列長を 8 文字以上とします.下図のようにユーザーフォーム上にラベルとテキストボックスとコマンドボタンを配置します.それぞれ Label1, TextBox1, CommandButton1 とします.
下記コードの 23 行目で制約条件を表現します.コメントアウトした 22 行目は半角英数字のみを 8 文字以上用いる場合の正規表現です.文字クラス内でエスケープが必要なメタ文字は \ と ] の 2 種類です.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
Option Explicit Private Sub CommandButton1_Click() With TextBox1 If Not CheckPassword(.Text) Then .SetFocus .SelStart = 0 .SelLength = Len(.Text) Exit Sub Else End If End With Unload Me End Sub Function CheckPassword(InputString As String) As Boolean Dim myReg As Object CheckPassword = False Set myReg = CreateObject("VBScript.RegExp") With myReg '.Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{8,})$" .Pattern = "(?!^[0-9]*$)(?!^[a-zA-Z]*$)(?!^[!-/:-@[-`{-~]*$)(?!^[a-zA-Z0-9]*$)(?!^[!-@[-`{-~]*$)(?!^[!-/:-~]*$)^([!-~]{8,})$" .IgnoreCase = False .Global = True End With If myReg.Test(InputString) Then CheckPassword = True End If Set myReg = Nothing End Function Private Sub UserForm_Initialize() With TextBox1 .IMEMode = fmIMEModeDisable .PasswordChar = "*" End With End Sub |
ここで解説が必要かと思います.(?!pattern) は否定先読みを示し,SQL で言うところの EXCEPT 演算子と同じ働きをします.半角英数字と半角記号を最低でも 1 文字以上使用するとは,下表の文字の組み合わせを許可しないということです.下図の 3 つの円の重なる領域を求めるには,その周辺の領域を引き算して求めます.許可しないパターンを否定先読みで予めフィルタリングしておき,最後に全種類の文字クラスの文字列長をチェックしています.集合論とも考え方の重なる領域です.ちなみに,n 種類の文字種を検証するのに必要な否定先読みのパターン数は 2n – 2 です.
Not Needed | Negative Lookahead Pattern |
Number | (?!^[0-9]*$) |
Character | (?!^[a-zA-Z]*$) |
Symbol | (?!^[!-/:-@[-`{-~]*$) |
Character and number | (?!^[a-zA-Z0-9]*$) |
Number and symbol | (?!^[!-@[-`{-~]*$) |
Character and symbol | (?!^[!-/:-~]*$) |
参照:
ASP.NET への入力を制約するために正規表現を使用する方法
正規表現の構文
ASCII文字コード(0-127)一覧表
インターフェースとしてのEXCEL VBAによるユーザーフォーム
I have asked National Institute of Health and Nutrition if I could publish the text file revised edition of “METs table of Physical Activities” on this blog on July 3rd, 2012. I have received e-mail from staff on July 12, 2012, they consent that I publish the text file.
The file has 4 columns and 826 rows. First row shows data structure. First column shows code, second column METS value, third column major heading, and fourth column specific activities. I couldn’t separate English from Japanese.
The source is this PDF file. Although you don’t have to contact National Institute of Health and Nutrition when you would like to use for yourself, you would be asked to contact National Institute of Health and Nutrition when you would like to publish the file or create a web service with the file.
2012年7月3日の記事で国立健康・栄養研究所に『身体活動メッツ表』から抽出したテキストファイルを公開して良いか問い合わせていましたが,7月12日回答があり,公開を許可して頂けましたので公開いたします.
下記ファイルのデータ構造は4列826行となっています.1行目はデータ構造を示しており,2行目以降がデータです.1列目はコード,2列目はMETs,3列目は大分類,4列目は個別活動となっています.日本語と英語の切り分けはできていません.
当ファイルは国立健康・栄養研究所の改訂版『身体活動メッツ表』を元に 作成したものです.個人利用においては特に連絡の必要はございませんが,ウェブサービス等,第三者が利用するサービスに当ファイルを使用する場合には国立健康・栄養研究所に確認のご連絡をお願いします.
2014 年 11 月 7 日,指摘を受け METS2011.csv ファイルを訂正しました.
National Institute of Health and Nutrition has revised ‘METs table of Physical Activities’ 2011 edition. Because it’s difficult to use as PDF file, I have extracted text data from it. I have asked if I could publish the file on this blog on July 3rd, 2012.
1. Open the file and copy all text.
2. Create new EXCEL book and paste with ‘Text File Wizard’. In the second tab, you have to remove all check mark of delimiters. In the third tab, select ‘String’ data type of column.
3. Press ‘Alt’ key and ‘F11’ key to launch VBE, insert module, and run the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
Option Explicit Sub METS() Dim mySht As Worksheet Dim myRng As Range Dim myAr As Variant Dim i As Long Dim j As Long Dim myReg As Object Dim myMatches As Object Dim myMatch As Object Const strReg As String = "^[0-9]{5}$" Dim CODE() As String Dim METS() As Single Dim MajorHeading() As String Dim SpecificActivities() As String Dim myArray() As Variant Set myReg = CreateObject("VBScript.RegExp") With myReg .Pattern = strReg .IgnoreCase = True .Global = True End With Set mySht = ActiveSheet Set myRng = mySht.UsedRange myAr = myRng j = 0 For i = LBound(myAr) To UBound(myAr) If myReg.Test(myAr(i, 1)) Then Set myMatches = myReg.Execute(myAr(i, 1)) ReDim Preserve CODE(j) ReDim Preserve METS(j) ReDim Preserve MajorHeading(j) ReDim Preserve SpecificActivities(j) CODE(j) = myAr(i, 1) METS(j) = myAr(i + 1, 1) MajorHeading(j) = myAr(i + 2, 1) SpecificActivities(j) = myAr(i + 3, 1) j = j + 1 End If Next i ReDim myArray(j, 3) For j = LBound(myArray) To UBound(myArray) - 1 myArray(j, 0) = CODE(j) myArray(j, 1) = METS(j) myArray(j, 2) = MajorHeading(j) myArray(j, 3) = SpecificActivities(j) Next j Set mySht = Worksheets.Add mySht.Range("A1").Value = "CODE" mySht.Range("B1").Value = "METS" mySht.Range("C1").Value = "MajorHeading" mySht.Range("D1").Value = "SpecificActivities" mySht.Range("A2:D827") = myArray End Sub |
国立健康・栄養研究所が2011年版『身体活動のメッツ表』を改訂しました.下記リンクはPDFファイルですが,そのままでは使いにくいためEXCELでテキスト情報を抽出しました.2012年7月3日,国立健康・栄養研究所に抽出したテキストファイルを公開して良いか問い合わせました.
1. PDFファイルを開き,全てを選択してコピーします.
2. EXCELの新規ブックを作成し,テキストファイルウィザードを使用して貼り付けます.この際,テキストファイルウィザードの2番目のタブで区切り文字の全てのチェックを外して下さい.3番目のタブでは列のデータ形式を『文字列』に変更して下さい.
3. ‘Alt’ キーと ‘F11’ キーを押下して VBE を起動し,標準モジュールを挿入して下記コードを実行して下さい.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
Option Explicit Sub METS() Dim mySht As Worksheet Dim myRng As Range Dim myAr As Variant Dim i As Long Dim j As Long Dim myReg As Object Dim myMatches As Object Dim myMatch As Object Const strReg As String = "^[0-9]{5}$" Dim CODE() As String Dim METS() As Single Dim MajorHeading() As String Dim SpecificActivities() As String Dim myArray() As Variant Set myReg = CreateObject("VBScript.RegExp") With myReg .Pattern = strReg .IgnoreCase = True .Global = True End With Set mySht = ActiveSheet Set myRng = mySht.UsedRange myAr = myRng j = 0 For i = LBound(myAr) To UBound(myAr) If myReg.Test(myAr(i, 1)) Then Set myMatches = myReg.Execute(myAr(i, 1)) ReDim Preserve CODE(j) ReDim Preserve METS(j) ReDim Preserve MajorHeading(j) ReDim Preserve SpecificActivities(j) CODE(j) = myAr(i, 1) METS(j) = myAr(i + 1, 1) MajorHeading(j) = myAr(i + 2, 1) SpecificActivities(j) = myAr(i + 3, 1) j = j + 1 End If Next i ReDim myArray(j, 3) For j = LBound(myArray) To UBound(myArray) - 1 myArray(j, 0) = CODE(j) myArray(j, 1) = METS(j) myArray(j, 2) = MajorHeading(j) myArray(j, 3) = SpecificActivities(j) Next j Set mySht = Worksheets.Add mySht.Range("A1").Value = "CODE" mySht.Range("B1").Value = "METS" mySht.Range("C1").Value = "MajorHeading" mySht.Range("D1").Value = "SpecificActivities" mySht.Range("A2:D827") = myArray End Sub |
According to the article, download the PDF files ‘1299012_1.pdf’ to ‘1299012_18.pdf’. Corresponding to each PDF file in PDF files, copy all text from one file and option paste to one worksheet. As a result, you would make 18 worksheets in a book. In the first tab of ‘Text File Wizard’, select option ‘The data field separated by delimiters such as comma or tab’. Go to the last tab without any change in second tab. In the last tab, change option data type of the first column to ‘String’. Mainly in column A of all worksheets, you have to fix cell value by yourself. Save the book as ‘Category.xlsm’. Furthermore, download the EXCEL book from this site, copy worksheet from it to ‘Category.xlsm’ which you previously prepared, and change the sheet name to ‘Sheet0’.
Copy or move the worksheet, which you made at Classify the Item_Number of the ‘Standard Tables of Food Composition in Japan 2010′, Part 1, to ‘Category.xlsm’. As a result, ‘Category.xlsm’ book has 20 worksheets. Press ‘Alt’ key and ‘F11’ key to launch VBE, insert module and run the code below. The code makes ‘M_CATEGORY’ sheet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
Option Explicit Sub Select_Class() Dim tmpSht As Worksheet Dim tmpRng As Range Dim tmpArray As Variant Dim workArray As Variant Dim h As Long Dim i As Long Dim j As Long Dim k As Long Dim l As Long Dim m As Long Dim n As Long Dim p As Long Dim q As Long Dim r As Long Dim RegExp_Japanese As Object Dim RegExp_English As Object Dim RegExp_ItemNum As Object Const PtnJPN As String = "[^A-Za-z0-9'\.\-\*]{2,}" Const PtnENG As String = "^[A-Za-z0-9'\,\.\-\%]+$" Const PtnItemNum As String = "^[0-9]{5}$" Dim Item_Number() As String Dim JapaneseItem() As String Dim EnglishItem() As String Dim EnglishString As String Dim JapaneseClass() As String Dim English_Class() As String Dim ClassStringEN As String Dim ItemNumArray() As String Dim ItemENGArray() As String Dim ClassArrayJP() As String Dim ClassArrayEN() As String Dim RegExp_AngleBracket As Object Dim RegExp_RoundStartJP As Object Dim RegExp_RoundStartEN As Object Dim RegExp_RoundExitEN As Object Const Ptn_Round_Start As String = "^(\(|()" Const Ptn_Round_Exit As String = "(\)|))$" Dim StringRoundEnglish As String Dim SubClassJapanese() As String Dim SubClass_English() As String Dim RegExp_Square_Start As Object Dim RegExp_SquareExitEN As Object Const Ptn_Angle_Start As String = "^[<<]" Const Ptn_SquareStart As String = "^\[" Const Ptn_Square_Exit As String = "\]$" Dim MidleClassJP() As String Dim MidleClassEN() As String Dim StrMidClassENG As String Dim SubClass_JPN() As String Dim SubClass_ENG() As String Dim mySht As Worksheet Dim myRng As Range Dim myAr As Variant Dim workArray2() As String Dim workArray3() As String Dim mySht2 As Worksheet Dim myRng2 As Range Dim myAr2 As Variant Dim CEREALS As Long Dim POTATOES As Long Dim SUGARS As Long Dim PULSES As Long Dim NUTS As Long Dim VEGETABLES As Long Dim FRUITS As Long Dim MUSHROOMS As Long Dim ALGAE As Long Dim FISHES As Long Dim MEATS As Long Dim EGGS As Long Dim MILK As Long Dim OIL As Long Dim CONFECTIONERIES As Long Dim BEVERAGES As Long Dim SEASONINGS As Long Dim PREPARED As Long Set RegExp_Japanese = CreateObject("VBScript.RegExp") With RegExp_Japanese .Pattern = PtnJPN .IgnoreCase = True .Global = True End With Set RegExp_English = CreateObject("VBScript.RegExp") With RegExp_English .Pattern = PtnENG .IgnoreCase = True .Global = True End With Set RegExp_ItemNum = CreateObject("VBScript.RegExp") With RegExp_ItemNum .Pattern = PtnItemNum .IgnoreCase = True .Global = True End With Set RegExp_Square_Start = CreateObject("VBScript.RegExp") With RegExp_Square_Start .Pattern = Ptn_SquareStart .IgnoreCase = True .Global = True End With Set RegExp_SquareExitEN = CreateObject("VBScript.RegExp") With RegExp_SquareExitEN .Pattern = "[A-Za-z0-9'\,\.\-\%]+" & Ptn_Square_Exit .IgnoreCase = True .Global = True End With Set RegExp_RoundStartJP = CreateObject("VBScript.RegExp") With RegExp_RoundStartJP .Pattern = Ptn_Round_Start & "[^A-Za-z0-9'\.\-\*]{2,}" .IgnoreCase = True .Global = True End With Set RegExp_RoundStartEN = CreateObject("VBScript.RegExp") With RegExp_RoundStartEN .Pattern = Ptn_Round_Start & "[A-Za-z'\,\.\-\%]+" End With Set RegExp_RoundExitEN = CreateObject("VBScript.RegExp") With RegExp_RoundExitEN .Pattern = "[A-Za-z0-9'\,\.\-\%]+" & Ptn_Round_Exit .IgnoreCase = True .Global = True End With j = 0 k = 0 l = 0 m = 0 q = 0 For Each tmpSht In Worksheets If tmpSht.Name = "M_CATEGORY" Then MsgBox prompt:="This book already has M_CATEGORY sheet." & vbCrLf & _ "Exit procedure.", _ Buttons:=vbOKOnly, _ Title:="Internal Error" Exit Sub End If If tmpSht.Name <> "Sheet0" And _ tmpSht.Name <> "Sheet00" And _ tmpSht.Name <> "Result" Then Set tmpRng = tmpSht.UsedRange tmpArray = tmpRng workArray = NoCancelArray(tmpArray) For h = LBound(workArray) To UBound(workArray) For i = workArray(h, 0) To workArray(h, 1) On Error Resume Next If RegExp_ItemNum.Test(tmpArray(i, 1)) And _ tmpArray(i, 2) <> "(欠番)" Then EnglishString = "" ReDim Preserve Item_Number(j) ReDim Preserve JapaneseItem(j) ReDim Preserve EnglishItem(j) For p = 1 To 6 If RegExp_English.Test(tmpArray(i + 1, p)) Then EnglishString = EnglishString & " " & tmpArray(i + 1, p) EnglishString = Trim(EnglishString) Else Exit For End If Next p Item_Number(j) = tmpArray(i, 1) JapaneseItem(j) = tmpArray(i, 2) EnglishItem(j) = EnglishString j = j + 1 End If On Error GoTo 0 If RegExp_Japanese.Test(tmpArray(i, 1)) And _ RegExp_English.Test(tmpArray(i + 1, 1)) Then ClassStringEN = "" ReDim Preserve JapaneseClass(k) ReDim Preserve English_Class(k) For p = 1 To 6 If RegExp_English.Test(tmpArray(i + 1, p)) Then ClassStringEN = ClassStringEN & " " & tmpArray(i + 1, p) ClassStringEN = Trim(ClassStringEN) Else Exit For End If Next p JapaneseClass(k) = tmpArray(i, 1) English_Class(k) = ClassStringEN k = k + 1 End If If RegExp_Square_Start.Test(tmpArray(i, 1)) And _ RegExp_Square_Start.Test(tmpArray(i + 1, 1)) Then StrMidClassENG = "" ReDim Preserve MidleClassJP(l) ReDim Preserve MidleClassEN(l) For p = 1 To 6 StrMidClassENG = StrMidClassENG + " " + tmpArray(i + 1, p) StrMidClassENG = Trim(StrMidClassENG) If RegExp_SquareExitEN.Test(tmpArray(i + 1, p)) Then Exit For Next p MidleClassJP(l) = tmpArray(i, 1) MidleClassEN(l) = StrMidClassENG l = l + 1 End If If RegExp_RoundStartJP.Test(tmpArray(i, 1)) And _ RegExp_RoundStartEN.Test(tmpArray(i + 1, 1)) Then StringRoundEnglish = "" ReDim Preserve SubClassJapanese(m) ReDim Preserve SubClass_English(m) For p = 1 To 6 StringRoundEnglish = StringRoundEnglish & " " & tmpArray(i + 1, p) StringRoundEnglish = Trim(StringRoundEnglish) If RegExp_RoundExitEN.Test(tmpArray(i + 1, p)) Then Exit For Next p tmpArray(i, 1) = Replace(tmpArray(i, 1), "(", "(") tmpArray(i, 1) = Replace(tmpArray(i, 1), ")", ")") SubClassJapanese(m) = tmpArray(i, 1) StringRoundEnglish = Replace(StringRoundEnglish, "(", "(") StringRoundEnglish = Replace(StringRoundEnglish, ")", ")") SubClass_English(m) = StringRoundEnglish m = m + 1 End If Next i Next h q = q + 1 End If Next tmpSht Set mySht = Worksheets("Sheet0") Set myRng = Intersect(mySht.Range("A:H"), mySht.UsedRange) myAr = myRng ReDim workArray2(UBound(myAr) - 1, 16) For i = LBound(workArray2) To UBound(workArray2) workArray2(i, 0) = myAr(i + 1, 1) workArray2(i, 1) = myAr(i + 1, 2) workArray2(i, 2) = myAr(i + 1, 3) myAr(i + 1, 4) = Replace(myAr(i + 1, 4), "(", "(") myAr(i + 1, 4) = Replace(myAr(i + 1, 4), ")", ")") workArray2(i, 6) = myAr(i + 1, 4) workArray2(i, 8) = myAr(i + 1, 5) workArray2(i, 10) = myAr(i + 1, 6) workArray2(i, 12) = myAr(i + 1, 7) workArray2(i, 14) = myAr(i + 1, 8) Next i Set mySht2 = Worksheets("Result") Set myRng2 = mySht2.UsedRange myAr2 = myRng2 For i = LBound(workArray2) To UBound(workArray2) For k = LBound(JapaneseClass) To UBound(JapaneseClass) If workArray2(i, 2) = JapaneseClass(k) Then workArray2(i, 3) = English_Class(k) End If If workArray2(i, 4) = JapaneseClass(k) Then workArray2(i, 5) = English_Class(k) End If If workArray2(i, 8) = JapaneseClass(k) Then workArray2(i, 9) = English_Class(k) End If If workArray2(i, 12) = JapaneseClass(k) Then workArray2(i, 13) = English_Class(k) End If Next k For m = LBound(SubClassJapanese) To UBound(SubClassJapanese) If workArray2(i, 6) = SubClassJapanese(m) Then workArray2(i, 7) = SubClass_English(m) End If Next m For l = UBound(MidleClassJP) To LBound(MidleClassJP) Step -1 If workArray2(i, 10) = MidleClassJP(l) Then workArray2(i, 11) = MidleClassEN(l) End If Next l For r = LBound(myAr2) To UBound(myAr2) If workArray2(i, 0) = myAr2(r, 1) Then workArray2(i, 4) = myAr2(r, 5) On Error Resume Next Select Case True Case workArray2(i, 0) >= "10001" And workArray2(i, 0) <= "10278" workArray2(i, 4) = "<魚類>" Case workArray2(i, 0) >= "10319" And workArray2(i, 0) <= "10341" workArray2(i, 4) = "<えび・かに類>" Case workArray2(i, 0) >= "10342" And workArray2(i, 0) <= "10362" workArray2(i, 4) = "<いか・たこ類>" Case workArray2(i, 0) >= "10376" And workArray2(i, 0) <= "10388" workArray2(i, 4) = "<水産練り製品>" Case workArray2(i, 0) >= "11205" And workArray2(i, 0) <= "11240" workArray2(i, 4) = "<鳥肉類>" Case workArray2(i, 0) >= "11245" And workArray2(i, 0) <= "11246" workArray2(i, 4) = "<獣肉類>" Case workArray2(i, 0) >= "11247" And workArray2(i, 0) <= "11247" workArray2(i, 4) = "<鳥肉類>" Case workArray2(i, 0) >= "13001" And workArray2(i, 0) <= "13050" workArray2(i, 4) = "<牛乳及び乳製品>" Case workArray2(i, 0) >= "15001" And workArray2(i, 0) <= "15040" workArray2(i, 4) = "<和生菓子・和半生菓子類>" Case workArray2(i, 0) >= "15041" And workArray2(i, 0) <= "15068" workArray2(i, 4) = "<和干菓子類>" Case workArray2(i, 0) >= "15069" And workArray2(i, 0) <= "15072" workArray2(i, 4) = "<菓子パン類>" Case workArray2(i, 0) >= "15073" And workArray2(i, 0) <= "15085" workArray2(i, 4) = "<ケーキ・ペストリー類>" Case workArray2(i, 0) >= "15086" And workArray2(i, 0) <= "15091" workArray2(i, 4) = "<デザート菓子類>" Case workArray2(i, 0) >= "15092" And workArray2(i, 0) <= "15100" workArray2(i, 4) = "<ビスケット類>" Case workArray2(i, 0) >= "15101" And workArray2(i, 0) <= "15104" workArray2(i, 4) = "<スナック類>" Case workArray2(i, 0) >= "15105" And workArray2(i, 0) <= "15113" workArray2(i, 4) = "<キャンデー類>" Case workArray2(i, 0) >= "15114" And workArray2(i, 0) <= "15116" workArray2(i, 4) = "<チョコレート類>" Case workArray2(i, 0) >= "15117" And workArray2(i, 0) <= "15117" workArray2(i, 4) = "<果実菓子類>" Case workArray2(i, 0) >= "15118" And workArray2(i, 0) <= "15120" workArray2(i, 4) = "<チューインガム類>" Case workArray2(i, 0) >= "16001" And workArray2(i, 0) <= "16032" workArray2(i, 4) = "<アルコール飲料類>" Case workArray2(i, 0) >= "16033" And workArray2(i, 0) <= "16044" workArray2(i, 4) = "<茶類>" Case workArray2(i, 0) >= "16045" And workArray2(i, 0) <= "16049" workArray2(i, 4) = "<コーヒー・ココア類>" Case workArray2(i, 0) >= "16050" And workArray2(i, 0) <= "16055" workArray2(i, 4) = "<その他>" Case workArray2(i, 0) >= "17001" And workArray2(i, 0) <= "17054" workArray2(i, 4) = "<調味料類>" Case workArray2(i, 0) >= "17055" And workArray2(i, 0) <= "17081" workArray2(i, 4) = "<香辛料類>" Case workArray2(i, 0) >= "17082" And workArray2(i, 0) <= "17084" workArray2(i, 4) = "<その他>" End Select On Error GoTo 0 workArray2(i, 5) = myAr2(r, 6) On Error Resume Next Select Case True Case workArray2(i, 0) >= "10001" And workArray2(i, 0) <= "10278" workArray2(i, 5) = "<FISHES>" Case workArray2(i, 0) >= "10319" And workArray2(i, 0) <= "10341" workArray2(i, 5) = "<PRAWNS,SHRIMPS AND CRABS>" Case workArray2(i, 0) >= "10342" And workArray2(i, 0) <= "10362" workArray2(i, 5) = "<CEPHALOPODS>" Case workArray2(i, 0) >= "10376" And workArray2(i, 0) <= "10388" workArray2(i, 5) = "<FISH PASTE PRODUCTS>" Case workArray2(i, 0) >= "11205" And workArray2(i, 0) <= "11240" workArray2(i, 5) = "<POULTRIES>" Case workArray2(i, 0) >= "11245" And workArray2(i, 0) <= "11246" workArray2(i, 5) = "<ANIMAL MEATS>" Case workArray2(i, 0) >= "11247" And workArray2(i, 0) <= "11247" workArray2(i, 5) = "<POULTRIES>" Case workArray2(i, 0) >= "13001" And workArray2(i, 0) <= "13050" workArray2(i, 5) = "<MILK AND DAIRY PRODUCTS>" Case workArray2(i, 0) >= "15001" And workArray2(i, 0) <= "15040" workArray2(i, 5) = "<TRADITIONAL FRESH AND SEMI-DRY CONFECTIONERIES>" Case workArray2(i, 0) >= "15041" And workArray2(i, 0) <= "15068" workArray2(i, 5) = "<TRADITIONAL DRY CONFECTIONERIES>" Case workArray2(i, 0) >= "15069" And workArray2(i, 0) <= "15072" workArray2(i, 5) = "<JAPANESE BUNS>" Case workArray2(i, 0) >= "15073" And workArray2(i, 0) <= "15085" workArray2(i, 5) = "<CAKES, BUNS AND PASTRIES>" Case workArray2(i, 0) >= "15086" And workArray2(i, 0) <= "15091" workArray2(i, 5) = "<DESSERTS>" Case workArray2(i, 0) >= "15092" And workArray2(i, 0) <= "15100" workArray2(i, 5) = "<BISCUITS>" Case workArray2(i, 0) >= "15101" And workArray2(i, 0) <= "15104" workArray2(i, 5) = "<SNACKS>" Case workArray2(i, 0) >= "15105" And workArray2(i, 0) <= "15113" workArray2(i, 5) = "<CANDIES>" Case workArray2(i, 0) >= "15114" And workArray2(i, 0) <= "15116" workArray2(i, 5) = "<CHOCOLATES>" Case workArray2(i, 0) >= "15117" And workArray2(i, 0) <= "15117" workArray2(i, 5) = "<CANDIED FRUITS>" Case workArray2(i, 0) >= "15118" And workArray2(i, 0) <= "15120" workArray2(i, 5) = "<CHEWING GUMS>" Case workArray2(i, 0) >= "16001" And workArray2(i, 0) <= "16032" workArray2(i, 5) = "<ALCOHOLIC BEVERAGES>" Case workArray2(i, 0) >= "16033" And workArray2(i, 0) <= "16044" workArray2(i, 5) = "<TEAS>" Case workArray2(i, 0) >= "16045" And workArray2(i, 0) <= "16049" workArray2(i, 5) = "COFFEES AND COCOAS>" Case workArray2(i, 0) >= "16050" And workArray2(i, 0) <= "16055" workArray2(i, 5) = "<OTHERS>" Case workArray2(i, 0) >= "17001" And workArray2(i, 0) <= "17054" workArray2(i, 5) = "<SEASONINGS>" Case workArray2(i, 0) >= "17055" And workArray2(i, 0) <= "17081" workArray2(i, 5) = "<SPICES>" Case workArray2(i, 0) >= "17082" And workArray2(i, 0) <= "17084" workArray2(i, 5) = "<OTHERS>" End Select On Error GoTo 0 If workArray2(i, 6) <> "" And _ workArray2(i, 7) = "" Then workArray2(i, 7) = myAr2(r, 8) End If If workArray2(i, 8) <> "" And _ workArray2(i, 9) = "" Then If myAr2(r, 10) = "" Then workArray2(i, 9) = myAr2(r, 15) Else workArray2(i, 9) = myAr2(r, 10) End If End If If workArray2(i, 12) <> "" And _ workArray2(i, 13) = "" Then workArray2(i, 13) = myAr2(r, 15) End If If workArray2(i, 14) <> "" Then workArray2(i, 15) = myAr2(r, 15) End If workArray2(i, 16) = myAr2(r, 11) End If Select Case True Case workArray2(i, 0) = "14004a" workArray2(i, 9) = "Safflower oil" Case workArray2(i, 0) = "14011a" workArray2(i, 9) = "Sunflower oil" Case workArray2(i, 0) = "14011b" workArray2(i, 9) = "Sunflower oil" End Select Next r Next i ReDim workArray3(UBound(workArray2), UBound(workArray2, 2)) For i = LBound(workArray3) To UBound(workArray3) workArray3(i, 0) = workArray2(i, 0) workArray3(i, 1) = workArray2(i, 1) workArray3(i, 2) = workArray2(i, 2) workArray3(i, 3) = workArray2(i, 4) workArray3(i, 4) = workArray2(i, 6) workArray3(i, 5) = workArray2(i, 8) workArray3(i, 6) = workArray2(i, 10) workArray3(i, 7) = workArray2(i, 12) workArray3(i, 8) = workArray2(i, 14) workArray3(i, 9) = workArray2(i, 3) workArray3(i, 10) = workArray2(i, 5) workArray3(i, 11) = workArray2(i, 7) workArray3(i, 12) = workArray2(i, 16) workArray3(i, 13) = workArray2(i, 9) workArray3(i, 14) = workArray2(i, 11) workArray3(i, 15) = workArray2(i, 13) workArray3(i, 16) = workArray2(i, 15) Next i Set mySht = Worksheets.Add With mySht .Name = "M_CATEGORY" .Range("A1").Value = "ItemNumber" .Range("B1").Value = "FoodGroupNumber" .Range("C1").Value = "FoodGroupJP" .Range("D1").Value = "SubGroupJP" .Range("E1").Value = "SubCategoryJP" .Range("F1").Value = "MajorCategoryJP" .Range("G1").Value = "MediumCategoryJP" .Range("H1").Value = "MinorCategoryJP" .Range("I1").Value = "DetailsJP" .Range("J1").Value = "FoodGroupEN" .Range("K1").Value = "SubGroupEN" .Range("L1").Value = "SubCategoryEN" .Range("M1").Value = "AcademicName" .Range("N1").Value = "MajorCategoryEN" .Range("O1").Value = "MediumCategoryEN" .Range("P1").Value = "MinorCategoryEN" .Range("Q1").Value = "DetailsEN" .Range("A2:Q1892") = workArray3 End With Set tmpSht = Nothing Set tmpRng = Nothing Set tmpArray = Nothing Set workArray = Nothing Set RegExp_Japanese = Nothing Set RegExp_English = Nothing Set RegExp_ItemNum = Nothing Set RegExp_Square_Start = Nothing Set RegExp_SquareExitEN = Nothing Set RegExp_RoundStartJP = Nothing Set RegExp_RoundStartEN = Nothing Set RegExp_RoundExitEN = Nothing Erase Item_Number() Erase JapaneseItem() Erase EnglishItem() Erase JapaneseClass() Erase English_Class() Erase ItemNumArray() Erase ItemENGArray() Erase ClassArrayJP() Erase ClassArrayEN() Erase SubClassJapanese() Erase SubClass_English() Erase MidleClassJP() Erase MidleClassEN() Erase SubClass_JPN() Erase SubClass_ENG() Erase workArray2() Erase workArray3() Set mySht = Nothing Set myRng = Nothing Set myAr = Nothing Set mySht2 = Nothing Set myRng2 = Nothing Set myAr2 = Nothing End Sub Function NoCancelArray(ByRef Sh As Variant) As Variant Dim mySht As Variant Dim myRng As Range Dim tmpAr As Variant Dim i As Long Dim j As Long Dim RegExpCancel As Object Dim RegExp_Exit As Object Const StrCancel As String = "^(1\)|residues)$" Dim CancelItem() As String Dim CancelRow1() As String Dim CancelRow2() As String Dim myCancelAr() As String Dim Cancel_Array() As String Set RegExpCancel = CreateObject("VBScript.RegExp") With RegExpCancel .Pattern = StrCancel .IgnoreCase = True .Global = True End With tmpAr = Sh j = 0 For i = LBound(tmpAr) To UBound(tmpAr) If RegExpCancel.Test(tmpAr(i, 1)) Then ReDim Preserve CancelItem(j) ReDim Preserve CancelRow1(i) CancelItem(j) = tmpAr(i, 1) CancelRow1(j) = i j = j + 1 End If Next i ReDim myCancelAr(UBound(CancelItem), 1) For j = LBound(myCancelAr) To UBound(myCancelAr) myCancelAr(j, 0) = CancelItem(j) myCancelAr(j, 1) = CancelRow1(j) Next j ReDim Preserve myCancelAr(UBound(myCancelAr), 2) j = 0 For i = LBound(myCancelAr) To UBound(myCancelAr) - 1 If myCancelAr(i, 0) = "1)" Then If UBound(myCancelAr) >= 2 Then If myCancelAr(i + 2, 0) = "residues" Then myCancelAr(i, 2) = myCancelAr(i + 2, 1) Else myCancelAr(i, 2) = myCancelAr(i + 1, 1) End If Else myCancelAr(i, 2) = myCancelAr(i + 1, 1) End If j = j + 1 End If Next i Erase CancelRow1 j = 0 ReDim CancelRow1(j) ReDim CancelRow2(j) CancelRow1(j) = myCancelAr(j, 1) CancelRow2(j) = myCancelAr(j, 2) For i = LBound(myCancelAr) + 1 To UBound(myCancelAr) If myCancelAr(i, 0) = "1)" And _ myCancelAr(i - 1, 0) <> "1)" Then j = j + 1 ReDim Preserve CancelRow1(j) ReDim Preserve CancelRow2(j) CancelRow1(j) = myCancelAr(i, 1) CancelRow2(j) = myCancelAr(i, 2) End If Next i ReDim Cancel_Array(UBound(CancelRow1), 1) j = 0 For j = LBound(Cancel_Array) To UBound(Cancel_Array) Cancel_Array(j, 0) = CancelRow1(j) Cancel_Array(j, 1) = CancelRow2(j) Next j j = 0 Cancel_Array(j, 0) = 1 Cancel_Array(j, 1) = CancelRow1(j) For j = LBound(Cancel_Array) + 1 To UBound(Cancel_Array) Cancel_Array(j, 0) = CancelRow2(j - 1) + 1 Cancel_Array(j, 1) = CancelRow1(j) - 1 Next j NoCancelArray = Cancel_Array End Function |
I have counted number of modified cells. It was more than 2400. I could not write complete code without manual processing. It is the responsibility of the Ministry of Education, Culture, Sports, Science & Technology in Japan (MEXT).
References:
CSV file of the ‘Standard Tables of Food Composition in Japan 2010′
Classify the Item_Number of the ‘Standard Tables of Food Composition in Japan 2010′, Part 1
日本食品標準成分表2010』のPDFを.txtファイルに変換するの記事を参考に,ファイル名 “1299012_1.pdf” から “1299012_18.pdf” までのファイルをダウンロードします.PDF1ファイルの全テキストをコピーしてワークシート1枚に貼り付けのオプションでペーストします.テキストファイルウィザード1/3では元のデータ形式で『カンマやタブなどの区切り文字によってフィールドごとに区切られたデータ』を選択します.テキストファイルウィザード2/3では特に変更なく次へ進みます.テキストファイルウィザード3/3では最初のカラムの列のデータ形式のみ『文字列』に変更して完了をクリックします.この作業をPDFファイル分繰り返します.主にA列に対して若干の修正を施します.さらに Webテク実験室 からダウンロードしたブック “成分表2010.xls” のワークシートをコピーし,シート名を “Sheet0” に変更します.このEXCELのブックに “Category.xlsm” と名前を付けて保存します.
日本食品標準成分表2010の食品番号をカテゴリー分類する その1で作成した ”Sample.xlsm” ブックから ”Result” シートを “Category.xlsm” ブックに移動又はコピーします.AltキーとF11キーを押下してVBEを起動します.標準モジュールを挿入し,下記コードを貼り付けて実行して下さい.結果として “M_CATEGORY” という名のシートが生成します.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 |
Option Explicit Sub Select_Class() Dim tmpSht As Worksheet Dim tmpRng As Range Dim tmpArray As Variant Dim workArray As Variant Dim h As Long Dim i As Long Dim j As Long Dim k As Long Dim l As Long Dim m As Long Dim n As Long Dim p As Long Dim q As Long Dim r As Long Dim RegExp_Japanese As Object Dim RegExp_English As Object Dim RegExp_ItemNum As Object Const PtnJPN As String = "[^A-Za-z0-9'\.\-\*]{2,}" Const PtnENG As String = "^[A-Za-z0-9'\,\.\-\%]+$" Const PtnItemNum As String = "^[0-9]{5}$" Dim Item_Number() As String Dim JapaneseItem() As String Dim EnglishItem() As String Dim EnglishString As String Dim JapaneseClass() As String Dim English_Class() As String Dim ClassStringEN As String Dim ItemNumArray() As String Dim ItemENGArray() As String Dim ClassArrayJP() As String Dim ClassArrayEN() As String Dim RegExp_AngleBracket As Object Dim RegExp_RoundStartJP As Object Dim RegExp_RoundStartEN As Object Dim RegExp_RoundE |