Insert Dates automatically

maxlm

Board Regular
Joined
Jul 22, 2003
Messages
112
I need to be able to put dates automatically on VBA for cells using offset shown below. The sample is only for one column. How do I add the other for j, k, l, m, n using the following offsets. PLESAE HELP... THANKS.
Offsets:
j = 11
k = 1
l = 30
m = 4
n = 1

Private Sub Worksheet_Change(ByVal Target As Range)
Dim I, j, k, l, m, n As Range
Set I = Intersect(Range("BC4:BC500"), Target)
Set j = Intersect(Range("AT4:AT500"), Target)
Set k = Intersect(Range("AD4:AD500"), Target)
Set l = Intersect(Range("AN4:AN500"), Target)
Set m = Intersect(Range("AL4:AL500"), Target)
Set n = Intersect(Range("AP4:AP500"), Target)

If I Is Nothing Then Exit Sub
Application.EnableEvents = False
Dim c As Range
For Each c In I
c.Offset(0, 1).Value = Format(Now, "mm/dd/yy")
Next c
Application.EnableEvents = True

End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hello, how about the following:

<font face=Courier New><SPAN style="color:darkblue">Private</SPAN> <SPAN style="color:darkblue">Sub</SPAN> Worksheet_Change(<SPAN style="color:darkblue">ByVal</SPAN> Target <SPAN style="color:darkblue">As</SPAN> Range)
<SPAN style="color:darkblue">If</SPAN> Intersect(Target(1, 1), Range("J:N")) <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">With</SPAN> Target(1, 1)
    .Item(, Choose(.Column - 9, 12, 2, 31, 5, 2)).Value = <SPAN style="color:darkblue">Date</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
Thanks Nate. I am really very poor at this. Ok, is what I've done. This is for the actual worksheet (different offsets). What I need to do is,
If change column BL, will put date on Column L,
If change column AT, will put a current date also on L;
And, If L has a Date value, will put the username of user on column M,
If chnage column BE, will put date on column BF,
If change column AV, will put date on column BG.

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target(1, 1), Range("K:BG")) Is Nothing Then Exit Sub
Application.EnableEvents = False
With Target(1, 1)
.Item(, Choose(.Column - 11, -52, -34, 1, 1, 11)).Value = Date
End With
Application.EnableEvents = True
End Sub



NateO said:
Hello, how about the following:

<font face=Courier New><SPAN style="color:darkblue">Private</SPAN> <SPAN style="color:darkblue">Sub</SPAN> Worksheet_Change(<SPAN style="color:darkblue">ByVal</SPAN> Target <SPAN style="color:darkblue">As</SPAN> Range)
<SPAN style="color:darkblue">If</SPAN> Intersect(Target(1, 1), Range("J:N")) <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">With</SPAN> Target(1, 1)
    .Item(, Choose(.Column - 9, 12, 2, 31, 5, 2)).Value = <SPAN style="color:darkblue">Date</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
I see. Try the following:

