Results 1 to 5 of 5

Thread: [resolved] erasing last 2 characters of a cell value

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49

    [resolved] erasing last 2 characters of a cell value

    Could someone please code me an algorythm to erase the last 2 characters in a cell value?

    EX: cell A1 has this: "hello world". I want cell A1 to have "hello wor"

    How would I go about doing this? Snippets would be nice.

    Thanks
    Last edited by LodBot; Nov 7th, 2003 at 04:48 PM.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Use the Mid and Len functions like so...
    VB Code:
    1. A1 = Mid(A1, 1, Len(A1) - 2)
    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

    Thread Starter
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    VB Code:
    1. rowCount = 2
    2.     While (Range("C" & rowCount).Value <> "")
    3.         Range("C" & rowCount).Value = Mid(Range("C" & rowCount).Value, 1, (Len(Range("C" & rowCount).Value) - 2))
    4.         rowCount = rowCount + 1
    5.     Wend

    When I run this code, I get an error saying "Run Time Error '5': Invalid procedure call or argument"

    I'm coding in Excel if it matters.

    Thanks

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    It looks like you may need to trap for when the cell only has 1 or
    2 characters. When you subtract 2 from 1 or 2 you get a negative
    number. Invalid Procedure call or argument.
    VB Code:
    1. rowCount = 2
    2. While (Range("C" & rowCount).Value <> "")
    3.     If Range("C" & rowCount).Value > 2 Then
    4.         Range("C" & rowCount).Value = Mid(Range("C" & rowCount).Value, 1, (Len(Range("C" & rowCount).Value) - 2))
    5.     Endif
    6.     rowCount = rowCount + 1
    7. Wend
    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
    Member
    Join Date
    Jul 2003
    Location
    Los Angeles, CA
    Posts
    49
    problem solved

    Thanks for the quick reply

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