VBA 将Excel工作簿中所有表格进行一键排版

自己管理的Excel工作簿经过多人的修改,其中的很多格式会经常出现不一致的情况,这里提供VBA代码,用来实现表格数据格式一键排版,免除手动频繁调整Excel数据格式。

 1 Option Explicit
 2 Option Base 1
 3 
 4 Sub autoSet()
 5     Dim i As Integer
 6     
 7     For i = 1 To Sheets.Count
 8         Sheets(i).Activate      '这行代码不能省,再进行表格操作前,需要先激活需要操作的表格
 9         Sheets(i).Cells.Select
10         With Selection.Font
11             .Name = "微软雅黑"    '设置字体种类
12             .Size = 11        '设置字体大小
13             .Strikethrough = False
14             .Superscript = False
15             .Subscript = False
16             .OutlineFont = False
17             .Shadow = False
18             .Underline = xlUnderlineStyleNone
19             .TintAndShade = 0
20             .ThemeFont = xlThemeFontNone
21         End With
22         With Selection
23             .HorizontalAlignment = xlLeft  '设置文本排列,靠左
24             .VerticalAlignment = xlCenter  '设置文本排列,垂直居中
25             .Orientation = 0
26             .AddIndent = False
27             .IndentLevel = 0
28             .ShrinkToFit = False
29             .ReadingOrder = xlContext
30             .MergeCells = False
31         End With
32         Sheets(i).Range("A1").Select
33     Next
34 End Sub

需要设置其他方面的格式,直接在for循环内添加相应代码即可实现,可以先录制宏来查看设置的代码,再进行添加

原文地址:https://www.cnblogs.com/architecture101-gbt/p/8459619.html