Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 10-20-2016, 09:50 AM
dwirony dwirony is offline Need macro to delete a column with a blank cell Windows 7 64bit Need macro to delete a column with a blank cell Office 2003
Advanced Beginner
Need macro to delete a column with a blank cell
 
Join Date: Oct 2016
Posts: 49
dwirony will become famous soon enough
Default Need macro to delete a column with a blank cell

Hello all,



I need a word macro to delete a column in any given table that has any blank cell within it. For example, my tables are generating and sometimes columns generate that have only a header but no information in the cell below it because there is none. I need to delete this entire column. I have this macro already but it only deletes a row/column if the entire row/column is empty; How can I modify it?
Code:
Sub DeleteEmptyTablerowsandcolumns()
Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
With ActiveDocument
  For Each Tbl In .Tables
    n = Tbl.Columns.Count
    For i = n To 1 Step -1
      fEmpty = True
      For Each cel In Tbl.Columns(i).Cells
        If Len(cel.Range.Text) > 2 Then
          fEmpty = False
          Exit For
        End If
      Next cel
      If fEmpty = True Then Tbl.Columns(i).Delete
    Next i
  Next Tbl
End With
With ActiveDocument
  For Each Tbl In .Tables
    n = Tbl.Rows.Count
    For i = n To 1 Step -1
      fEmpty = True
      For Each cel In Tbl.Rows(i).Cells
        If Len(cel.Range.Text) > 2 Then
          fEmpty = False
          Exit For
        End If
      Next cel
      If fEmpty = True Then Tbl.Rows(i).Delete
    Next i
  Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True
End Sub

Last edited by macropod; 10-20-2016 at 01:06 PM. Reason: Added code tags & formatting
Reply With Quote
 



Similar Threads
Thread Thread Starter Forum Replies Last Post
Need macro to delete a column with a blank cell If value of cell A Matches a value in a Range of cells (column) then add value of cell A to cell C rick10r Excel 1 07-05-2016 12:07 PM
Identify last blank cell in column mbesspiata Excel 0 02-27-2015 11:29 AM
Return Sum value of one column from cells not blank in another column zulugandalf Excel 3 08-14-2014 03:37 AM
Need macro to delete a column with a blank cell Macro to remove blank column netchie Word VBA 4 03-14-2013 02:22 PM
How can I delete the content of a cell in column if the cell value is more than 1000? Learner7 Excel 2 06-27-2011 05:44 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 11:09 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