Right Function using VBA

omairhe

Well-known Member
Joined
Mar 26, 2009
Messages
2,040
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone,

In cell I3 of my sheet named "HSheet" when I press the letter "a" from my keyboard, the vba runs in the following fashion..

VBA Code:
Private Sub Workbook_Activate()
Application.OnKey "a", "onA"
End Sub

VBA Code:
Sub onA()
Sheets("HSheet").Range("I3").Value = Sheets("HSheet").Range("I3").Value & "a"
End Sub

I would like to keep the text in I3 with only one character from the right and then join it with the letter "a" at the end of it, instead of joining the entire text in cell I3 with an a.

Example requirment:

I3 = Cat
vba code = t & "a" = ta

I3 = Tennis
vba code = s & "a" = sa

I3 = p
vba code = p & "a" = pa

I3 = "" (blank)
vba code = "" & "a" = a

Thanks and will appreciate.
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
VBA Code:
Sheets("HSheet").Range("I3").Value = Right(Sheets("HSheet").Range("I3").Value, 1) & "a"

But on second run onwards, it'll probably just keep taking "a" from cell I3
 
Upvote 0
Thanks .
I have other alphabet keys other than "a" that should run the same way.
example Application.Onkey "b", "OnB"

Hoping to not get into problems.
 
Upvote 0

Forum statistics

Threads
1,215,591
Messages
6,125,711
Members
449,252
Latest member
cryss1988

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