# Excel:  How do I run a VBA script when a cell is updated?

**URL:** <https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124>\
**Category:** Factual Questions\
**Created:** [February 16, 2009, 5:57pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124 "2009-02-16T17:57:31Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Whack-a-Mole](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/whack-a-mole/32/141_2.png) [@Whack-a-Mole](https://boards.straightdope.com/u/Whack-a-Mole)\
**Post date:** [February 16, 2009, 5:57pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/1 "2009-02-16T17:57:31Z")

</div>

I am trying to get a date to autofill when a cell is updated.

So lets say I enter “6100” in cell D3 I want cell E3 to autofill the date (and then stay that way and not recalc till another amount is input in D3).

I see people all over wanting this and it seems unusually difficult. Anyway, I found the follow VB script:

\*Private Sub Worksheet\_Change(ByVal Target As Range)  
If Target.Column = 1 Then  
Intcolumn = Target.Column  
introw = Target.Row  
Cells(introw, Intcolumn + 1) = Now()

End If

End Sub\*

I assume that will work but for the life of me cannot sort how to get it to run.

My Google-fu fails since it seems everyone who knows this stuff just assumes something as basic as making it work is something we all know.

Help! 😃

---

<div class="post-metadata">

**Author:** ![Anaglyph](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Anaglyph](https://boards.straightdope.com/u/Anaglyph)\
**Post date:** [February 16, 2009, 6:25pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/2 "2009-02-16T18:25:23Z")

</div>

I can’t give you the answer off hand, but i would post the question [here](http://groups.google.com/group/microsoft.public.excel/topics?hl=en&lnk)

---

<div class="post-metadata">

**Author:** ![Dervorin](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dervorin](https://boards.straightdope.com/u/Dervorin)\
**Post date:** [February 16, 2009, 6:41pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/3 "2009-02-16T18:41:10Z")

</div>

> [@Whack-a-Mole](#):
>
> I am trying to get a date to autofill when a cell is updated.
> 
> So lets say I enter “6100” in cell D3 I want cell E3 to autofill the date (and then stay that way and not recalc till another amount is input in D3).
> 
> I see people all over wanting this and it seems unusually difficult. Anyway, I found the follow VB script:
> 
> \*Private Sub Worksheet\_Change(ByVal Target As Range)  
> If Target.Column = 1 Then  
> Intcolumn = Target.Column  
> introw = Target.Row  
> Cells(introw, Intcolumn + 1) = Now()
> 
> End If
> 
> End Sub\*
> 
> I assume that will work but for the life of me cannot sort how to get it to run.
> 
> My Google-fu fails since it seems everyone who knows this stuff just assumes something as basic as making it work is something we all know.
> 
> Help! 😃

Well, one of the problems appears to be that the script you have only works in column A, and if you want it to work in column D you have to change the

```auto

If Target.Column = 1 Then

```

to

```auto

If Target.Column = 4 Then

```

If you’re looking for the place to insert this code, I can help you out, if you let me know which version of Excel you’re using.

---

<div class="post-metadata">

**Author:** ![Whack-a-Mole](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/whack-a-mole/32/141_2.png) [@Whack-a-Mole](https://boards.straightdope.com/u/Whack-a-Mole)\
**Post date:** [February 16, 2009, 6:57pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/4 "2009-02-16T18:57:43Z")

</div>

> [@Dervorin](#):
>
> Well, one of the problems appears to be that the script you have only works in column A, and if you want it to work in column D you have to change the
> 
> ```auto
> 
> If Target.Column = 1 Then
> 
> ```
> 
> to
> 
> ```auto
> 
> If Target.Column = 4 Then
> 
> ```
> 
> If you’re looking for the place to insert this code, I can help you out, if you let me know which version of Excel you’re using.

Thanks.

It is Excel 2007.

Think I got it to work with an Iterative function.

_=IF(A1=“”,“”,IF(B1=“”,NOW(),B1))_

Had to change a setting that allowed it to do a circular lookup but seems to be working.

I’d still like to know how to get that VB stuff working though.

Thanks again.

---

<div class="post-metadata">

**Author:** ![Dervorin](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@Dervorin](https://boards.straightdope.com/u/Dervorin)\
**Post date:** [February 16, 2009, 7:15pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/5 "2009-02-16T19:15:43Z")

</div>

Excel 2007, right. _cracks knuckles_

First make sure you’ve got the Developer tab displayed: [Microsoft Support](http://office.microsoft.com/en-us/excel/HA101730521033.aspx)  
Then change to the Developer tab and click on the Visual Basic button  
Right-click Sheet1 (or whatever sheet you’re working with) and click on View Code  
Paste the entire macro in (including the Private Sub and End Sub lines) into the right-hand pane.  
Click Save in the Visual Basic Editor and then close it.  
Go back to your worksheet and check. With your original macro, any changes in column A should give you an instant update in column B.

---

<div class="post-metadata">

**Author:** ![Whack-a-Mole](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/whack-a-mole/32/141_2.png) [@Whack-a-Mole](https://boards.straightdope.com/u/Whack-a-Mole)\
**Post date:** [February 16, 2009, 8:07pm UTC](https://boards.straightdope.com/t/excel-how-do-i-run-a-vba-script-when-a-cell-is-updated/486124/6 "2009-02-16T20:07:53Z")

</div>

> [@Dervorin](#):
>
> Excel 2007, right. _cracks knuckles_
> 
> First make sure you’ve got the Developer tab displayed: [Your request has been blocked. This could be due to several reasons.](http://office.microsoft.com/en-us/excel/HA101730521033.aspx)  
> Then change to the Developer tab and click on the Visual Basic button  
> Right-click Sheet1 (or whatever sheet you’re working with) and click on View Code  
> Paste the entire macro in (including the Private Sub and End Sub lines) into the right-hand pane.  
> Click Save in the Visual Basic Editor and then close it.  
> Go back to your worksheet and check. With your original macro, any changes in column A should give you an instant update in column B.

Thx!

I’ll give it a go.
