Worksheet_Change problem

noob987

New Member
Joined
Jul 27, 2018
Messages
3
Hi everyone,

I've written a little code in vba but since I'm totally new to vba, it doesn't really do yet what i want.

The code is meant to track changes in a document that is used over different companies. (I know there are better options like office online but this is impossible for several reasons). Now, when someone changes a cell I want to make sure they give a reasoning for their change or otherwise they can't go on.

The problem with this code is that when I double click a cell and don't change anything, the code still asks for a reasoning. Which I would like to avoid.

Here's what I've got already:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)    Dim KeyCells As Range
    Dim rng As Range
    Dim MyInput As Variant
    Dim CellContent As Variant
    Set KeyCells = Range("D4:D35")
        
    If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
        
        'ask for reasoning and put it in the comment of the cell.
        MyInput = InputBox("Give a reasoning please.")
        Set rng = Range(Target.Address)
        With rng.AddComment
            .Visible = False
            .Text MyInput
         
            While MyInput = ""
               MyInput = InputBox("Give a reasoning please.")
            Wend
            
        End With
        'Highlight changed cell
        Target.Interior.ColorIndex = 6
    End If
End Sub

My first idea was to read in the entire range in a variable or table or something (before any edits are made) and then compare the new value of the cell with the old one that is stored. But it seems to me that there should be easier solutions...

Does anyone have an idea?
Thank you!
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Welcome to the Board!

The problem with this code is that when I double click a cell and don't change anything, the code still asks for a reasoning. Which I would like to avoid.
Why not just add another line of code that checks to see if the cell actually has anything in it, i.e.
Code:
If Target <>"" Then
That way, if they double-click, but don't enter anything, it will not ask for reasoning.
 
Last edited:
Upvote 0
Thank you for your answer :)

The problem is when there's already something in the cell. So for example cell C4 contains the number 10. Now someone double clicks the cell by accident, but doesn't want to change the number 10, my code will still ask for a reasoning for his change.
 
Upvote 0
You could check the cell to see if it already has a reasoning.
However, if you want to add a new reasoning if they change an already changed value, then you need to capture the previous value of the cell to see if it has changed. That gets a little bit tricky.
See this thread on how to do that: https://stackoverflow.com/questions...-the-old-value-of-a-changed-cell-in-excel-vba

So basically, you are declaring a global value, capturing the value of a cell as you enter it, and then in your Worksheet_Change code, you would need to compare the current value of Target to the global variable to see if it has changed.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

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