macros for trigering events on focus loose

nattynids

New Member
Joined
Mar 1, 2011
Messages
11
I want to know, can we write a macro that does some acton when we loses our focus from one cell.
To be precise, i have column A which has some values. Now the moment the focus is shifted from any row of column value A, the value in that row of column A should get cpoied to same row of column B.
is it possible to do so or i have to write a macro that cpies the entire content of column A at once?
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
If you mean the moment you put a figure in col a and step down you also want that that number/letter in col b then you could just type

=A1 in cell B1 and drag the formula down
 
Upvote 0
Not sure I understand completely what you want to achieve but the following code may help:

Code:
Private colA As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Set colA = Target
    Else
        Cells(colA.Row, "B") = colA.Value
    End If
End Sub
 
Upvote 0
Not sure I understand completely what you want to achieve but the following code may help:

Code:
Private colA As Range
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Set colA = Target
    Else
        Cells(colA.Row, "B") = colA.Value
    End If
End Sub


hey, thanks for a quick reply.
But now i have one more query before running this.
i have copied this in Macro1 & renamed this macro.
But now it does not show me Macro1 in the list of macros to be run.
Similar thing was happening when i tried some other code from net.
i am not abe to undrstand what m i missing?
 
Upvote 0
This sub won't be visible under macros. It's set to Private. You will have to paste it like this:

1. Right-click the sheet for which you want this to apply
2. Left-click "View Code"
3. Paste the code in the window that appears
4. Now when you go back to the sheet and change selections, you will get your desired result. There's no need for you to run the macro manually.
 
Upvote 0
Under no circumstances should you rename this macro. :) Keep it as:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 
Upvote 0
Under no circumstances should you rename this macro. :) Keep it as:

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)


i was in some training so couldn't try this earlier.
It works great thanks. Te only problem is that it copies data from col A to col B only in the scenerio when we move our cursor from col A to col B. If i move my cursor from A1 to A2, it does not copies anything.
 
Upvote 0
Replace the earlier code with this:

Code:
Private colA As Range
Private prevA As Boolean
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    On Error Resume Next
    If prevA = True Then Cells(colA.Row, "B") = colA.Value
    If Not Intersect(Target, Range("A:A")) Is Nothing Then
        Set colA = Target
        prevA = True
    Else
        prevA = False
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,597
Messages
6,179,808
Members
452,944
Latest member
2558216095

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