Please help with VBA. Insert date into cell in a particular row.

coordintor

New Member
Joined
Jun 9, 2015
Messages
3
Hello,

I'm a new on forum. I need your help with creating VBA for button which will insert the date in particular cell in a row found by specyfied value)).
To put it simple, below is my tab.

[TABLE="width: 521"]
<tbody>[TR]
[TD][/TD]
[TD]a[/TD]
[TD]b[/TD]
[TD]c[/TD]
[TD]d[/TD]
[TD]e[/TD]
[TD]f[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD]content[/TD]
[TD]f[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD][/TD]
[TD]column1[/TD]
[TD]column2[/TD]
[TD]column3[/TD]
[TD]column4[/TD]
[TD]column5[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]b[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]c[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]f[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD]15-06-09[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]g[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]h[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]10[/TD]
[TD][/TD]
[TD]xxxx[/TD]
[TD]a[/TD]
[TD]yyyy[/TD]
[TD]zzzz[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
[TABLE="width: 521"]
<tbody>[TR]
[TD="align: right"][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
The logic is follow:
1. The value in D2 is changed
2. By means of button I need to insert and save the actual date in column5(F) if value in colum2(C) equals to D2.
3. If not found - message "not Found"

Thank you in advance for any help.
Please note that I'm not good in VBA :help:
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You can do this using a formula. Enter this formula in cell F5 and copy it down as far as your need it:=IF(C5=$D$2,NOW(),"Not found")
If you want to use a macro:
Code:
Sub EnterDate()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("C5:C" & LastRow)
        If rng = Range("D2") Then
            Range("F" & rng.Row) = Date
        Else
            Range("F" & rng.Row) = "Not found"
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Hi Mumps,

Thank you for quick response. I cannot use the formula as I need to keep the date after D2 is changed to other possible value. I gonna try your macro and get back to you with result. Thanks
 
Upvote 0
Code:
Sub FindAndDate()
Dim vVal
On Error GoTo errSkip


vVal = InputBox("Enter Value", "set date on value")


    Columns("C:C").Select
    Selection.Find(What:=vVal, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
Range(ActiveCell.Address).Select
activecell.offset(0,3).value = date()


Exit Sub


errSkip:
Range("C1").Select
MsgBox "Not Found"
End Sub
 
Upvote 0
You can do this using a formula. Enter this formula in cell F5 and copy it down as far as your need it:=IF(C5=$D$2,NOW(),"Not found")
If you want to use a macro:
Code:
Sub EnterDate()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Dim rng As Range
    For Each rng In Range("C5:C" & LastRow)
        If rng = Range("D2") Then
            Range("F" & rng.Row) = Date
       [B] Else
            Range("F" & rng.Row) = "Not found"[/B]        
        End If
    Next rng
    Application.ScreenUpdating = True
End Sub

Hi Mumps,
Macro works good but "Else" logic is not what I expected. I need to see the message box with "Not found"
Could you help with this?
 
Upvote 0

Forum statistics

Threads
1,224,271
Messages
6,177,609
Members
452,785
Latest member
3110vba

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