I modified the codes to "work", but we nneed to think again I think....
I will take a guess that what you want is for when entries are made in column B , you would then, at that time, want the date and time of those entries to be written in column F. So column F shows the date of the last entries made in column B?
The code you gave is written to do that for any number of entries at one time in column B. So like if you paste in a whole lot of entries in one go , then all the corresponding rows in column F will be updated.
I think we really should have started with that code, and then the question would have been for that to be modified to put the previous entries in column E. But that assumed you/ we knew that a formuula was not possible, which you / we didn't. So, no worries....
But, I just mentioned that as the code below is an inefficient mess now, and will only work for one entry at a time, as that is what my first simple code did. My code snippet would error, and so will this new code for more than one entry at a time.
But I will post it anyway just for fun , and then start again and write a code from scratch to do what I expect you actually are needing to do
Alan
Option Explicit
Dim CurrentCostColumn2 As String
Private Sub Worksheet_selectionChange(ByVal Target As Range)
If Target.Column = 2 Then Let CurrentCostColumn2 = Target.Value
End Sub
Private Sub Worksheet_Change(ByVal Target As Range)
Rem 0) User defined Error handler
On Error GoTo TheEnd ' Always a good idea, especially in codes that may turn things off (like the Application.EnableEvents = False), too ooveride the default Error handling with this which will not prevent an error situation occuring, but on such an event, the code will continue at the spring point sent to. There you cann do anything that shoould always be done , even if the code errors
Rem 1) Alan's Bit
If Target.Column = 2 And CurrentCostColumn2 <> "" Then ' I could ignoor the <> "" check and just paste the "" in for the first entry, as for such a case the previous entry was ""
Application.EnableEvents = False ' This stops the code kicking in again at the next line. One problem if that did occur is that the Target Range would then change.
Target.Offset(0, 3).Value = CurrentCostColumn2
Application.EnableEvents = True ' It is always a good idea to switch anything back on as sooon as possible
Else ' case no change in column 2
End If
Rem 2) Put current date of entry in column F
' 2a) This repeats some of my code , making a Range object of the cross over of Target and column B, then checking to see if that exists as an indication of a valid range ( coolumn B ) hit
Dim WorkRng As Range ' Dim: For the Variable type declared, there should be some Blue Print or master Instruction list. If not The code will error at the start as it need to examine and therefore prepare for usage of the variable in the code run. The variable itself will go to some “Pigeon hole” that has a copy of the instructions or in some way has information on how to use the original instructions such that it can distinguish between different variables of the same type. When the code meets the variable it will look in the Pigeon Hole Pointer for instructions as to what to do in various situations. Knowing of the type allows in addition to get easily at the Methods and Properties through the applying of a period ( .Dot) ( intellisense ). Note this is a look up type list and may not be a guarantee that every offered thing is available – most will be typically.
Dim Rng As Range
Dim xOffsetColumn As Long ' Integer'_- If you need some sort of validation the value should only be within the range of a Byte/Integer otherwise there's no point using anything but Long.--upon/after 32-bit, Integers (Short) need converted internally anyways, so a Long is actually faster. ) Long is very simple to handle, - final memory "size" type is known (123.456 and 000.001 have same "size" computer memory ) , and so a Address suggestion can be given for the next line when the variable is filled in. '( Long is a Big whole Number limit (-2,147,483,648 to 2,147,483,647)
Set WorkRng = Intersect(Application.ActiveSheet.Range("B:B"), Target) ' Set: Setting to a Class will involve the use of an extra New at this code line. I will then have an Object referred to as an instance of a Class. At this point I include information on my Pointer Pigeon hole for a distinct distinguishable usage of an Object of the Class. For the case of something such as a Workbook this instancing has already been done, and in addition some values are filled in specific memory locations which are also held as part of the information in the Pigeon Hole Pointer. Wed will have a different Pointer for each instance. In most excel versions we already have a few instances of Worksheets. Such instances Objects can be further used., - For this a Dim to the class will be necessary, but the New must be omitted at Set. I can assign as many variables that I wish to the same existing instance
' xOffsetColumn = 1 ?
If Not WorkRng Is Nothing Then ' checking to see if that exists as an indication of a valid range ( coolumn B ) hit
Let Application.EnableEvents = False
For Each Rng In WorkRng
If Not VBA.IsEmpty(Rng.Value) Then ' only if target cell is not empty
Let Rng.Offset(0, 4).Value = Now ' Curren date in..
Let Rng.Offset(0, 4).NumberFormat = "dd-mm-yyyy, hh:mm:ss" ' .. a specific format
Else
Rng.Offset(0, 4).ClearContents ' column F cell if column B cell was empty
End If
Next
Let Application.EnableEvents = True
End If
TheEnd: ' Error handling code section. Put anything here that should be done in any case before the code ends
Let Application.EnableEvents = True
End Sub
Bookmarks