• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Automatic date update in a cell when another cell's value changes [SOLVED]

vinwin06

New Member
Hi All,


I need an help one excel formula ,i have specific range with prices, but when ever there is a change in value in the range then one particular need to be updated with now() formula. Find below the snap shot of desired data....


Product Changes happened Jan'13 Feb'13 Mar'13 Apr'13 May'13 Jun'13 Jul'13 Sep'13

Apple

Orange

Pineapple

Fruits


In the above data i have values for all product in all months. if there is any change happened then in the changes happened column need to be updated with now() formula.


Thanks in advance...


Vinwin
 
Hi, vinwin06!

Would you please elaborate and explain how the data is arranged and where do you want to set the actual date of change?

Consider uploading a sample file (including manual examples of desired output if applicable), it'd be very useful for those who read this and might be able to help you. Thank you. Give a look at the green sticky posts at this forums main page for uploading guidelines.

Regards!
 
Hi SirJB7,


http://www.mediafire.com/?dqbxs09xt7p5igh


In my sample file attached in the below link, the range of cell need to be set is "C3:M6". Meaning when ever they are changing on this range based on the product, the current date and time need to entered in column B.


For example if i am entering some value for apple on jan'13 column. then current date to entered in column B against apple. same case if i am entering some value tomorrow in feb'1 then simultaneously the value in column need to be changed to tomorrow date and time for apple.


Hope this is clear for you.


May be for a help for you i have some codes available also. But the problem is its having range to a particular only.


Private Sub Worksheet_Change(ByVal Target As Range)

End Sub

Dim R1 As Range

Dim R2 As Range

Dim InRange As Boolean

Set R1 = Range(Target.Address)

Set R2 = Range("C2:C20")

Set InterSectRange = Application.Intersect(R1, R2)


InRange = Not InterSectRange Is Nothing

Set InterSectRange = Nothing

If InRange = True Then

R1.Offset(0, 1).Value = Now()

End If

Set R1 = Nothing

Set R2 = Nothing

End Sub
 
Hi vinwin06,


Little modification in your provided macro..


Please check if its working fine..

[pre]
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim R1 As Range
Dim R2 As Range
Dim InRange As Boolean
Set R1 = Range(Target.Address)
Set R2 = Range("C2:M6")
InRange = Not Application.Intersect(R1, R2) Is Nothing
If InRange = True Then
Cells(R1.Row, 2) = Now()
End If
Set R1 = Nothing
Set R2 = Nothing
End Sub
[/pre]

Regards,

Deb
 
Hi, vinwin06!


Give a look at this file:

https://dl.dropboxusercontent.com/u/60558749/Automatic%20date%20update%20in%20a%20cell%20when%20another%20cell%27s%20value%20changes%20%28as%20calculated%20%28for%20vinwin06%20at%20chandoo.org%29.xlsm


It uses 2 dynamic named ranges:

DataTable: =DESREF(Hoja1!$A$2;;;CONTARA(Hoja1!$A:$A)-1;CONTARA(Hoja1!$1:$1)) -----> in english: =OFFSET(Hoja1!$A$2,,,COUNTA(Hoja1!$A:$A)-1,COUNTA(Hoja1!$1:$1))

HistoryTable: =DESREF(DataTable;;2;;COLUMNAS(DataTable)-2) -----> in english: =OFFSET(DataTable,,2,,COLUMNS(DataTable)-2)


This is the code:

-----

[pre]
Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)
' const
Const ksData = "DataTable"
Const ksHistory = "HistoryTable"
Const gkiChange = 2
' declarations
Dim rngD As Range, rngH As Range
Dim I As Long, J As Integer
' start
Set rngH = Range(ksHistory)
If Application.Intersect(Target, rngH) Is Nothing Or _
Target.Cells.Count > 1 Then GoTo Worksheet_Change_Exit
Set rngD = Range(ksData)
Application.EnableEvents = False
' process
With rngD
.Cells(Target.Row - .Row + 1, gkiChange).Value = Now()
End With
' end
Application.EnableEvents = True
Set rngD = Nothing
Worksheet_Change_Exit:
Set rngH = Nothing
End Sub
[/pre]
-----


Just advise if any issue.


Regards!


EDITED


@Debraj Roy

Hi!

Ooops... I didn't see you, you weren't here a while ago...

Regards!
 
Thank you so much both of us. Both the codes are working fine. But sirjb7 codes are little bit difficult to understand it.


Anyway thank you so much for helping me.
 
Hi, vinwin06!

Glad you solved it. Thanks for your feedback and welcome back whenever needed or wanted.

Regards!

PS: Sorry for the coding style, maybe it's a bit more complex but I think it ends being more flexible.
 
Hi, vinwin06!


Let me try:


1) I absolutely dislike to change the main part of any code only because the worksheet, range or cell to which it was applicable changed, that's to say the code should take a sort of parameters instead of being modified; it's safer and more clear to say (and Debraj Roy'll be going to kill me) that a name rngHistory is used rather than a Range("C2:M6"), the first gives another entity to the people who read it.


2) Besides I neither like to be changing the named ranges definition each time that the data changes, so if it's possible I always define dynamic named ranges instead of fixed reference named ranges. I know its easier to understand that a range named rngHistory address is "C2:M6" than =OFFSET(DataTable,,2,,COLUMNS(DataTable)-2) (and even more if DataTable is defined as =OFFSET(Hoja1!$A$2,,,COUNTA(Hoja1!$A:$A)-1,COUNTA(Hoja1!$1:$1))), but you can add product rows or dates columns without doing anything; so I choose this last option.


