Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-09-2012, 06:16 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default Macro for MS Word

I was wondering if someone can provide macro for ms word. I want to do the following:

if there is a row such as:



Cheryl 0 1 0 0 1
Chile 0 0 0 0 0

Word should delete the row with all zeros (except in the first column of that row).

So Row Chile should be deleted completely.
Reply With Quote
  #2  
Old 04-09-2012, 06:38 PM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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 umeshbanga,

Are these 'rows' in a Word table? If so, how many tables are there in the document? If there is more than one table, which tables do you want processed?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #3  
Old 04-09-2012, 06:43 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Hi Paul,

Yes these rows are in Word Table.

There are around 5-20 tables in MS Word. every time the number of tables can change, but the maximum table could go upto 20.

I want to process all tables.

Regards

Umesh Banga
Reply With Quote
  #4  
Old 04-09-2012, 08:03 PM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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

Your requirements are ambiguous:
Quote:
Cheryl 0 1 0 0 1
Chile 0 0 0 0 0

Word should delete the row with all zeros (except in the first column of that row).
So Row Chile should be deleted completely.
So, do you want to end up with:
Cheryl 0 1 0 0 1
Chile
or:
Cheryl 0 1 0 0 1
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #5  
Old 04-09-2012, 08:15 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

apologies, If I didnot make myself clear enough.

I should end up with Cheryl 0 1 0 0 1 row only.

Regards
Reply With Quote
  #6  
Old 04-09-2012, 09:15 PM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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

Try:
Code:
Sub CleanTables()
Application.ScreenUpdating = False
Dim oTbl As Table, Rng As Range, i As Long, j As Long, StrTmp As String
With ActiveDocument
  For Each oTbl In .Tables
    With oTbl
      j = .Columns.Count
      For i = .Rows.Count To 1 Step -1
        With .Rows(i)
          Set Rng = .Range
          Rng.Start = .Cells(2).Range.Start
          StrTmp = Replace(Replace(Replace(Replace(Rng.Text, Chr(7), ""), vbCr, ""), " ", ""), "0", "")
          If StrTmp = "" Then .Delete
        End With
      Next
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #7  
Old 04-09-2012, 09:50 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Hey mate,

it does not work, it says :

Rng.Start = .Cells(2).Range.Start ----------

on this row it says
Run time error 5941, the requested member of the collection does not exist.
Reply With Quote
  #8  
Old 04-09-2012, 10:50 PM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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 umeshbanga,

I coded the macro in the expectation that your names & numbers were in different cells on the same row. The error you're getting suggests there's only one cell per row. Is that correct?
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #9  
Old 04-09-2012, 11:11 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

no. They all are in different cells. So it is table with 6 rows and 5 columns (means row is divided in 5 cells).
Reply With Quote
  #10  
Old 04-09-2012, 11:49 PM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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

The error, though, suggests the macro is encountering a row with only one cell.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #11  
Old 04-09-2012, 11:54 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

can there be any other reason.
Reply With Quote
  #12  
Old 04-10-2012, 12:00 AM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Its works man.

I know what happened. I have got a document, in which there are heaps of tables. There are tables ranging from ------- one row and only one column (only one cell in a row), and to 20 rows and 20 columns.

The moment this code find the table with one cell only, its stops working and throughs an error.

Do you think there is a way around that, I mean - in case if there is a table with one cell only, then the code just ignores it and look for another table, and keeps doing it untill it reaches the end of the document.
Reply With Quote
  #13  
Old 04-10-2012, 12:01 AM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

BTW thanks heaps, you are genious.
Reply With Quote
  #14  
Old 04-10-2012, 12:38 AM
macropod's Avatar
macropod macropod is offline Macro for MS Word Windows 7 64bit Macro for MS Word Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,373
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

Try:
Code:
Sub CleanTables()
Application.ScreenUpdating = False
Dim oTbl As Table, Rng As Range, i As Long, j As Long, StrTmp As String
With ActiveDocument
  For Each oTbl In .Tables
    With oTbl
      j = .Columns.Count
      For i = .Rows.Count To 1 Step -1
        With .Rows(i)
          If .Cells.Count > 1 Then
            Set Rng = .Range
            Rng.Start = .Cells(2).Range.Start
            StrTmp = Replace(Replace(Replace(Replace(Rng.Text, Chr(7), ""), vbCr, ""), " ", ""), "0", "")
            If StrTmp = "" Then .Delete
          End If
        End With
      Next
    End With
  Next
End With
Application.ScreenUpdating = True
End Sub
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #15  
Old 04-10-2012, 04:18 PM
ubns ubns is offline Macro for MS Word Windows 7 32bit Macro for MS Word Office 2010 32bit
Competent Performer
Macro for MS Word
 
Join Date: Apr 2012
Posts: 177
ubns is on a distinguished road
Default

Hi Paul,

Thanks for this extensive code.

When I run this code: its gives the run time error - 5991 - cannot access individual rows in this collection because the table has vertically merged cells.

PS - When I click on debug --- it stop to execute code (as expected) and highlights the sentence in the code (as expected again), indicating the possible source of error.

However, when I click on End - it delete the rows with zero.

So with my limited knowledge may be there is a need to include exception handling or something. Anyways I am just guessing.

Mate also I wanted to ask you, which is the best source for beginners to learn all this. You seem to have so much knowledge, its awesome and quite amazing.

Do you also have forums for excel?
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro in word khalfenadeem Word 1 04-11-2011 04:35 PM
Macro for MS Word Help with Word macro please Philcraig69 Word VBA 2 01-21-2011 04:19 PM
Print Macro in MS Word steve207 Word VBA 0 09-10-2010 02:11 AM
Word 2003 macro to Word 2007 to 2010 to... maruapo Word VBA 0 06-04-2010 03:43 PM
Word macro weezypenguin Word 0 02-25-2010 01:25 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:45 PM.


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