Search : in
By :

Make a macro run when data entered in a cell

Last answer on Nov 10, 2009 11:33:16 am GMT SandsB, on Feb 20, 2009 4:52:46 pm GMT 
 Report this message to moderators

Hello,

I have a macro that runs when the user clicks on a button. Instead, I want the macro to run when Cell D10 is populated. The data for D10 comes from a Data Validation drop down, if that matters. This should be easy but I can't figure it out.

Configuration: Windows XP
Internet Explorer 6.0

Best answers for « Make a macro run when data entered in a cell » in :
Excel tips : How to insert date in a cell ShowExcel tips : How to insert date in a cell Below are some tips on how to insert date and time in an excel cell for a specific purpose:- To insert current date, press CTRL ¯+ ;¯ in the chosen cell. To insert current time, press CTRL¯+...
[Excel] – Running Macro Automatically Show[Excel] – Running Macro Automatically Issue Solution Issue I have a macro in excel which needs to be run twice a day and I don’t even want to open the excel sheet. How can I make this process automatic? If I can convert the macro...
[VBA] Detecting changes in cell Show[VBA] Detecting changes in cell The Event Change feature of a sheet will detects the change in the active cell but it gives no information about the content. The example given below will help you to find out if the cell was changed,...
Download PDFtoHTML ShowPDFToHTML (PDF2HTML) is a command line application which allows to convert PDF files into HTML format. The source code is available on the site of the editor so that it is possible to make it run on a large number of platforms. It is possible to...
Using Telnet ShowIntroduction to Telnet Telnet is a protocol for emulating a remote terminal, which means that it can be used to run commands entered on a keyboard at a remote machine. The tool Telnet is an implementation of the Telnet protocol, which means that it...
Using Telnet ShowIntroduction to Telnet Telnet is a protocol for emulating a remote terminal, which means that it can be used to run commands entered on a keyboard at a remote machine. The tool Telnet is an implementation of the Telnet protocol, which means that it...
Spreadsheets - Cell Selection ShowCell Selection Spreadsheets are powerful tools for working with data. However, to work with data, it is necessary to have tools to rapidly choose the required cells. Line Selection An entire line can be chosen by clicking directly on the line...

1

WutUp WutUp, on Feb 20, 2009 7:13:24 pm GMT
  • +1

You could save your current macro from the button into a module, and then make a code for a worksheet change event.

Something like......

Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$D$10" Then

Call MyMacro

End If

Reply to WutUp WutUp

2

SandsB, on Feb 23, 2009 2:19:47 pm GMT

I tried this but nothing happens. What am I doing wrong here? I entered it in Module1. I'm using Excel 2007 if it matters.

Private Sub GetNotes(ByVal Target As Range)
If Target.Address = "$D$10" Then
If Range("D10").Value = "MyText" Then
Range("C15").Value = "This is what happens when D10 is equal to the text I want."
End If
If Range("D10").Value = "" Then
Range("C15").Value = ""
End If
End If
End Sub

Reply to SandsB

3

WutUp WutUp, on Feb 23, 2009 9:10:51 pm GMT
  • +4

You can keep your macro in module one. You must use the worksheet change event in the worksheet itself, not a module.
Let me explain.

I will provide a file to go along with the explanation. In the worksheet, range A5:A25 is fill with numbers.
If you enter a numeric value in cell D10, range G5:G10 will copy what is in A5:A25. If you delete the value in D10, then the contents of range G5:G10 will be deleted.

http://www.4shared.com/file/89145449/43c8c38/Change_Value_of­_D10.html


Whatever your original code was to run your macro, put that back in it's original form.

Since you are using Excel 2007, this is what you do.

1) Click on the Developer tab.
2) Click on the Visual Basic icon.
3) On the left pane window, double click the sheet where you need your code to run.
4) Now, at the top of the code window you will see... (General) with a drop down, and (Declarations) with a drop down.
5) Click the drop down by (General) and select Worksheet.
6) Now in the code window you will see.... Private Sub Worksheet_SelectionChange(ByVal Target As Range)
7) Remove the word "Selection". You want to remove "Selection" because that means when you click on a cell in the worksheet something will happen. You do not want that, you want to enter a value in D10. It should now read...Private Sub Worksheet_Change(ByVal Target As Range)
8) Now this is where you want the code...

Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Address = "$D$10" Then

Call MyMacro

End If

End Sub
9) Meaning, when you change the value in D10 then the worksheet change event will "Call" your macro.

Reply to WutUp WutUp

11

 Aaaacke, on Nov 10, 2009 11:33:16 am GMT

THANK YOU! You solved a problem I've worked with for over a week!

Hugs from a trainee in Sweden :)

Reply to Aaaacke

4

Bhumi, on Mar 13, 2009 12:13:17 pm GMT

Hi I am very new to VBA. Because of this thread i do sort of know what you mean but the code doesnt seem to be working for me.


Private Sub Worksheet_Change(ByVal Target As Range)

If Target.Address = "$A$8" Then
Call PVValue
Call OrderData

End If

End Sub

the sub function PVValue and OrderData are in a modul, and otherwise work absolutely fine. what could be wrong??

thanks
Bhumi

Reply to Bhumi

5

WutUp WutUp, on Mar 13, 2009 3:15:26 pm GMT

Did you put the code in the worksheet or create another module?
If you did, are you putting a value in A8, or are you wanting to click on it?

Reply to WutUp WutUp

6

Joe, on May 22, 2009 12:52:03 pm BST

Hi Wutup,

just wanted to say thank you! I have quite some experience in VBA for Access, but honestly, in Excel I had no idea where to start.

Your introduction was great, and the best: It works :-)

Thanks again


Joe

Reply to Joe

7

Miki, on Jun 1, 2009 3:19:16 pm BST

Hi there,

I have a very similar requirement but have been trying for a couple of hours (without success) to tailor-make this scenario to meet my requirement, which is:

If a predefined value is entered against one fo the cells in an Excel column, a macro is to be triggered. The macro is to copy the active row and insert this as a new row beneath it.

PLEASE can one of you assist?

Reply to Miki

8

RapedByVBA, on Aug 4, 2009 9:40:46 pm BST

I had the same issue and just wanted to thank the original questioner and WutWut for the really amazing explanation.

Reply to RapedByVBA

9

tipusalman87, on Aug 25, 2009 8:47:02 pm BST

I love you WutUp WutUp! I didn't know a thing about using a macro function or even using VB. But I wholeheartedly followed your instructions when I had a similar problem of calling a macro, to limit the characters in a cell, when a cell is populated, and you know what I got it right right at the first instance. I am so thankful to you for teaching me something that I really wanted to learn and which would have taken me hours to figure out otherwise.

Keep on helping us just the way you are doing it.

Reply to tipusalman87

10

Phil11, on Nov 3, 2009 4:49:45 pm GMT

Hi,

Thank you for this thread, It has been a great help to me. I have a little extension of the problem though.
I have a spreadsheet which calls on data from SQL which my I.T guy set up, and it then populates information on sheets which have been created from running a previous macro. (If that doesn't make sense, let me know).
Then I actually want the function of being able to call the macro on these new sheets however it won't copy the call function into the new sheets.
I currently have a macro button on the original "template" doc, which is copied to the new sheets, however it'd be nice if I could get rid of these buttons and have the macro run on a change.
Is there any way of doing this or am I asking too much of excel??
Thanks

Reply to Phil11