' [352.xls] ' [Module1] のコード '★★☆ テキストボックスの入力文字数を制限する ☆★★ Option Explicit Sub start() UserForm1.Show End Sub ' [UserForm1] のコード Option Explicit Private Sub TextBox1_Change() Dim strg As String Dim ok_strg As String Dim byt_count As Integer Dim sitei_count As Integer Dim I As Integer '入力文字の制限数を取得 sitei_count = Val(TextBox2.Text) '変数の初期化 byt_count = 0 ok_strg = "" 'TextLength プロパティーはTextBoxに入力されている文字数を取得 '一文字ずつバイト数を調べ加算していく For I = 1 To TextBox1.TextLength strg = Mid(TextBox1.Text, I, 1) 'Asc 関数を使って全角文字か半角文字か判断 If Asc(strg) < 256 And Asc(strg) >= 0 Then byt_count = byt_count + 1 Else byt_count = byt_count + 2 End If '加算されたバイト数が指定文字数を越えていたら For文から抜ける If byt_count > sitei_count Then Exit For '文字数が指定文字数に満たないときは文字をたす ok_strg = ok_strg & strg Next '決定した文字数に書き換える TextBox1.Text = ok_strg End Sub Private Sub UserForm_Initialize() 'フォーカスを持ったときに日本語IMEの状態をON、ひらがなにします TextBox1.IMEMode = fmIMEModeHiragana 'フォーカスを持ったときに日本語IMEの状態をOFF、半角英語にします TextBox2.IMEMode = fmIMEModeOff End Sub