Macro to paste a string as plain text into the active cell, then accomodate more text from keyboard

Ryan_N

New Member
Joined
Mar 10, 2022
Messages
1
Office Version
  1. 2019
Platform
  1. Windows
Pretty simple :)

I wish to press, say, F1 (Windows OS) and have something like "XZ" print in the active cell as plain text, but in such a way that I can then enter "123" on the keypad to result in XZ123 before I hope out of the cell. That's it! The only caveat is that I wish the automation to exist only on my computer, because the .xls is a shared file used by others.

Huge thanks in advance for any guidance! I realize this is crazy-simple but I honestly can't figure out how to do it. Cheers!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Hi, This might work for you.
VBA Code:
Sub PasteTextWithEsc()
    ' In order to run from View Macro or
    ' Assign to a button, you will need to call Private Subs from a Sub
    '
    Call assignESCkey
End Sub

Private Sub assignESCkey()
    'When ESC is pressed, call macro dataToPaste.
    'For other keys, visit https://docs.microsoft.com/en-us/office/vba/api/excel.application.onkey
    'But I don't recall ever using ESC in Excel, I normally click the X.
    '
    Application.OnKey "{ESC}", "dataToPaste"
End Sub

Private Sub dataToPaste()
    'The below controls what is placed in the ActiveCell.
    'Hardcode it by replacing XZ or use cell A2 from the right workbook.
    '
    Dim wbs As Workbook: Set wbs = Workbooks("forPasteTextWithESC.xlsm")
    Dim mws As Worksheet: Set mws = wbs.Worksheets("Sheet1")
    
    If Range("A2").Value = "" Then
        ActiveCell.Value = "XZ"
    Else
       ActiveCell.Value = mws.Range("A2").Value
    End If
        
    Application.SendKeys "{F2}" 'Edit the cell
End Sub

Sub stopPasting()
    'In case you want to stop it.
    Application.OnKey "{ESC}", Null
End Sub
 

Attachments

  • forPasteTextWithESC.jpg
    forPasteTextWithESC.jpg
    95.9 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,214,984
Messages
6,122,601
Members
449,089
Latest member
Motoracer88

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