Results 1 to 8 of 8

Thread: function to determine if the variable is "-"

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    function to determine if the variable is "-"

    Hello

    is there a function which would enable me to determine if the contents of a variable is say "-"
    Last edited by vb_student; Sep 1st, 2005 at 11:05 AM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: function to determine if the variable is "-"

    Yes, you can use Instr function to test for it and return the index position of it in the variable string.
    VB Code:
    1. Dim str As String
    2. Dim iRet As Integer
    3. str = "VB-Office Guru"
    4. iRet = Instr(1, str, "-")
    5. If iRet > 0 Then
    6.     MsgBox "Contains a '-' at position: " & iRet ' = 3
    7. Else
    8.     MsgBox "does not contain a '-'"
    9. End If
    Last edited by RobDog888; Aug 23rd, 2005 at 12:37 PM.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: function to determine if the variable is "-"

    Minor correction ... the opening quote is required ...
    str = "VB-Office Guru"

    "InStr" is nice in that it automatically casts a number to a string, so you can detect negative signs (-) in numbers as well as hyphens (-) in text strings !!!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: function to determine if the variable is "-"

    What type-o? You didn't see nut'n.

    Thanks.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: function to determine if the variable is "-"

    i meant do a comparison of a single character to check if the character is '-' or not.
    i guess i could do something like

    VB Code:
    1. str = '-'
    2. if str='-' then
    3. MsgBox "it is a -"
    4. else
    5. MsgBox "it is not a -"

  6. #6
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: function to determine if the variable is "-"

    You can certainly do that, but you need an "End If" at the end. You can still use the "Instr" for the a character match in any string if you want ... but it can't tell you if the string happens to be longer than 1 character.
    Last edited by Webtest; Sep 1st, 2005 at 02:45 PM. Reason: re-worded statement
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

  7. #7
    Fanatic Member dannymking's Avatar
    Join Date
    Jul 2005
    Location
    Darlington, North East UK
    Posts
    677

    Re: function to determine if the variable is "-"

    If needs be you can also scan through a string character by character to determine what it is (loop around the length using MID), or if you are needing to replace a certain character with another then the REPLACE function will help.
    Danny

    Never Think Impossible

    If you find my answer helpful then please add to my reputation

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2005
    Posts
    1,069

    Re: function to determine if the variable is "-"

    thanks guys

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