Sub TestSub()Dim i, lastColCells.Ungroup '取消全部分组'找到最后一列数据lastCol = Cells(1, Cells.Columns.Count).End(xlToLeft).ColumnFor i = 2 To lastCol '从第2列开始循环If Cells(1, i).Value <> '' Then '第一行单元格里要写数据了Columns(i).OutlineLevel = Cells(1, i).Value '设置分组级别End IfNextEnd Sub