3) In the code itself I could have written range rngH definition as Range("DataTable") but I prefer to define all constants (except 0, 1, space, "", or sometimes 8 if talking of bits, 256 if talking of bytes, ...) at the top of the procedure rather than reading a code with constants, which if I need to change them I'd have to do it every time they're referenced as opposite to only one with my method; it increases a bit the code size, but disk space it's cheap nowadays (remember that the panic for the effect Y2K before year 2000 arrived was originated by guys who decided in the earlier 60's -as my grandpa told me-. and no one ever was forced to change, to store dates in format YYMMDD instead of YYYYMMDD just to save 2 bytes per date).


4) I like to divide each procedure in the following structures:

a) non-executable code

- option statements (usually only explicit, but compare database..., always declare everything)

- public constants (only if there are)

- public declarations (idem)

- constants

- declarations

- api calls (only if exist)

b) executable code

- start

- process

- end

In such way all what I over spend in the non-executable code I gain it many times in the executable code, making it simpler to maintain, and here I don't care if simpler to read: if anyone else or myself tomorrow have to get hands dirty on it, I prefer to have it clean and accessible even if long, before than scrambled and difficult to follow but short.

5) Naming conventions are fundamental for readability even if it's easier to write R1 than rngH. I adopted this long time ago, and for greater projects a three prefix instead of one for all:

Variables: i(nteger), l(ong), v(ariant), d(ate), s(tring), n(single), b(boolean), e(double)... iNoOfRows

Constants: same as variables plus the prefix k... kiNoOfRows

Parameters: same as variables plus the prefix p... piNoOfRows

Global var o consts: same as previous plus the prefix g... giNoOfRows, gkiNoOfRows

Public var o consts: same as previous pulus the prefix p... pgiNoOfRows, pgkiNoOfRows

Objects: usually three letters, app for application, rng for range, obj for object, ctl for control, cmd for command button, opt for option button, chk for checkbox, lst for list, cbo for combo, lbl for label, txt for text box, img for image, hsb for horizontal scroll bar, vsb for vertical...

Subroutine: normal name, descriptive, and those called within and not shared, with a similar prefix as the original name, InitializeStuff, InitializeStuffInternal, InitializeStuffExternal...

Function: idem subroutine but with the prefix rules of variables

I'll always like to read pgkiX rather than X, because X doesn't tell me anything, neither object, or variable, or type, or... but pgkiX it comes with the pedigree: public global constant of type integer, and that helps even if it's easier to read X = X + 12 than pgkiX = pgkiX + kiMonthsOfDelay...

6) Writing the main code.

Start section prepares the environment for the process, defines ranges, sets objects, dimensions arrays, retrieves parameters, assigns variables.

Process section, does the job using what start prepared.

End section cleans the mess.


This is my personal style, there're a lot of conventions, techniques, methodologies, and... and... and... but they're all really beyond the scope of this topic. You could google them with keywords as best practices coding, conventions, and you'll be crowded with lecture for the next millenium.


One humble advice: what's cheaper today, will be expensive today, and always proportionally, the more cheaper, the more expensive, so if you ask me, spend today everything you can, and tomorrow go for a Carlsberg.


Which is what I'm gonna do now.


Hope it helps and good luck in your path thru coding.


Regards!
 
@ SirJB7!


Believe me.. Its an ambition for me.. to write code like you..


Above code is just modified from provided code.. otherwise.. it will be more

shorter as well as hard to understand, hard to modify, hard to trap error.. even for myself also, after few days..


This type of ONE LINER are good to IMPRESS others.. but never good to EXPRESS yourself..


In my professional life.. I always use your coding style.. my each and every macro running in my office are very each to modify for me in case of vital changes also.. I only need to change in my default sheet or in constant area.


Regards,

Deb
 
hi sirjb7,

thank you sirjb7 for your explanation . I think it will take some time for me to understand . let me slowly try to understand it.


but I would like to write code like you as well. but let's see how it goes...... :)
 
@Debraj Roy

Hi!


Now that I know that you're not going to kill me... "Range("C2:M6")" really... ehmmm...


Regards!


PS: for more or less serious applications (remember we're talking about Excel, so strictness and security don't match with seriousness) try converting code to VB .NET, and handling Excel Object COM (component object model), so you'd be behind an .exe file... it's interesting the processs. Or if should remain in Excel consider implementing techniques like this:

http://chandoo.org/forums/topic/user-form-via-qat#post-28628

Users will hate you, but their bosses will love you, unless they become users too.


Hi, vinwin06!

You're welcome, I didn't write such a thing since many time ago, I hope it were more complete than incomplete and more understandable than illegible. My humble advice is to don't try to force a quicker path, go at your own rhythm and eventually take guiding courses. Besides come back here whenever needed or wanted but... please don't make write so much, maybe? :)

Regards!
 
thank you sirjb7 but can you suggest some books or reference material I can through to develop my skills and techniques on VBA coding.
 
Hi, vinwin06!

Have you tried searching in here?

http://chandoo.org/wp/search/?q=vba%20book%20learn

Regards!
 
Back
Top