SendKeys to insert new row

TheJay

Active Member
Joined
Nov 12, 2014
Messages
364
Office Version
  1. 2019
Platform
  1. Windows
Hello there,

I want to trigger inserting a new row using Ctrl + I by assigning that combination as a macro bind.

Can someone please tell me what's wrong with my code so that it works?

VBA Code:
Sub InsertRow()
Application.SendKeys "(+"" "")"    'Shift+Spacebar to select the row. Space (" ")
Application.SendKeys "(%ir)"       'Alt+I+R to add a new row above.
End Sub

'For SHIFT use +
'For CTRL use ^
'For ALT use %

Thanks.
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
If anyone else is interested, this is the code that works (and removes focus from highlighting the whole of the newly inserted row).

VBA Code:
Sub InsertRow()
Application.SendKeys "+( )%(ir){UP}{DOWN}"
End Sub

It doesn't appear that it is possible to bind to Ctrl + I though.
 
Upvote 0
Sendkeys is unstable. (It doesn't work consistently.) Here's a stable alternative that I would advise you to consider (which I rarely do).

Look at: Application.OnKey method (Excel)

For your specific case, you would need to have a sub which assigns the key press, but another sub which UNASSIGNS it. Because it will (if I recall correctly) carry to other workbooks if they are opened at the same time as this one. If not, then that's even better. (But I recall something like that.)

Put these subs in a standard module:
VBA Code:
Sub Disable_Key()
Application.OnKey "^i", "Do_Nothing"
End Sub
Sub Do_Nothing()
'Do nothing
End Sub
Sub Insert_Row()
MsgBox "Insert Row Sub is called now", vbInformation, "Successful"
End Sub

And perhaps put this sub in the Sheet's sheet code (right click on the sheet tab and select View Code). It's got to go in an Event sub somewhere. Either here or where I suggest next, perhaps.
VBA Code:
Private Sub Worksheet_Activate()
Application.OnKey "^i", "Insert_Row"
End Sub

Or if you want to apply this to the entire Workbook at the start, double click on this and put the following code in there:
Workbook module.PNG

VBA Code:
Private Sub Workbook_Open()
Application.OnKey "^i", "Insert_Row"
End Sub

And if you want the key to be unassigned when you close this Workbook (so that it's only assigned in this workbook and not others), in the same module you can put the unassign code from above instead of in a standard code module:
VBA Code:
Private Sub Workbook_Open()
Application.OnKey "^i", "Insert_Row"
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "^i", "Do_Nothing"
End Sub
 
Last edited:
Upvote 0
Thank you very much for your reply. It doesn't work quite the same way, does it?

I tried replacing existing VBA and it's coming up with errors. For example:

Sheet3 Code
VBA Code:
Sub Worksheet_Activate()
Application.OnKey "^i", "InsertRow"
End Sub

Sub Worksheet_Deactivate()
Application.OnKey "^i", ""     'Empty double quotes remove binding?
End Sub

InsertRow Module
VBA Code:
Sub InsertRow()
Application.OnKey "+( )%(ir){UP}{DOWN}"
End Sub

It doesn't like that key combination, which worked with SendKeys.
 
Upvote 0
Application.OnKey "^i", ""
That's incorrect. You literally need to have it call a sub that does nothing. (Just like I had it before.) You have to assign it to a sub. If the sub has no code in it, it will disable the assigned keypress (in effect).
 
Upvote 0
Okay, I'll keep that in mind. It's still reporting a problem with the keypresses changing from sendkeys to onkey?
 
Upvote 0
I don't know how they work together. So you will have to choose one or the other. But I already mentioned why you shouldn't use SendKeys. (And it's not just my opinion. A lot of people have said the same thing.)
 
Upvote 0
I'm happy to use OnKey, as long as it can input the keys as mentioned previously.
 
Upvote 0
According to this page, it doesn't need to refer to a macro?


VBA Code:
Call Application.OnKey(key) - restore hotkey assignment to normal 
Call Application.OnKey(key, "") - disables hotkey

Is that not your interpretation?
 
Upvote 0
Further reading would suggest it's even simpler:


The following code demonstrates how to reassign, disable, and restore a built-in key assignment:

VBA Code:
Sub TestOnKey(  )
    ' Reassign Ctrl+C
   Application.OnKey "^c", "CopyMsg"
   ' Disable Ctrl+C
   'Application.OnKey "^c", ""
   ' Restore Ctrl+C
   ' Application.OnKey "^c"
End Sub
 
Sub CopyMsg(  )
    MsgBox "You can't copy right now."
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,499
Messages
6,131,010
Members
449,613
Latest member
MedDash99

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