Solved

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

Posted on 2010-09-01
13
319 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

932 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

12 Experts available now in Live!

Get 1:1 Help Now