PDA

View Full Version : Word: VBA to Delete Columns From Table



WA4OSH
05-10-2017, 09:13 AM
I have a Word file which contains a table that I don't want to show certain customers. I want a macro that can delete (or hide) certain columns of this table.

Sample CSV file that is being formatted into a word-printable document. I cannot change the excel program to omit the column:

Name,Address,City,State,Zip
John Doe,123 Magnolia St.,Hempstead,NY,11550-1234
ABC Movers,1500E Main Ave Ste 201,Springfield,VA,21162-1010

I use this macro to convert it to a table:

' The number of rows to be converted to a table
NbrOfRows = 3
' Convert it to a table
Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=5, _
NumRows:=NbrOfRows, Format:=wdTableFormatNone, ApplyBorders:=True, ApplyShading _
:=True, ApplyFont:=True, ApplyColor:=True, ApplyHeadingRows:=True, _
ApplyLastRow:=False, ApplyFirstColumn:=True, ApplyLastColumn:=False, _
AutoFit:=True, AutoFitBehavior:=wdAutoFitContent




Name
Address
City
State
Zip


John Doe
123 Magnolia St
Hempstead
NY
11550-1234


ABC Movers
1500E Main Ave Ste 201
Springfield
VA
22162-1010








Now, I want to hide/delete the address field. Don't bother lecturing me about Word and that it's not excel, etc. I really don't care about the shortcomings of Word and what things you can and cannot do in macros. I want to solve the problem.




Name

City
State
Zip


John Doe

Hempstead
NY
11550-1234


ABC Movers

Springfield
VA
22162-1010

Paul_Hossler
05-10-2017, 10:09 AM
Now, I want to hide/delete the address field. Don't bother lecturing me about Word and that it's not excel, etc. I really don't care about the shortcomings of Word and what things you can and cannot do in macros. I want to solve the problem.


Sure .. OK



Option Explicit
Sub Macro1()
'
' Macro1 Macro
'
'
Selection.ConvertToTable Separator:=wdSeparateByCommas, NumColumns:=5, _
NumRows:=3, AutoFitBehavior:=wdAutoFitFixed
With Selection.Tables(1)
.Style = "Table Grid"
.ApplyStyleHeadingRows = True
.ApplyStyleLastRow = False
.ApplyStyleFirstColumn = True
.ApplyStyleLastColumn = False

.Columns(2).Delete
End With
End Sub