PDA

View Full Version : Create a Table Using Listobject without Generating Autofilters



abbab
04-03-2013, 12:40 PM
Hello,

I have a VBA code to generate a table in Excel as follows:
If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
Range("R1:Y8").Select
ActiveSheet.ListObjects.Add(xlSrcRange, Range("$R$1:$Y$8"), , xlYes).Name = _
"Table1"
ActiveSheet.ListObjects("Table1").TableStyle = "TableStyleMedium2"

However, after the table is created with headers such as those in the top half of the attached image. I'd like to be able to create something like those in the bottom half of the attached image instead.

Is there anyway to do this without having the filters generate and then going back and modifying the contents of the cells? I'd like to prevent the filters from being inserted in the table in the first place if that is possible. And I would like to keep all of the other formatting.

Thanks in advance for your assistance with this!
Amy

Aflatoon
04-03-2013, 02:34 PM
Try simply:
ActiveSheet.ListObjects("Table1").Showautofilter = False

p45cal
04-03-2013, 03:05 PM
Aflatoon's way is the way of specifically removing the autofilter arrows from the Table, but you could try moving your If ActiveSheet.AutoFilterMode line to after the Table is created.

As to the rest, (you won't be able to merge a header cell with the cell above it in a listobject) your headers appear to be in row 2, so you can get close:Set xx = ActiveSheet.ListObjects.Add(xlSrcRange, Range("$R$2:$Y$8"), , xlYes)
With xx
.Name = "Table1"
.ShowAutoFilter = False
With .HeaderRowRange
With .Offset(-1, 1).Resize(1, .Columns.Count - 1)
.Cells(1) = "Referral Type"
.HorizontalAlignment = xlCenterAcrossSelection
.Interior.ColorIndex = 41
With .Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = xlAutomatic
End With
End With
End With
End With

abbab
04-05-2013, 06:14 AM
Thanks! Why can't I keep the merged cell format, just out of curiosity?

p45cal
04-05-2013, 08:35 AM
I'm not sure; I tried it and it wouldn't let me. It's probably to do with a Table/ListObject being something that has rules to follow, such as unique headers, perhaps to be like tables in Access and other databases?