Worksheet_Change Inquiry

delulytric

New Member
Joined
Mar 15, 2019
Messages
4
Hi, I'm a beginner in VBA and I'm not familiar with most of the code here. I read up on several threads about Worksheet_Change() and can't find something that suits to my need.

Currently, I'm doing a project and let's assume that I changed the price of fish from $2 to $4.

Sheet 1 (Original sheet)

ABC
1NamePriceQuantity
2Chicken$2.507
3Fish$4.003

<tbody>
</tbody>

I want it to transfer the entire row 3 Fish and store it into a historical data sheet for record keeping purpose. Bear in mind that for every change, it should automatically update itself through a new row! I would also want a date changed on column D so that it is understood when the last person did change the price or quantity of the row itself.

Sheet 2 (Historical data sheet)

ABCD
1NamePriceQuantityDate Changed
2Fish$4.00315 March 2019
3Fish$5.00316 March 2019

<tbody>
</tbody>

I've search through the forum and sad to say I'm not very capable in handling VBA as opposed to my other projectmates. I only understood the start command Private Sub Worksheet_Change(), the end command and if intersect(target, range) is Nothing exit sub.

Any help here would be greatly appreciated here!
 
Last edited by a moderator:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
Welcome to the Board!

Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim rng As Range
    Dim cell As Range
    Dim r As Long
    Dim nr As Long
    
    Application.ScreenUpdating = False
    
'   Set range to watch for changes (column B)
    Set rng = Intersect(Target, Range("B:B"))
    
'   Exit sub if change is not in column B
    If rng Is Nothing Then Exit Sub
    
'   Loop through changed cells in column B (usually 1)
    For Each cell In rng
'       Capture row number of change
        r = cell.Row
'       Find next available row on sheet 2
        nr = Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Row + 1
'       Copy row from sheet1 to sheet2
        Sheets("Sheet1").Range(Cells(r, "A"), Cells(r, "C")).Copy Sheets("Sheet2").Cells(nr, "A")
'       Add date stamp to column D
        Sheets("Sheet2").Cells(nr, "D") = Date
    Next cell
        
    Application.ScreenUpdating = True
    
End Sub
 
Last edited:
Upvote 0
Thank you so much Joe4! I really appreciate the effort you put to help me out! Will try to read up on the code and try to understand it for future usage!
 
Upvote 0
You are welcome.
So, does it do what you want? They most important things in these Worskheet_Change event procedures is that they need to be placed in the proper location, in order to work automatically (in the proper sheet module).
If you have any particular questions about my code, feel free to ask! I always like helping people who show an interest in learning.
 
Upvote 0
Yups it does what I want but I need to wait for my projectmates to figure if they want any improvements. I think they will be able to improvise it further! Yeah, I played around with the sheet location directory, like sheet2 to sheet1, or sheet4 to sheet3 if sheet location is different. I get the code logic right there and learnt some new command personally haha.

Let's say if I want to record the previous data and current data (fish $2 previously, and fish $4 currently), do I need to declare a few more variables and keep track of like previous data and then copy the previous stored variables (fish, $2) first before copying the currently stored variables (fish, $4)? Haha.
 
Upvote 0
Let's say if I want to record the previous data and current data (fish $2 previously, and fish $4 currently), do I need to declare a few more variables and keep track of like previous data and then copy the previous stored variables (fish, $2) first before copying the currently stored variables (fish, $4)? Haha.
Yes, it gets a little tricky (you need to add Worksheet_SelectionChange to capture the value before you change it) If you do a Google Search, you should be able to find lots of posts on how to do that.
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top