-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.bas
More file actions
150 lines (111 loc) · 6.15 KB
/
Code.bas
File metadata and controls
150 lines (111 loc) · 6.15 KB
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
Attribute VB_Name = "Code"
'+----------------------------------------------------------------------------------------------------------------+
'| Code adapted by: |
'| Ferdinand Feoli |
'| |
'| Adapted from: |
'| https://stackoverflow.com/questions/54063168/lose-focus-from-combobox-with-control-arrowkeys-excel/ |
'| |
'| My profile: |
'| www.linkedin.com/in/ferdinandfeoli |
'| |
'| Description: |
'| Just a VBA coding test. |
'| |
'| Places a combobox in selected cell (in this case range of action are yellow cells only), |
'| you can move through cells/comboboxes using CTRL+{ARROW} keys. |
'| |
'| Use this code as you want. |
'| |
'+----------------------------------------------------------------------------------------------------------------+
'declare virtual key event listener ---------------------------------------
#If VBA7 Then
Private Declare PtrSafe Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
#Else
Private Declare Function GetAsyncKeyState Lib "user32" _
(ByVal vKey As Long) As Integer
#End If
'--------------------------------------------------------------------------
'declare virtual key constants -------------------------------------------
Private SWITCH As Boolean
'The variable SWITCH is an auxiliar wich helps to break iterations.
Private Const VK_CTRL = &H11 'CONTROL key
Private Const VK_LEFT = &H25 'LEFT ARROW key
Private Const VK_UP = &H26 'UP ARROW key
Private Const VK_RIGHT = &H27 'RIGHT ARROW key
Private Const VK_DOWN = &H28 'DOWN ARROW key
'--------------------------------------------------------------------------
'For more information about virtual key interactions with VBA visit the following link:
' https://wellsr.com/vba/2017/excel/GetAsyncKeyState-vba-to-wait-until-a-key-is-pressed/
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Variable declaration ---------
Dim xCombox As OLEObject
Dim xStr As String
Dim xWs As Worksheet
Dim xArr
'------------------------------
'Set objects -----------------------------
Set xWs = Application.ActiveSheet
On Error Resume Next
Set xCombox = xWs.OLEObjects("TempCombo")
'-----------------------------------------
' Set combobox properties ----------------
With xCombox
.ListFillRange = ""
.LinkedCell = ""
.Visible = False
End With
'-----------------------------------------
If Target.Validation.Type = 3 Then
' If the cell conians data validation list,
'place the combobox object on it, an load
'in there the items.
Target.Validation.InCellDropdown = False
Cancel = True
xStr = Target.Validation.Formula1
xStr = RIGHT(xStr, Len(xStr) - 1)
If xStr = "" Then Exit Sub
With xCombox
.Visible = True
If Not SWITCH Then
.LEFT = Target.LEFT
.Top = Target.Top
End If
.Width = Target.Width + 5
.Height = Target.Height + 5
.ListFillRange = xStr
If .ListFillRange = "" Then
xArr = Split(xStr, ",")
Me.TempCombo.List = xArr
End If
.LinkedCell = Target.Address
End With
xCombox.Activate
End If
End Sub
Private Sub TempCombo_KeyDown(ByVal keycode As MSForms.ReturnInteger, ByVal Shift As Integer)
' When a key is pressed while a combobox is active, read the key pressed by the user,
' If it's the CTRL key, proceed to read the second key pressed (ARROW).
' Execute a different command depending of wich key is pressed.
If Not SWITCH Then
Do While GetAsyncKeyState(VK_CTRL) <> 0
If GetAsyncKeyState(VK_LEFT) Then
ActiveSheet.Range(Application.ActiveCell.Cells(1, 0).Address).Activate
Exit Do
ElseIf GetAsyncKeyState(VK_RIGHT) Then
ActiveSheet.Range(Application.ActiveCell.Cells(1, 2).Address).Activate
Exit Do
ElseIf GetAsyncKeyState(VK_UP) Then
ActiveSheet.Range(Application.ActiveCell.Cells(0, 1).Address).Activate
Exit Do
ElseIf GetAsyncKeyState(VK_DOWN) Then
ActiveSheet.Range(Application.ActiveCell.Cells(2, 1).Address).Activate
Exit Do
End If
Loop
Else
SWITCH = False
End If
If keycode = 17 Then SWITCH = True
End Sub