Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any GotFocus event for Excel Cell?

Tags:

excel

vba

Whenever I get focus into one particular cell, and if that cell contains a value which is less than 2, then the content in that cell should be multiplied by 60.

BTW, I can get focus into one particular cell by using navigation keys, or by clicking on one partiuclar cell or anything.

For example, I got focus into one particular cell which has a value 1.5. Then, my VBA program should automatically convert that cell value with 90 as 60*1.5=90.

I don't know much about programming in Excel. I can't open most of the sites due to company security policies.

Can anyone please help me on this!

like image 941
Ashok kumar Avatar asked Jan 03 '23 08:01

Ashok kumar


1 Answers

Say the cell in question is cell B9. Put the following event macro in the worksheet code area:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim B9 As Range
    Set B9 = Range("B9")
    If Intersect(Target, B9) Is Nothing Then
    Else
        If B9 < 2 Then B9 = 60 * B9
    End If
End Sub

Because it is worksheet code, it is very easy to install and automatic to use:

  1. right-click the tab name near the bottom of the Excel window
  2. select View Code - this brings up a VBE window
  3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it. If you are using a version of Excel later then 2003, you must save the file as .xlsm rather than .xlsx

To remove the macro:

  1. bring up the VBE windows as above
  2. clear the code out
  3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

Macros must be enabled for this to work!

like image 132
Gary's Student Avatar answered Feb 28 '23 09:02

Gary's Student