#1
|
|||
|
|||
Row Number of Selected Cell in Table
Hi
I am trying to write a VBA program to return the row number of the current selection. This would allow me to copy and paste the entire row which is found searching for a key word. The code I have written so far seems to return the cell that the cursor was in before the code is run, and not the cell selected using Find. Many thanks in advanced for any help with this, Alex Sub RowNumber() Dim wApp As Word.Application Dim wDoc As Word.Document Dim i As Integer 'OPEN WORD IF NOT OPEN Dim wObj As Word.Application On Error Resume Next ' Get existing instance of Word if it exists. Set wApp = GetObject(, "Word.Application") If Err <> 0 Then ' If GetObject fails, then use CreateObject instead. Set wApp = CreateObject("Word.Application") End If wApp.Visible = True Set wDoc = wApp.Documents.Open("C:\Users\alex.olczak\Document s\Proposal Automation\ERS Proposal\ERS Proposal Template Edit\PeopleParagraphMacro\TableTest.docx") 'wDoc.Application.Selection.Find.ClearFormatting wDoc.Application.Selection.Find.Replacement.ClearF ormatting wDoc.Application.Selection.Find.Replacement.Highli ght = True With wDoc.Application.Selection.Find .Text = "H" .Replacement.Text = "H" .Forward = Flase .Wrap = wdFindContinue .Format = True .MatchCase = False .MatchWholeWord = False .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False End With wDoc.Application.Selection.Find.Execute Replace:=wdReplaceAll wDoc.Application.Selection.Find.Execute wDoc.Application.Options.DefaultHighlightColorInde x = wdYellow a = wDoc.Application.Selection.Range.Information(wdEnd OfRangeRowNumber) a = wDoc.Application.Selection.Information(wdEndOfRang eRowNumber) 'wDoc.Application.Selection.GoTo MsgBox a End Sub |
#2
|
||||
|
||||
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
||||
|
||||
If this is Word VBA, the you don't have to get or create the Word object. You are already in Word. If the aim is to find the row number of the first instance of a cell containing the upper case letter 'H', then
Code:
Sub Macro1() Dim wDoc As Document Dim oRng As Range Dim i As Integer Set wDoc = wApp.Documents.Open("C:\Users\alex.olczak\Document s\Proposal Automation\ERS Proposal\ERS Proposal Template Edit\PeopleParagraphMacro\TableTest.docx") Set oRng = wDoc.Range With oRng.Find Do While .Execute(FindText:="H", MatchCase:=True) If oRng.Information(wdWithInTable) Then oRng.HighlightColorIndex = wdYellow i = oRng.Rows(1).Index 'This line is not required to copy the row MsgBox i 'This line is not required to copy the row oRng.Rows(1).Range.Copy 'The row is copied to the clipboard Exit Do 'Find only the first instance End If Loop End With 'Do something with the copied row e.g. Set oRng = wDoc.Range 'set a range to the document oRng.Collapse 0 'collapse the range to its end oRng.Paste 'Paste the found row at then end of the document lbl_Exit: Exit Sub End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
Tags |
find, table, vba |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Data validation,force cell to be filed with number if respective cell is not empty | nicholes | Excel Programming | 0 | 08-01-2015 09:08 AM |
Entering a number in a cell causes it to fill with the color of a nearby cell | Bill Martz | Excel | 1 | 04-23-2015 07:57 PM |
Setting a particular table cell background color when an option button is selected in Word 2007 | a888 | Word VBA | 11 | 03-25-2015 05:33 AM |
Pivot Table Question- Can the table display in the order the boxes were checked/selected? | blackjack | Excel | 0 | 09-13-2014 08:57 PM |
Heading row disappears from table styles when pasted table is selected | andrewballem | Word Tables | 2 | 11-12-2013 05:18 AM |