Consulting

Results 1 to 14 of 14

Thread: VBA to Insert a character in a specific position in a cell

  1. #1
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location

    VBA to Insert a character in a specific position in a cell

    Trying to insert a "," in the 7th position from the right, if a cell length is > 6 AND the Cell contains a " . "

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,446
    Location
    Off the top
    If Len(cell).Value) > 6 And Instr(cell,".") > 0 Then
    
        cell.Value = Left$(cell.Value, 6) & "," & Mid$(cell.Value, 7,255)
    End If
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    Thanks Bob Phillips :

    But This is putting the "," 7 places from the Left, not the 7th position from the Right.

  4. #4
    Is this what you mean Bob meant?
    = Left$(c.Value, Len(c) - 6) & "," & Mid$(c, Len(c) - 5, 6)
    Last edited by jolivanes; 01-28-2021 at 11:39 PM.

  5. #5
    VBAX Mentor
    Joined
    Jan 2008
    Posts
    424
    Location
    Thanks jolivanes;

    This solution is what worked.

       If Len(cell.Value) > 6 And InStr(cell, ".") > 0 Then
                cell.Value = Left$(cell.Value, Len(cell) - 6) & "," & Mid$(cell, Len(cell) - 5, 6)     
        End If
    Thanks for the efforts of all who contributed.

  6. #6
    snb
    Guest
    Sufficient:

    If (Len(cl) > 6)*InStr(cl, ".") Then cl = Left(cl, Len(cl) - 6) & "," & Mid(cl, Len(cl) - 5)
    Avoid variable names that can interfere with VBA names.
    Last edited by snb; 01-29-2021 at 09:34 AM.

  7. #7
    Len(cl)

  8. #8
    snb
    Guest
    correct.

  9. #9
    as usual!

  10. #10
    snb
    Guest
    snoever

  11. #11
    As ge oe čige nie kietelt dan laŕchte nooit!

  12. #12
    snb
    Guest
    Kruikezeikers ?

  13. #13
    Parel vh zuiden aka 't kielegat

  14. #14
    snb
    Guest
    compris !

Posting Permissions

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