Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-02-2011, 03:19 PM
silverspr silverspr is offline Word Macro to search all tables Windows 7 64bit Word Macro to search all tables Office 2010 64bit
Novice
Word Macro to search all tables
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Post Word Macro to search all tables

Hello
I have four (4) tables in word, each with 9 columns with varying rows. In all instances the last row, column holds a field that sums values above it. I would like to have a macro that searches the last column and resets the values to zero (0) except for the last row. I've tried creating said macro, however it only runs if the cursor is positioned in a table and the macro runs within that table, I can't get the macro to programmatically move to the next table using the table index:


Dim C As Cell
Dim LastCell As Long
Dim i As Long

i = 1
For i = 1 To 4
Set tbl = ActiveDocument.Tables(i)
LastCell = ActiveDocument.Tables(i).Rows.Count - 1
Selection.SetRange _


Start:=Selection.Tables(i).Cell(2, 9).Range.Start, _
End:=Selection.Tables(i).Cell(LastCell, 9).Range.End
With Selection
For Each C In Selection.Cells
C.Range.Text = "0"
Next C
End With
Next i

Your help is appreciated, thx
Reply With Quote
  #2  
Old 04-02-2011, 05:51 PM
macropod's Avatar
macropod macropod is offline Word Macro to search all tables Windows 7 32bit Word Macro to search all tables Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi silverspr,

Try:
Code:
Sub Demo()
Dim oTbl As Table, i As Integer, j As Integer
For Each oTbl In ActiveDocument.Tables
  With oTbl
    j = .Columns.Count
    For i = 1 To .Rows.Count - 1
      .Cell(i, j).Range.Text = "0"
    Next
  End With
Next
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-02-2011, 10:16 PM
silverspr silverspr is offline Word Macro to search all tables Windows 7 64bit Word Macro to search all tables Office 2010 64bit
Novice
Word Macro to search all tables
 
Join Date: Apr 2011
Posts: 24
silverspr is on a distinguished road
Default

Thanks Paul,
Works wonderfully....and much more sophisticated than my solution. One more small problem...although I said I only had 4 tables, there are actually 6, but I only want the macro to edit the first 4. How to control this programmatically...thats why I thought I could use the table index....or was my intent.

thanks
dale
Reply With Quote
  #4  
Old 04-02-2011, 11:20 PM
macropod's Avatar
macropod macropod is offline Word Macro to search all tables Windows 7 32bit Word Macro to search all tables Office 2000
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,963
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi Dale,

Try:
Code:
Sub Demo()
Dim i As Integer, j As Integer, k As Integer
With ActiveDocument
  For i = 1 To 4
    With .Tables(i)
      j = .Columns.Count
      For k = 1 To .Rows.Count - 1
        .Cell(k, j).Range.Text = "0"
      Next
    End With
  Next
End With
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Tags
column, reset, table



Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding a google search to Word alexb123 Word 0 03-23-2011 04:30 PM
Search Problems in MS Word LoganB Word 0 09-03-2010 06:49 AM
Word Macro to search all tables I need a macro that removes background of many tables masa57 Word VBA 2 05-08-2010 07:34 AM
Search and Replace - Clear Search box JostClan Word 1 05-04-2010 08:46 PM
Word Macro to search all tables How to search for email addresses lacomputech Word 2 12-10-2009 08:33 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 02:31 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft