Results 1 to 14 of 14

Thread: how to convert this function from excel to vb???

  1. #1

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    how to convert this function from excel to vb???

    hello

    i have been looking around so long just to know the correct way to convert this function from excel to vb

    =TEXT(A45/24,"hh:mm")

    anyone can help???

    this function convert the number to hours and minutes

    thanks

  2. #2
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: how to convert this function from excel to vb???

    VB classic :

    Num = 8.5 '-- 8 and a half hours
    x = Format(CDate(Num/24),"hh:nn") '-- 08:30

    Same as the Excel formula above, this function only works correctly for Num >= 0 and Num < 24

    Edit: CDate may not required: x = Format(Num/24,"hh:nn") '-- 08:30
    Last edited by anhn; Jul 8th, 2008 at 11:16 PM.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to convert this function from excel to vb???

    vb.net Code:
    1. Dim ts As TimeSpan = TimeSpan.FromDays(dayCount)
    2. Dim str As String = String.Format("{0:D2}:{1:D2}", _
    3.                                   CInt(Math.Floor(ts.TotalHours)), _
    4.                                   ts.Minutes)
    5.  
    6. MessageBox.Show(str)
    There are numerous ways to create a TimeSpan. If you don't have a number of days then check the documentation for the appropriate way given the data you have.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by anhn
    this function only works correctly for Num >= 0 and Num < 24
    Which is why you should use a TimeSpan rather than a DateTime. A DateTime represents a moment in time, while a TimeSpan represents a time period.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by jmcilhinney
    Which is why you should use a TimeSpan rather than a DateTime. A DateTime represents a moment in time, while a TimeSpan represents a time period.
    No argue, but the function I provided
    x = Format(Num/24,"hh:nn")
    is equivalent exactly with the original Excel formula:
    =TEXT(A45/24,"hh:mm")
    No more or less.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  6. #6

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by anhn
    VB classic :

    Num = 8.5 '-- 8 and a half hours
    x = Format(CDate(Num/24),"hh:nn") '-- 08:30

    Same as the Excel formula above, this function only works correctly for Num >= 0 and Num < 24

    Edit: CDate may not required: x = Format(Num/24,"hh:nn") '-- 08:30
    i'm useing vb.net 2003

    the code is about the same i tried this one

    Dim num As Decimal = 2.5
    Dim x As Date
    x = Format(System.DateTime.FromOADate(num / 24), "hh:mm")
    y4.Text = x

    but what the answer was is = 02:30:00 AM
    i only need the hours and the mins how am i ganna remove the last part??

  7. #7

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by jmcilhinney
    vb.net Code:
    1. Dim ts As TimeSpan = TimeSpan.FromDays(dayCount)
    2. Dim str As String = String.Format("{0:D2}:{1:D2}", _
    3.                                   CInt(Math.Floor(ts.TotalHours)), _
    4.                                   ts.Minutes)
    5.  
    6. MessageBox.Show(str)
    There are numerous ways to create a TimeSpan. If you don't have a number of days then check the documentation for the appropriate way given the data you have.
    i think thats far too complicated 4 me to understand sorry

  8. #8

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by anhn
    No argue, but the function I provided
    x = Format(Num/24,"hh:nn")
    is equivalent exactly with the original Excel formula:
    =TEXT(A45/24,"hh:mm")
    No more or less.
    yeah it works fine with vb6
    but i'm usein vb.net 2003

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by snakegaer
    i'm useing vb.net 2003

    the code is about the same i tried this one

    Dim num As Decimal = 2.5
    Dim x As Date
    x = Format(System.DateTime.FromOADate(num / 24), "hh:mm")
    y4.Text = x

    but what the answer was is = 02:30:00 AM
    i only need the hours and the mins how am i ganna remove the last part??
    The Format() function returns a String, not Date.

    y4.Text = Format(System.DateTime.FromOADate(num / 24), "hh:mm")
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to convert this function from excel to vb???

    It's pretty simple stuff really. Create a TimeSpan and then use it to create a formatted string containing hours and minutes. Every method in that code is in the MSDN Library and you can simply click each one and press F1 to go straight to the appropriate topic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by anhn
    The Format() function returns a String, not Date.

    y4.Text = Format(System.DateTime.FromOADate(num / 24), "hh:mm")
    awsom
    thanx a lot

  12. #12

    Thread Starter
    Lively Member snakegaer's Avatar
    Join Date
    Jul 2007
    Posts
    80

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by jmcilhinney
    It's pretty simple stuff really. Create a TimeSpan and then use it to create a formatted string containing hours and minutes. Every method in that code is in the MSDN Library and you can simply click each one and press F1 to go straight to the appropriate topic.
    there is no msdn library in my cp i tried to install it but didn't work out

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to convert this function from excel to vb???

    If you're going to go that way at least do it the .NET way:
    vb.net Code:
    1. y4.Text = Date.FromOADate(num / 24).ToString("HH:mm")
    Note the upper case "HH" too, or else you'll get anything over 12 hours reported incrrectly.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: how to convert this function from excel to vb???

    Quote Originally Posted by snakegaer
    there is no msdn library in my cp i tried to install it but didn't work out
    www.msdn.com
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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