Reading the keyboard in VBA

D

dwinmac

Can someone show me how to code the conditional statement shown in the
comments below i.e. how to put the "x" in the cell only if the SHIFT ( or
ctrl or alt)key is selected.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
RowPos = Target.Row
ColPos = 3
' If Shift Key selected then
' Put an "x" in cell row = RowPos and Column = ColPos
ThisWorkbook.ActiveSheet.Cells(RowPos, ColPos) = "x"
'End If
End Sub
 
S

ShaneDevenshire

Hi,

Is this code you got from somewhere or a guess on how it might look? I
think it would be better is you explained exactly what you want to do and to
what range because if you put an X into a cell everytime you move the cursor
with the SHIFT ALT or CTRL key down you may get some unpleasent supprises.
 
P

Peter T

For the reasons Shane mentioned I doubt you individual Shift, Ctrl or Alt
down will be of use for you. OTH, Ctrl+Shift might serve your needs

Private Declare Function GetAsyncKeyState Lib "user32" ( _
ByVal vKey As Long) As Integer

Function ShiftCtrl() As Boolean
If GetAsyncKeyState(vbKeyControl) Then
ShiftCtrl = GetAsyncKeyState(vbKeyShift)
End If

End Function

' the above could go in a normal module and called from other object modules

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim RowPos As Long, ColPos As Long

If Target.Count = 1 Then
If ShiftCtrl Then
RowPos = Target.Row
ColPos = 3
Cells(RowPos, ColPos) = "x"
End If
End If
End Sub
ThisWorkbook.ActiveSheet.Cells(RowPos, ColPos) = "x"

No need to qualify the sheet in the sheet module unless you want to refer to
some other sheet.

If you need to know more than simply Ctrl+Shift I'm sure you can adapt the
above for your needs.

Regards,
Peter T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top