<font face=Courier New><SPAN style="color:darkblue">Private</SPAN> <SPAN style="color:darkblue">Sub</SPAN> Worksheet_Change(<SPAN style="color:darkblue">ByVal</SPAN> Target <SPAN style="color:darkblue">As</SPAN> Range)
<SPAN style="color:darkblue">Dim</SPAN> z <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>
<SPAN style="color:darkblue">If</SPAN> Intersect(Target(1, 1), Range("AT:AT,AV:AV,BE:BE,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> Nothing _
    <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> Intersect(Target(1, 1), Range("AT:AT,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
    <SPAN style="color:darkblue">With</SPAN> Cells(Target.Row, 12)
        <SPAN style="color:darkblue">If</SPAN> Len(.Value) <SPAN style="color:darkblue">Then</SPAN>
            .Value = Environ("UserName")
            z = <SPAN style="color:darkblue">Not</SPAN> z
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">With</SPAN> Target(1, 1)
    <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> z <SPAN style="color:darkblue">Then</SPAN> Cells(.Row, WorksheetFunction.Lookup(.Column, _
        [{46,12;48,59;57,58;64,12}])).Value = <SPAN style="color:darkblue">Date</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
Works like wonder... One little snag, the username should be in column M (not in L). How it works, is if not blank and value is date in L, M = username of user who updated L.

This code is very efficient. thanks,
Max



NateO said:
I see. Try the following:

<font face=Courier New><SPAN style="color:darkblue">Private</SPAN> <SPAN style="color:darkblue">Sub</SPAN> Worksheet_Change(<SPAN style="color:darkblue">ByVal</SPAN> Target <SPAN style="color:darkblue">As</SPAN> Range)
<SPAN style="color:darkblue">Dim</SPAN> z <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>
<SPAN style="color:darkblue">If</SPAN> Intersect(Target(1, 1), Range("AT:AT,AV:AV,BE:BE,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> Nothing _
    <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> Intersect(Target(1, 1), Range("AT:AT,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
    <SPAN style="color:darkblue">With</SPAN> Cells(Target.Row, 12)
        <SPAN style="color:darkblue">If</SPAN> Len(.Value) <SPAN style="color:darkblue">Then</SPAN>
            .Value = Environ("UserName")
            z = <SPAN style="color:darkblue">Not</SPAN> z
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">With</SPAN> Target(1, 1)
    <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> z <SPAN style="color:darkblue">Then</SPAN> Cells(.Row, WorksheetFunction.Lookup(.Column, _
        [{46,12;48,59;57,58;64,12}])).Value = <SPAN style="color:darkblue">Date</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
Right, misread that a touch. it's a minor adjustment:

<font face=Courier New><SPAN style="color:darkblue">Private</SPAN> <SPAN style="color:darkblue">Sub</SPAN> Worksheet_Change(<SPAN style="color:darkblue">ByVal</SPAN> Target <SPAN style="color:darkblue">As</SPAN> Range)
<SPAN style="color:darkblue">Dim</SPAN> z <SPAN style="color:darkblue">As</SPAN> <SPAN style="color:darkblue">Boolean</SPAN>
<SPAN style="color:darkblue">If</SPAN> Intersect(Target(1, 1), Range("AT:AT,AV:AV,BE:BE,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> Nothing _
    <SPAN style="color:darkblue">Then</SPAN> <SPAN style="color:darkblue">Exit</SPAN> <SPAN style="color:darkblue">Sub</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">False</SPAN>
<SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> Intersect(Target(1, 1), Range("AT:AT,BL:BL")) <SPAN style="color:darkblue">Is</SPAN> <SPAN style="color:darkblue">Nothing</SPAN> <SPAN style="color:darkblue">Then</SPAN>
    <SPAN style="color:darkblue">With</SPAN> Cells(Target.Row, 12)
        <SPAN style="color:darkblue">If</SPAN> IsDate(.Value) <SPAN style="color:darkblue">Then</SPAN>
            .Item(, 2).Value = Environ("UserName")
            z = <SPAN style="color:darkblue">Not</SPAN> z
        <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
    <SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">If</SPAN>
<SPAN style="color:darkblue">With</SPAN> Target(1, 1)
    <SPAN style="color:darkblue">If</SPAN> <SPAN style="color:darkblue">Not</SPAN> z <SPAN style="color:darkblue">Then</SPAN> Cells(.Row, WorksheetFunction.Lookup(.Column, _
        [{46,12;48,59;57,58;64,12}])).Value = <SPAN style="color:darkblue">Date</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">With</SPAN>
Application.EnableEvents = <SPAN style="color:darkblue">True</SPAN>
<SPAN style="color:darkblue">End</SPAN> <SPAN style="color:darkblue">Sub</SPAN></FONT>
 
Upvote 0
thanks again Nate. Did not work the way it should. Only inputs username the second time when I delete record in Cell AT or BL. It does not insert username in M if L has a date value.

Should be, Change cells AT or BL, inserts date in L.
then, if L=date, M=username.

thanks,
 
Upvote 0
I thought you meant that if there was a date in L in the first place, rather than one placed there in the same routine call. Otherwise, you just do both in the same data push. I am taking you quite literally. It works like this:

Change AL or BT:
If L has a date, enter a username in M.
If not, enter a date into L, no more, no less.
 
Upvote 0
NateO said:
I thought you meant that if there was a date in L in the first place, rather than one placed there in the same routine call. Otherwise, you just do both in the same data push. I am taking you quite literally. It works like this:

Change AL or BT:
If L has a date, enter a username in M.
If not, enter a date into L, no more, no less.

Dont want to confuse you Nate, sorry. I meant on one routine call, If change BT or AL, Date in L, username in M. If that is what the code should do, it does not insert username in M.

thanks again,
 
Upvote 0

Forum statistics

Threads
1,215,641
Messages
6,125,980
Members
449,276
Latest member
surendra75

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