VBA SendKeys (F2) and NumLock Issue

Joshua_Q

New Member
Joined
Dec 24, 2015
Messages
32
Hello Everyone!

I've got a set of merged cells that users will paste into. To bypass the error when pasting into merged cells, I wrote the following code:

Code:
Private Sub Worksheet_Selectionchange(ByVal Target As Range)    If Not Intersect(Target, Range("B25:B50")) Is Nothing Then
        Application.SendKeys keys:="{F2}"
    End If
End Sub

This code makes it such that when you click a cell in the specified range, it will send the F2 key-press (similar to double clicking a cell), thereby bypassing the problem with pasting into merged cells.

The problem is, as I'm sure many are aware, SendKeys is bugged. It will toggle Caps Lock, NumLock, Scroll Lock on/off when activated. I've been looking for a solution to this for some time, but have been unable to find anything that works. I've tried using some of the code found online that finds the keystate of numlock and if off, turn it on. However, I cannot seem to get this code to work (I am somewhat new to VBA, so it's possible I'm doing something wrong).

So, I'm looking for a way to resolve this. I either need some code that works similar to sending the F2 keypress without actually using SendKeys. Or I need to find a functional method of finding the keystate of numlock and turning it on when it inevitably gets turned off (or possibly reset it to it's initial state (on or off) before the SendKeys macro activates).


Interesting side note. I did some experimentation on this. If I open my workbook with number lock turned on and then click in the given range B25:B50, number lock toggles off. If I then activate macro:

Code:
Sub Button99_Click()
SendKeys "{NUMLOCK}"
End Sub

while number lock is turned off, it then turns number lock back on. Following that, subsequent activations of the macro that sends the F2 keypress do not toggle number lock. I've tried some experimentation with duplicating this with a macro that activates when the workbook is first opened, but I've yet to see success here.

Anyhow, any and all help on this matter is appreciated!
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
What is the problem with pasting into merged cells you are trying to handle?
 
Upvote 0
The individuals using my form are not particularly Excel savvy. They want to be able to click a cell and ctrl + v to paste into it. If you try to do this in a merged cell, you get an error stating "Data on the Clipboard is not the same size and shape as the selected area. Do you want to paste the data anyway?" and clicking OK then returns a second error stating "Cannot change part of a merged cell."

To bypass this, if you double click a merged cell and then paste, you do not get an error. VBA (to my understanding) does not have code to mimic a double click. My solution was to use F2, which has a similar functionality. However, as stated above, SendKeys is bugged so I'm hoping for other solutions or a potential fix to the SendKeys bug.
 
Upvote 0
Send F2 twice with DoEvents as follows :
Code:
Private Sub Worksheet_Selectionchange(ByVal Target As Range) 
    If Not Intersect(Target, Range("B25:B50")) Is Nothing Then
        Application.SendKeys keys:="{F2}"
        DoEvents
        Application.SendKeys keys:="{F2}"
    End If
End Sub
 
Last edited:
Upvote 0
Had the same problem, this works fine for me (don't know how robust it is) :)
insert after sendkeys statement:
Code:
Application.Numlock = True
 
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,874
Members
452,363
Latest member
merico17

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top