Thread: VBC Code
View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Leanne Leanne is offline
external usenet poster
 
Posts: 87
Default VBC Code

Thank you so much, this works great.

"Gary''s Student" wrote:

The only change you need to make in your version is to replace the line:
v = ra.Value
with:
v=t.Value

Here is another version that goes a little further. Since we can change any
of a set of cells and want to record the history, this version records that
address of the cell being changed in column C of the "Visit History"
worksheet. This we we know which cell has benn changed, the new value, and
the date:

Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set ra = Range("C2:C300")
Set s2 = Sheets("Visit History")
If Intersect(ra, t) Is Nothing Then Exit Sub
v = t.Value
n = s2.Cells(Rows.Count, 1).End(xlUp).Row + 1
Application.EnableEvents = False
s2.Cells(n, 1).Value = v
s2.Cells(n, 2).Value = Date
s2.Cells(n, 3).Value = t.Address
Application.EnableEvents = True
End Sub

Of course, if you try the new version, you must delete the old version.


--
Gary''s Student - gsnu200779


"Leanne" wrote:

Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set ra = Range("C2:C300")
Set s2 = Sheets("Visit History")
If Intersect(ra, t) Is Nothing Then Exit Sub
v = ra.Value
n = s2.Cells(Rows.Count, 1).End(xlUp).Row + 1
Application.EnableEvents = False
s2.Cells(n, 1).Value = v
s2.Cells(n, 2).Value = Date
Application.EnableEvents = True
End Sub

Thanks! On just about everything else Excel I have no trouble - just not VBA.



"Gary''s Student" wrote:

We are making excellent progress!

Post the code as you have modified it and we will get it to work
--
Gary''s Student - gsnu200779


"Leanne" wrote:

Hi Gary,
Thanks, this sounds exactly what I want. I have given it a go and I have
the following problems.
1. I have changed the Range to what I need (C2:C300) but it does not record
changes to other cells.
2. It records a change when other cells are changed but records what is in
C2 only and not the cell that was changed.

I need to keep a record of changes to several cells and they may not all be
changed at the same time. Also as each row is for a different customer I
want to see the name of the customer whos information was changed.

Thanks again

"Gary''s Student" wrote:

You may be able to adapt this to your needs. Say in Sheet1 we make manual
changes to cell A1. Every time we manually change A1 we want to record the
new value and the date in Sheet2, in a running list. In the worksheet code
area for Sheet1 enter:

Private Sub Worksheet_Change(ByVal Target As Range)
Set t = Target
Set ra = Range("A1")
Set s2 = Sheets("Sheet2")
If Intersect(ra, t) Is Nothing Then Exit Sub
v = ra.Value
n = s2.Cells(Rows.Count, 1).End(xlUp).Row + 1
Application.EnableEvents = False
s2.Cells(n, 1).Value = v
s2.Cells(n, 2).Value = Date
Application.EnableEvents = True
End Sub


Because it is worksheet code, it is very easy to install and automatic to use:

1. right-click the tab name near the bottom of the Excel window
2. select View Code - this brings up a VBE window
3. paste the stuff in and close the VBE window

If you have any concerns, first try it on a trial worksheet.

If you save the workbook, the macro will be saved with it.


To remove the macro:

1. bring up the VBE windows as above
2. clear the code out
3. close the VBE window

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

To learn more about Event Macros (worksheet code), see:

http://www.mvps.org/dmcritchie/excel/event.htm

--
Gary''s Student - gsnu200779