Consulting

Results 1 to 5 of 5

Thread: Copy second last character from cell string

  1. #1

    Copy second last character from cell string

    Hi again,

    I have search and search and try and try but I can not seem to get this simple thing.

    I just want to copy the second last (from end) from a string in a cell for use later in the macro. example abcdefXg (copy the X)

    I must be missing something.

    Thanks for your help and patience.

    JJ

  2. #2
    Given that this is a Word forum I assume you must be talking about a Word table cell? That being the case it is fairly straightforward top grab a character from the cell.

    You haven't said which table of which cell, but the following should get you started:

    Dim oCell As Range
    Dim strCharacter As String
        'Set a range to the required cell
        Set oCell = ActiveDocument.Tables(1).Cell(1, 1).Range 'Row1, Column1
        'Remove the cell end character from the range
        oCell.End = oCell.End - 1
        'Get the left most of the last two characters
        strCharacter = Left(Right(oCell.Text, 2), 1)
        'Do what you want with strCharacter
        MsgBox strCharacter
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Hello Graham,

    Yes thank you for assuming correctly. I will try to be more clear next time.

    I am having some difficulty adapting what you have given me... probably my fault, I still think I am missing something...



         
    ' Get the information from cell
    Set dTextCell = oDataTable.Cell(Row:=3, Column:=3)
    Set dTextRange = dTextCell.Range
    
    ' Extract 2nd last character from cell and place it in new table/cell
    ActiveDocument.Tables(14).Cell(2, 3).Range.Text = Left(Right(dTextCell.Text, 2), 1)
    The macro stops... Perhaps I am doing too much at once?

    I hope I am clear enough... If not I will try to explain better.

  4. #4
    The most obvious thing you are missing, based on the snippet of code you have posted is the removal of the cell end character from the range

    ' Get the information from cell
    Set dTextCell = oDataTable.Cell(Row:=3, Column:=3)
    Set dTextRange = dTextCell.Range
    
    'Remove the cell end character from the range
    dTextRange.End = dTextRange.End - 1
    
    ' Extract 2nd last character from cell and place it in new table/cell
    ActiveDocument.Tables(14).Cell(2, 3).Range.Text = Left(Right(dTextCell.Text, 2), 1)
    The code assumes that you are addressing the correct tables i.e. oDataTable and ActiveDocument.Tables(14). I would need to see the document(s) and the rest of your code to check that.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    Graham,

    Thank you again... I was correct in thinking I was doing too much, or at least too little... The issue was with

    ActiveDocument.Tables(14).Cell(2, 3).Range.Text = Left(Right(dTextCell.Text, 2), 1)
    When I expanded it and did it in TWO Steps, the code worked

       
     ActiveDocument.Tables(14).Cell(2, 3).Range.Text = dTextRange.Text
    ActiveDocument.Tables(14).Cell(2, 3).Range.Text = Left(Right(dTextRange.Text, 2), 1)
    Thank you so very much for your help... as always...

    JJ

Posting Permissions

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