vba if not in column then add

winnienewtovba

New Member
Joined
Jul 1, 2012
Messages
10
hi,
i'm new to vba. i kept searching on how to do something that seems so simple.
any help on how to start would be great!
so, i have an user enter textbox C and an user enter textbox numeric value in D.
All i want the code to do is to look in column A for that C date, if the date exist exit, if not then add that date in column A and add the D value to the cell next to the matching A date.
is that at all possible?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Try something like this...

Code:
[color=darkblue]Sub[/color] Add_Date_and_Value()


    [color=darkblue]Dim[/color] d [color=darkblue]As[/color] Date
    [color=darkblue]Dim[/color] num [color=darkblue]As[/color] [color=darkblue]Double[/color]
    
    [color=green]'Prompt for a date[/color]
    d = Application.InputBox("Enter a date.", "Date Entry", Format(Date, "mm/dd/yyyy"), Type:=1)
    [color=darkblue]If[/color] d = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'User canceled[/color]
    
    [color=green]'Test if the date exists in column A[/color]
    [color=darkblue]If[/color] WorksheetFunction.CountIf(Range("A:A"), d) [color=darkblue]Then[/color]
        MsgBox "The date already exists in column A. ", , "Date Exists"
    [color=darkblue]Else[/color]
        [color=green]'Date doesn't exist[/color]
        [color=green]' Prompt for a number[/color]
        num = Application.InputBox("Enter a value.", "Value Entry", Type:=1)
        [color=darkblue]If[/color] num = 0 [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color] [color=green]'User canceled[/color]
        [color=darkblue]With[/color] Range("A" & Rows.Count).End(xlUp).Offset(1)
            .Value = Format(d, "mm/dd/yyyy")    [color=green]'Put date in the next empty row in column A[/color]
            .Offset(, 1).Value = num            [color=green]'Put the value in column B[/color]
        [color=darkblue]End[/color] [color=darkblue]With[/color]
    [color=darkblue]End[/color] [color=darkblue]If[/color]


End [color=darkblue]Sub[/color]
 
Upvote 0

Forum statistics

Threads
1,223,634
Messages
6,173,474
Members
452,516
Latest member
archcalx

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