#1
|
|||
|
|||
VBA Find last cell in Column 2 of Word table
I've tried researching on Google on how to find the last cell in a column of a Word table but have been unable to find anything.
When I run my TextToTable code, I would like to change the semi colon in the very last cell of Column 2 to a period/full stop but unsure how to add this to my existing code. Can anyone help at all? Capture.JPG Code:
Sub TextToTables() Dim rRng As Range, rCell As Range 'Convert text to table Dim oBorder As Border Dim oTbl As Table Dim i As Integer Application.ScreenUpdating = False Set rRng = ActiveDocument.Range Set oTbl = rRng.ConvertToTable(Separator:=wdSeparateByTabs, NumColumns:=2, AutoFitBehavior:=wdAutoFitFixed) With oTbl .Style = "Table Grid" .ApplyStyleHeadingRows = True .ApplyStyleLastRow = False .ApplyStyleFirstColumn = True .ApplyStyleLastColumn = False .Columns.PreferredWidth = InchesToPoints(2.7) .Columns(2).PreferredWidth = InchesToPoints(3.63) For Each oBorder In .Borders oBorder.LineStyle = wdLineStyleNone Next oBorder Call DPU_ApplyDefinitionStylesToTable For i = 1 To .Rows.count 'check each row Set rCell = .Cell(i, 1).Range 'set a range to the cells in column 1 rCell.Style = "DefBold" 'apply the style to the range Set rCell = .Cell(i, 2).Range 'set a range to the cells in column 2 rCell.Collapse 1 'collapse the range to its start rCell.MoveEndWhile "means,:" 'move the end of the range to include any of these characters If rCell.text Like "means*" Then 'if that range starts with 'means' rCell.MoveEndWhile Chr(32) 'move the end of the range to include any following spaces rCell.text = "" 'and empty the range End If Next i End With Application.ScreenUpdating = True Set rRng = Nothing Set oTbl = Nothing Set rCell = Nothing Set oBorder = Nothing End Sub |
#2
|
|||
|
|||
In the code block after
Code:
Set rCell = .Cell(i, 2).Range Code:
If i = .Rows.Count Then |
#3
|
|||
|
|||
VBA Find last cell in Column 2 of Word table
Hi, apologises but don't think I understand - does this line of code find only the very last cell in Column 2 - I only need to change the very last semi-colon not all of them?
|
#4
|
||||
|
||||
In your example the last cell in column 2 is also the last cell in the table. Looking at your previous question, I'm going to assume that is always the case. I'm also going to assume there are no trailing spaces on that last cell so this simple code would work.
Code:
Sub CleanseTheColon() Dim aTbl As Table, aRng As Range For Each aTbl In ActiveDocument.Tables Set aRng = aTbl.Range aRng.End = aRng.End - 2 If aRng.Characters.Last = ";" Then aRng.Characters.Last = "." Next aTbl End Sub
__________________
Andrew Lockton Chrysalis Design, Melbourne Australia |
#5
|
|||
|
|||
VBA Find last cell in Column 2 of Word table
Absolutely perfect Andrew, thank you so much
|
#6
|
|||
|
|||
Quote:
As you are using i as the row number the statement Code:
If i = .Rows.Count Then |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
What formula can I use to find the lowest cell in a column that has a value in it? | EddyWD | Excel | 3 | 04-20-2016 09:29 PM |
VBA Word - Find Specific Table - Prepend & Append Data to Each Cell | jc491 | Word VBA | 3 | 12-02-2015 09:48 PM |
VBA Table – Search All Tables - Find & Replace Text in Table Cell With Specific Background Color | jc491 | Word VBA | 8 | 09-30-2015 06:10 AM |
Find text within cell and return column and row title next to the name on a new sheet. | tanyabowring@live.co.uk | Excel Programming | 2 | 03-26-2015 01:48 AM |
How do I reference a merged cell in a multi column & row table in MS Word ('03')? | jihanemo | Word Tables | 0 | 03-18-2009 08:33 AM |