Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

upgrading short code that deletes empty rows in excel

Posted on 2008-06-11
4
Medium Priority
?
315 Views
Last Modified: 2010-04-21
hi,
i have the attached code, that currently deletes every empty row in a word table (also rows with only numbering bullet). i need to upgrade this code so that also if the row cells containes only spaces (" "), it should also be deletd.
appreciate your kind help :)
Sub DelRows()
    Dim tbl As Table
    Dim rw As Row
    
    Set tbl = ActiveDocument.Tables(1)
    For Each rw In tbl.Rows
        If rw.Range.Characters.Count - rw.Range.Cells.Count = 1 Then
            rw.Delete
        End If
    Next rw
End Sub

Open in new window

0
Comment
Question by:Or_A
  • 2
4 Comments
 
LVL 5

Expert Comment

by:danengle
ID: 21763261
I'm not familiar with the syntax for acquiring the actual cell content so am leaving that to you, but the logic should work:


    For Each rw In tbl.Rows
        If rw.Range.Characters.Count - rw.Range.Cells.Count = 1 Then
            rw.Delete
' replace 
        elseif replace(rw.<whatevertoretrievecontent>, " ", "") = "" then
            rw.Delete
        End If
    Next rw

Open in new window

0
 
LVL 5

Expert Comment

by:danengle
ID: 21763295
Actually, upon further inspection, this deletes just empty cells?  based on the sub you posted, it appears it will delete everything.

Also, for the first If, couldn't it just use:
If rw.Range.Characters.Count = 0 then rw.delete

?
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 2000 total points
ID: 21764553
Not sure why this is in Zone VBScript. If you really are using VBScript, these procedures need a bit of tweaking

Similar, but slightly different technique.
Sub DelRows2()
    Dim tbl As Table
    Dim rw As Row
    
    Set tbl = ActiveDocument.Tables(1)
    For Each rw In tbl.Rows
        If Len(Replace$(rw.Range.Text, " ", "")) = (rw.Cells.Count + 1) * 2 Then
            rw.Delete
        End If
    Next rw
End Sub

Open in new window

0
 

Author Closing Comment

by:Or_A
ID: 31466297
worked great, thanks!
but i didnt understand your question, what i've written is not in VB?
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Lost Word File? Eagerly, need it back? Read ahead; this File Recovery guide is for you.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question