Consulting

Results 1 to 10 of 10

Thread: character adding

  1. #1
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location

    character adding

    hi,

    there are numbers in different cells. how can I add a character to the beginning of these numbers easily.

    for example

    7665m007
    7874dt33
    2011kf85

    will be
    d7665m007
    d7874dt33
    d2011kf85
    OSMAN

  2. #2
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    paste the code in module & run
    (pl change the sheet & range reference accordingly)

    [VBA]Sub addcharacter()
    Application.ScreenUpdating = False
    Dim iLastRow As Long
    Dim i As Long
    Dim addchar As String
    addchar = "d" '--Change as per requirement
    With Sheet1 '--Change as per requirement
    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = iLastRow To 1 Step -1
    .Cells(i, "A") = addchar & .Cells(i, "A")

    Next i

    End With
    Range("a1").Select
    Application.ScreenUpdating = True
    End Sub[/VBA]
    Always Mark your Thread as Solved the moment u got acceptable reply (located under Thread tools)
    Practice this & save time of others in thinking for unsolved thread

  3. #3
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location

    character addding

    Hi,

    I tried but I get object required error. can you please check from the attached file
    OSMAN

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    In a spare column enter ="d" & A1 and copy down. Copy/PasteSpecial Values over the original data and clear the helper column.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    For i = iLastRow To 1 Step -1
    No need to step backwards as you are not inserting/deleting rows.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Regular aoc's Avatar
    Joined
    Apr 2007
    Location
    Istanbul
    Posts
    90
    Location

    character adding

    hi,

    if I want to add * symbol after 4 and delete k in g1204k

    it will be g1204*. how can I do ?
    OSMAN

  7. #7
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Check out Replace in Help.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  8. #8
    VBAX Mentor anandbohra's Avatar
    Joined
    May 2007
    Location
    Mumbai
    Posts
    313
    Location
    Quote Originally Posted by aoc
    hi,

    if I want to add * symbol after 4 and delete k in g1204k

    it will be g1204*. how can I do ?
    Just replace existing line with the below one

    [VBA].Cells(i, "A") = addchar & Left(.Cells(i, "A"), Len(.Cells(i, "A")) - 1) & "*"[/VBA]

    the above code first delete last 1 character then add given character on starting & asterisk (*) at the end
    Always Mark your Thread as Solved the moment u got acceptable reply (located under Thread tools)
    Practice this & save time of others in thinking for unsolved thread

  9. #9
    VBAX Contributor
    Joined
    Jul 2004
    Location
    Gurgaon, India
    Posts
    148
    Location
    Quote Originally Posted by anandbohra
    paste the code in module & run
    (pl change the sheet & range reference accordingly)

    [vba]Sub addcharacter()
    Application.ScreenUpdating = False
    Dim iLastRow As Long
    Dim i As Long
    Dim addchar As String
    addchar = "d" '--Change as per requirement
    With Sheet1 '--Change as per requirement
    iLastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = iLastRow To 1 Step -1
    .Cells(i, "A") = addchar & .Cells(i, "A")

    Next i

    End With
    Range("a1").Select
    Application.ScreenUpdating = True
    End Sub[/vba]
    You could avoid looping. See

    [vba]Sub kTest()
    Dim r As Range, f As String
    Set r = Range("a1", Range("a" & Rows.Count).End(xlUp))
    f = "=""D""&" & r.Address & ""
    With r
    .Value = Evaluate(f)
    End With
    End Sub[/vba]

    HTH

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Quote Originally Posted by Krishna Kumar
    You could avoid looping. See

    [vba]Sub kTest()
    Dim r As Range, f As String
    Set r = Range("a1", Range("a" & Rows.Count).End(xlUp))
    f = "=""D""&" & r.Address & ""
    With r
    .Value = Evaluate(f)
    End With
    End Sub[/vba]

    HTH
    Neat
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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