Solved

i need to detect when the value of the active cell is change

Posted on 2010-09-01
13
317 Views
Last Modified: 2012-05-10
Best regards to all

I need to detect when the active cell on the worksheet is "A2" and after that I need to catch the value of the active cell if it is diferent to empty

0
Comment
Question by:altariamx2003
  • 5
  • 5
  • 2
  • +1
13 Comments
 

Author Comment

by:altariamx2003
ID: 33582166
sorry

the problem is that i need to catch the value (for example in another cell) at the same time that the activecell is editing
0
 
LVL 50

Expert Comment

by:teylyn
ID: 33582204
Hello altariamx,

you can do that with a Worksheet Change event macro.

Right-click the sheet tab and select "View Code". Copy the macro below into the code window and add the steps you want the macro to do.

cheers, teylyn


Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, [A2]) Is Nothing Then
    If Target = "" Then
        ' do something
    Else
        ' do something else
End If

End Sub

Open in new window

0
 

Author Comment

by:altariamx2003
ID: 33582238
thks teylyn for your example but is not what Im looking for

your code show the value after the active cell change to another one

I need to catch the value of the active cell when you are writing on it



0
 
LVL 16

Expert Comment

by:Jerry Paladino
ID: 33582335
Can you provide an example with the original cells and values and then - what they should look like after the change?
0
 
LVL 50

Expert Comment

by:teylyn
ID: 33582396
You cannot duplicate a cell content in another cell while you are still editing the cell. You need to confirm the data entry in the cell with one of the known methods (Enter key, tab key, etc) and only then is the cell content available for manipulation via VBA or formulas.
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 33582489
While I'm not quite 100% on what you what

This code
- checks to see if A2 was changed
- if A2 is now blank then the value is restored to the prior non blank value (using a static variable)
- if A2 is not blank that the static variable is updated to the new value

Cheers

Dave

Private Sub Worksheet_Change(ByVal Target As Range)
    Static str1 As String
    If Intersect(Target, [a2]) Is Nothing Then Exit Sub
    Application.EnableEvents = False
    If Target.Value = vbNullString Then
        [a2] = str1
    Else
        str1 = [a2]
    End If
    Application.EnableEvents = True
End Sub

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:altariamx2003
ID: 33582703
mmmm I cannot catch the value????

Ok one last question it is possible to detect when the active cell is editing???

0
 
LVL 50

Expert Comment

by:teylyn
ID: 33582721
Well, kind of. As far as I know you can't run VBA code while Excel is in edit mode, so not being able to run code might be one indicator.

But maybe you need to step back a bit. Instead of assuming the solution can be achieved with specific technique, why don't you describe what you want to achieve or why you want to do this? That may take us to different approaches.

cheers, teylyn
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 33582729
> why don't you describe what you want to achieve or why you want to do  this?
Seconded

0
 

Author Comment

by:altariamx2003
ID: 33582988
Let me try to explain myself

this is what I try to do

when the cel "a2" is the active cell and is in edit mode I want to detect that state to make visible a combobox (instead the cell )with two values:
  1.- Value 1
  2.- Value 2

When the user select a value from the combobox I would like to assign that value to the active cell .



0
 
LVL 50

Accepted Solution

by:
teylyn earned 500 total points
ID: 33583019
Ah! That is called data validation and can be achieved without macros.

Click the cell. Click Data - Data Validation
Select List in the upper dropdown
In the Source box enter the values, separated by commas
Hit OK

Now, when you click your cell, you will see a drop-down arrow and you can select your value from the list.

You can also keep a list of values somewhere else in the sheet and refer to this range in the Data Validation setup. Things get a bit more complicated if you have the list of valid entries on another sheet. In Excel pre 2010 you will need to define a range name and use that as the source.

See the screen shot for referring to a list of values in the same sheet.

cheers, teylyn
0
 
LVL 50

Expert Comment

by:teylyn
ID: 33583031
oops, forgot the screenshots. Here comes:

dataval1.jpg
dataval2.jpg
0
 

Author Comment

by:altariamx2003
ID: 33583344
Sweet!!!

That was a great solution bro

thanks!!!!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now