View Full Version : [SOLVED:] Copy second last character from cell string
JinkyJulie
04-02-2015, 12:53 PM
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
gmayor
04-03-2015, 02:48 AM
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
JinkyJulie
04-07-2015, 12:37 PM
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.
gmayor
04-07-2015, 09:48 PM
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.
JinkyJulie
04-08-2015, 07:57 AM
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 :hi:
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.