1

I have an excel file with a column which has date data. I want the user to input a date of their choosing and then I want to create a new column that lists the difference in days between the two dates. The Macro that I have is working but I have a few questions and I would like to make it better. Link to MWE small data file is here.

The user input date was 9/30/2013, which I stored in H20

Macro:

Sub Date_play()
    Dim x As Date
    Dim x2 As Date
    Dim y As Variant

    x = InputBox(Prompt:="Please enter the Folder Report Date. The following formats are acceptable: 4 1 2013 or April 1 2013 or 4/1/2013")

    x2 = Range("E2")

    y = DateDiff("D", x2, x)
    MsgBox y  

    'Used DateDiff above and it works but I don't know how to use it to fill a column or indeed a cell.

    Range("H20").FormulaR1C1 = x
    Range("H1").FormulaR1C1 = "Diff"
    Range("H2").Formula = "=DATEDIF(E2,$H$20,""D"")"
    Range("H2").AutoFill Destination:=Range("H2:H17")
    Range("H2:H17").Select

End Sub
  1. Now, could I have done this without storing the user input date in a particular cell? I would've preferred to use the variable "x" in the formula but it wasn't working for me. I had to store the user input in H20 and then use $H$20.

  2. What's the difference between the function Datedif and the procedure DateDiff? I am able to use the procedure DateDiff in my macro but I don't know how to use it to fill out my column. Is one method better than the other?

  3. Is there a better way to add columns to the existing sheet, where the columns include some calculations involving existing data on the sheet and some user inputs? There are tons of more complicated calculations I want to do next.

Thanks

watakushi
  • 51
  • 9
Amatya
  • 1,203
  • 6
  • 32
  • 52

1 Answers1

1

Q1. Now, could I have done this without storing the user input date in a particular cell? I would've preferred to use the variable "x" in the formula but it wasn't working for me. I had to store the user input in H20 and then use $H$20.

Try this (UNTESTED)

Replace

Range("H20").FormulaR1C1 = x
Range("H1").FormulaR1C1 = "Diff"
Range("H2").Formula = "=DATEDIF(E2,$H$20,""D"")"
Range("H2").AutoFill Destination:=Range("H2:H17")
Range("H2:H17").Select

by

Range("H2:H17").Formula = "=DATEDIF(E2," & datevalue(x) & ",""D"")"

The above will fill all the cells with the formula in one go. You do not need the Autofill to do the job. Also Inputbox is the worst choice to accept dates. You might want to see THIS

Q2. What's the difference between the function Datedif and the procedure DateDiff? I am able to use the procedure DateDiff in my macro but I don't know how to use it to fill out my column. Is one method better than the other?

DATEDIF is a worksheet function and DateDiff is a VBA Function.

Community
  • 1
  • 1
Siddharth Rout
  • 147,039
  • 17
  • 206
  • 250
  • Hey Siddharth, datevalue(x) is not working. I am getting #NUM! in the cells and the formula for the cell says =DATEDIF(E2, 8/25/13,"D") if my x was 8/25/13 – Amatya Nov 14 '13 at 15:43
  • I just tested it and it works just fine. What happens if you put x as "25/8/13"? – Siddharth Rout Nov 14 '13 at 16:15
  • If I accept x in the InputBox as "8/25/13" then I get a mismatch because I have DIM x As Date. If I change x to String, and then accept x in InputBox as "8/25/13" then I am getting an error and #NUM! in the cells. – Amatya Nov 14 '13 at 16:26
  • for me it works both ways. Did you see the link in my answer on how to accept dates? – Siddharth Rout Nov 14 '13 at 16:29
  • I totally missed the link. I'll check it out and respond. Thanks! :) – Amatya Nov 14 '13 at 16:31