ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   VBC Code (https://www.excelbanter.com/excel-programming/409386-vbc-code.html)

Leanne

VBC Code
 
Hi,

Sorry but this is probably a first grade question but I am so new to VBA
that I am still working out how to type in code!

All I want to do is ensure that when I have done the function I want that I
am taken to the next available cell.

Basically what I am trying to do (and probably the wrong way) is keep a
record of entries in a field that constantly changes. When something else is
changed it is updated but I want to keep a record of all the dates in that
field.

Please help

Gary''s Student

VBC Code
 
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


"Leanne" wrote:

Hi,

Sorry but this is probably a first grade question but I am so new to VBA
that I am still working out how to type in code!

All I want to do is ensure that when I have done the function I want that I
am taken to the next available cell.

Basically what I am trying to do (and probably the wrong way) is keep a
record of entries in a field that constantly changes. When something else is
changed it is updated but I want to keep a record of all the dates in that
field.

Please help


Leanne

VBC Code
 
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



Gary''s Student

VBC Code
 
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



Leanne

VBC Code
 
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



Gary''s Student

VBC Code
 
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


Leanne

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


Leanne

VBC Code
 
Hi Gary''s Student, I hope you pick this up again.

I have one final question. Is there a way to use a lookup function (which
in code I know little about) to show what the Cell address refers to if I
have a list?

This would be the result I am looking for -
Visit Date Date Changed Cell Company
01/05/2008 17/04/2008 $C$3 Portsmouth ERF

Using a list such as this created either in the same sheet or another one
altogether-
$C$2 Marchwood ERF
$C$3 Portsmouth ERF
$C$4 Marchwood Treatment Plant

I have asked this question of someone else but unfortunatly I can not
understand the instructions so was hoping you could help me as you have done
before.
Thanks

"Leanne" wrote:

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


Gary''s Student

VBC Code
 
If you have an entry and want to look up that entry in another table and pull
information from that table, then see:

http://www.cpearson.com/excel/TablesAndLookups.aspx
--
Gary''s Student - gsnu200779


"Leanne" wrote:

Hi Gary''s Student, I hope you pick this up again.

I have one final question. Is there a way to use a lookup function (which
in code I know little about) to show what the Cell address refers to if I
have a list?

This would be the result I am looking for -
Visit Date Date Changed Cell Company
01/05/2008 17/04/2008 $C$3 Portsmouth ERF

Using a list such as this created either in the same sheet or another one
altogether-
$C$2 Marchwood ERF
$C$3 Portsmouth ERF
$C$4 Marchwood Treatment Plant

I have asked this question of someone else but unfortunatly I can not
understand the instructions so was hoping you could help me as you have done
before.
Thanks

"Leanne" wrote:

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



All times are GMT +1. The time now is 06:04 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com