#3、cjです。
> その、すこしが難しい
よくご存じのようで、安心しました。
一応、仕様等私好みで整理してみました。
#好きでやってるので、気にしないで下さいね。あのままじゃ余りにもアレなので。
インデックスをセル値に指定するだけで事足りる話だったなら、それはそれでいいので。
' ' ===ThisWorkbookモジュール===
Option Explicit
' ' ============================ 7767828we2
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Sheets("Sheet1").FrmTBox.Visible = False '◆ Sheet名を指定
End Sub
' ' ============================
' ' ======Sheet モジュール======
Option Explicit
Private aList ' List配列
Private nUB As Long ' List配列のサイズ
Private n As Long ' List配列用インデックス
Private Const S_REF = "D5:D10" '◆処理対象範囲を参照文字列指定
' ' ============================ 7767828shEv
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range(S_REF)) Is Nothing Or Target.Count > 1 Then
If FrmTBox.Visible Then FrmTBox.Visible = False
Else
Call ActTBox(Target)
End If
End Sub
' ' ----------------------------
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range(S_REF)) Is Nothing Then Exit Sub
Cancel = True
Call ActTBox(Target)
End Sub
' ' ============================ 7767828tbEv
Private Sub FrmTBox_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
With FrmTBox
Select Case KeyCode
Case 27: Application.OnTime Now(), Me.Name & ".HideTBox" ' ▼"ESC"
Case 37: ActiveCell.Offset(, -1).Select ' ▼"←"
Case 38: ActiveCell(0).Select ' ▼"↑"
Case 39: ActiveCell(1, 2).Select ' ▼"→"
Case 40: ActiveCell(2).Select ' ▼"↓"
End Select
End With
End Sub
' ' ----------------------------
Private Sub FrmTBox_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
On Error GoTo CrArr_
nUB = UBound(aList)
On Error GoTo 0
With FrmTBox
Select Case KeyCode
Case 48 To 57, 96 To 105: Call ValVal(n & KeyCode Mod 48) ' ▲"0" To "9", "0" To "9"(NumLockedTenKey)
Case 107, 187: Call ValVal(n + 1) ' ▲Shift+"+", "+"(TenKey)
Case 109, 189: Call ValVal(n - 1) ' ▲"-", "-"(TenKey)
Case 8: Call ValVal(n \ 10) ' ▲"BACKSPACE"
Case 46: Call ValVal(0) ' ▲"DEL"
Case 9, 13 ' ▲"TAB", "ENTER"
If .Value <> "" Then ActiveCell.Value = .Value
If Shift Then ' "SHIFT"
ActiveCell(0).Select
Else
ActiveCell(2).Select
End If
End Select
End With
DoEvents
Exit Sub
CrArr_:
Call SetList
Resume
End Sub
' ' ============================ 7767828subR
Sub SetList() ' ◆ List内容を一次元配列で設定。方法、内容は運用に合わせて適宜。Null値不可。
aList = VBA.Array(Empty, "炭俵灰之介", "Alfred", "Benjamin", "Charlie", "David", "Edward" _
, "Frank", "George", "Harry", "Isaac", "Jack", Empty, "King", "London" _
, "Mary", "Nellie", "Oliver", "Peter", "Queen", "Robert", "Samuel", "Tommy")
End Sub
' ' ----------------------------
Sub ActTBox(Target As Range) ' FrmTBox 初期化 位置設定 表示 等
n = 0
With FrmTBox
.Object.Value = Empty
.Object.IMEMode = fmIMEModeOff
.Top = Target(1).Top
.Left = Target(1).Left
.Activate
If Not .Visible Then .Visible = True
End With
End Sub
' ' ----------------------------
Private Sub HideTBox() ' ESC 処理
DoEvents: DoEvents
FrmTBox.Visible = False
ActiveCell.Activate
End Sub
' ' ----------------------------
Sub ValVal(nn As Long) ' List用インデックス n と FrmTBox.Value の管理
If nn < 0 Then nn = 0
n = nn
With FrmTBox
If n > nUB Then ' 割り当ての無いインデックスは数値のまま
.Value = n
ElseIf n = 0 Then
.Value = Empty
ElseIf aList(n) = "" Then
.Value = n
Else
.Value = aList(n)
End If
End With
End Sub
' ' ============================
' ' ============================ 7767828prep
Private Sub Prep7767828() ' TextBox 初期設定 利用開始時に一度だけ実行
With OLEObjects.Add(ClassType:="Forms.TextBox.1")
With .Object
.BackColor = &HC0FFFF
.SpecialEffect = fmSpecialEffectFlat
End With
.Height = Range("D5").Height '◆
.Width = Range("D5").Width '◆
.Name = "FrmTBox"
.PrintObject = False
.Visible = False
End With
End Sub
' ' ===========================
お礼
回答 ありがとうございます お礼が遅くなり、失礼しました このマクロは難しすぎます。 でも、最初の行から順に理解していけば、いつかは・・・・