Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 01-05-2022, 04:26 AM
Atuck88 Atuck88 is offline deleting a row in word table if a certain number of cells are empty and/or contain a certain value Windows 10 deleting a row in word table if a certain number of cells are empty and/or contain a certain value Office 2019
Novice
deleting a row in word table if a certain number of cells are empty and/or contain a certain value
 
Join Date: Jan 2022
Posts: 2
Atuck88 is on a distinguished road
Default deleting a row in word table if a certain number of cells are empty and/or contain a certain value

I have this code (attached below) which will delete the rows in a word table if all the cell rows are empty.



However I have a table with eight columns, the first four of which will always contain text. The latter four contains data linked to an excel document.

The question is; how do I get the code to search through the latter four (data) columns and IF the columns are empty and/or contain a value (such as zero) THEN delete the row.

Any help will be much appreciated.

Thanks in advance



Code:
       Sub DeleteBlankRowsAndTablesInATable()
      Dim objCell As Cell
      Dim nRowIndex As Integer, nRows As Integer, nColumns As Integer, nColumnIndex As Integer
      Dim varCellEmpty As Boolean
     
      Application.ScreenUpdating = False
     
      If Selection.Information(wdWithInTable) = False Then
        MsgBox ("Put cursor inside a table first!")
        Exit Sub
      Else
        With Selection.Tables(1)
          nRows = .Rows.Count
          For nRowIndex = nRows To 1 Step -1
            varCellEmpty = True
            For Each objCell In .Rows(nRowIndex).Cells
              If Len(objCell.Range.Text) > 2 Then
                varCellEmpty = False
                Exit For
              End If
            Next objCell
              If varCellEmpty = True Then
                .Rows(nRowIndex).Delete
              End If
          Next nRowIndex
     
    
        End With
      End If
    
      Set objCell = Nothing
    
      Application.ScreenUpdating = True
    End Sub
Reply With Quote
  #2  
Old 01-05-2022, 05:24 AM
gmayor's Avatar
gmayor gmayor is offline deleting a row in word table if a certain number of cells are empty and/or contain a certain value Windows 10 deleting a row in word table if a certain number of cells are empty and/or contain a certain value Office 2019
Expert
 
Join Date: Aug 2014
Posts: 3,829
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
Default

If all the cells in the relevant columns are empty or contain 0 the following will delete the row:
Code:
Sub DeleteBlankRowsAndTablesInATable()
Dim objCell As Range
Dim nRowIndex As Integer, nRows As Integer, nColumns As Integer, nColumnIndex As Integer
Dim varCellEmpty As Boolean

    Application.ScreenUpdating = False

    If Selection.Information(wdWithInTable) = False Then
        MsgBox ("Put cursor inside a table first!")
        Exit Sub
    Else
        With Selection.Tables(1)
            nRows = .Rows.Count
            For nRowIndex = nRows To 1 Step -1
                varCellEmpty = True
                For nColumns = 5 To .Columns.Count
                    Set objCell = .Rows(nRowIndex).Cells(nColumns).Range
                    objCell.End = objCell.End - 1
                    If Len(objCell) > 0 And Not objCell.Text = "0" Then
                        varCellEmpty = False
                        Exit For
                    End If
                Next nColumns
                If varCellEmpty = True Then .Rows(nRowIndex).Delete
            Next nRowIndex
        End With
    End If
    Set objCell = Nothing
    Application.ScreenUpdating = True
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 01-05-2022, 06:03 AM
gmayor's Avatar
gmayor gmayor is offline deleting a row in word table if a certain number of cells are empty and/or contain a certain value Windows 10 deleting a row in word table if a certain number of cells are empty and/or contain a certain value Office 2019
Expert
 
Join Date: Aug 2014
Posts: 3,829
gmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to beholdgmayor is a splendid one to behold
Default

Cross posted at deleting rows in word table if certain cells are empty or contain a specific value
Please cross post correctly!
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #4  
Old 01-06-2022, 01:47 AM
Atuck88 Atuck88 is offline deleting a row in word table if a certain number of cells are empty and/or contain a certain value Windows 10 deleting a row in word table if a certain number of cells are empty and/or contain a certain value Office 2019
Novice
deleting a row in word table if a certain number of cells are empty and/or contain a certain value
 
Join Date: Jan 2022
Posts: 2
Atuck88 is on a distinguished road
Default

That's amazing! thank you so much!!!

Sorry for cross posting. I didn't realise that was a thing. I'm new to all this. I shall refrain doing so in the future.

Thanks again. Your help is much appreciated.
Reply With Quote
Reply

Tags
vba, word table

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can i upload values of Non-Empty Cells Reference of a Table in ComboBox SamDsouza Word VBA 4 03-08-2021 10:00 PM
auto progress number evenly between two values across empty cells ? DBenz Excel 4 03-28-2018 04:46 AM
deleting a row in word table if a certain number of cells are empty and/or contain a certain value problem with deleting empty rows MS Word VBA nasserlouis Word VBA 2 08-17-2017 04:00 PM
deleting a row in word table if a certain number of cells are empty and/or contain a certain value How to number table cells continuously from a fixed point within the table in Word 2013 wondermuse Word Tables 15 12-07-2015 12:16 AM
Deleting info from specific table cells jamierbooth Word VBA 3 07-29-2014 03:20 PM

Other Forums: Access Forums

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


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