Results 1 to 16 of 16

Thread: [RESOLVED] [Excel] How to subtract Date from Userform

  1. #1

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Resolved [RESOLVED] [Excel] How to subtract Date from Userform

    Hi Guys,

    How can is subract 2 dates in a userform VBA? Can anyone help me?

    Thanks and Regards,
    Allan
    Last edited by allankevin; Oct 30th, 2008 at 12:23 AM.
    "In order to improve the mind, we ought less to learn, than to contemplate."

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] How to subtract Date from Userform

    Use The DateDiff function

    Code:
    Function DiffInDates(pDate1 As Date, pDate2 As Date) As Long
         DiffInDates = DateDiff("d", pDate1, pDate2)
    End Function
    
    Sub DateDifference()
        'This will give you 11
        MsgBox DiffInDates(#12/13/2008#, #12/24/2008#)
    End Sub
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [Excel] How to subtract Date from Userform

    Hi Kool,

    Your code seems to work.. but i dont know how to apply in my userform..

    Want i want to do is when I input a date in textbox1 10/28/08 and I input again 10/20/08 in textbox2 then the answer should be in textbox3 8 after I Exit on textbox2..
    "In order to improve the mind, we ought less to learn, than to contemplate."

  4. #4
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] How to subtract Date from Userform

    TextBox3.Text = DiffInDates(TextBox1.Text, TextBox2.Text)
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  5. #5

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [Excel] How to subtract Date from Userform

    Thanks Kool! It works..
    "In order to improve the mind, we ought less to learn, than to contemplate."

  6. #6

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Hi Kool,

    What if I use Date Picker and netWorkdays?
    "In order to improve the mind, we ought less to learn, than to contemplate."

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Date Picker
    same goes there...

    Replace the values inside DiffInDates()

    netWorkdays
    What about this?
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Ive already get the datepicker..networkdays function..if there is an holiday or saturday and sunday it will not count that as regualar day.. somthing like that..is it posible?
    "In order to improve the mind, we ought less to learn, than to contemplate."

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Do you mean that when you insert dates in two textboxes then you want the difference between two dates which will exclude an holiday or saturday and sunday ???
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Hi Kool,

    hmmm..something like that.. But I will use the NETWORKDAYS function NETWORKDAYS(start_date,end_date,holidays)... and I will apply the DateDiff on the DatePicker.. So in the computation it will not include holidays..
    Last edited by allankevin; Oct 29th, 2008 at 09:09 AM.
    "In order to improve the mind, we ought less to learn, than to contemplate."

  11. #11
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    There are few complications here...

    1) NETWORKDAYS will not work till the time Analysis Toolpack is installed
    2) The syntax is NETWORKDAYS(start_date,end_date,holidays) where Holidays is an optional range of one or more dates to exclude from the working calendar, such as state and federal holidays and floating holidays. The list can be either a range of cells that contains the dates or an array constant (array: Used to build single formulas that produce multiple results or that operate on a group of arguments that are arranged in rows and columns. An array range shares a common formula; an array constant is a group of constants used as an argument.) of the serial numbers that represent the dates. The challenge that I see here is that you will have to use the Excel workbook for this or then use an array.

    This is not easy and will be time consuming...

    Let me know if you are still interested...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  12. #12

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Hi Kool,

    It seems complicated but im still interested.. Im fine in using a Excel workbook.. I already add the Analysis Toolpack and Analysis Toolpack for VBA In the worksheet but I cant find the NETWORKDAYS in VBA userform I tried looking in the Add-Ins but it is empty..
    Last edited by allankevin; Oct 30th, 2008 at 01:16 AM.
    "In order to improve the mind, we ought less to learn, than to contemplate."

  13. #13

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [Excel] How to subtract Date from Userform

    Code:
    Private Sub DTPicker2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim Holidays As Range
    
    Set Holidays = Sheet1.[A2:A10]
    
    Me.txt2.Value = _
        Application.WorksheetFunction.Networkdays(DTPicker1.Value, DTPicker2.Value, Holidays)
    
    End Sub
    I've come out to this code.. but i dont think there is a NETWROKDAYS function in my vb.. How can i install the analysistool pack in VBA..
    "In order to improve the mind, we ought less to learn, than to contemplate."

  14. #14

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [Excel] How to subtract Date from Userform

    Yehey! I got it! atpvbaen.xls and funcres is the key! Thanks Kool!
    Code:
    Function DaysDiff(StartDay As Date, EndDay As Date, Hols As Range)
    DaysDiff = networkdays(StartDay, EndDay, Hols)
    End Function
    
    Private Sub DTPicker2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
    Dim Holidays As Range
    
    Set Holidays = Sheet1.[A2:A3]
    txt2 = DaysDiff(DTPicker2.Value, DTPicker1.Value, Holidays)
    End Sub
    Last edited by allankevin; Oct 30th, 2008 at 02:18 AM.
    "In order to improve the mind, we ought less to learn, than to contemplate."

  15. #15
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: [Excel] How to subtract Date from Userform

    sorry just came in...

    yup... atpvbaen.xls is the key
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  16. #16

    Thread Starter
    Addicted Member allankevin's Avatar
    Join Date
    Jul 2008
    Posts
    173

    Re: [RESOLVED] [Excel] How to subtract Date from Userform

    Thanks again.. I got my idea in your datediff almost the same..just need to set an array for the list of holidays in the worksheet.. I thought i can use the worksheetfunction.networkdays..
    "In order to improve the mind, we ought less to learn, than to contemplate."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width