注意:ide
1.這段宏是在MicrosoftOfficeStandard2013下撰寫和調試的。用在其餘Office版本中可能須要適當修改字體
2.保存帶有宏的Excel表格,應該保存爲*.xlsm格式(啓用宏的工做簿)spa
關於本宏的用途調試
如今修改了一個程序的若干個模塊,每一個模塊修改了若干個文件,如今要用一個EXCEL表格總結修改的文件信息。工做簿中每一個模塊(Sheet)都要有標題,修改單中每一個文件都要有文件名、SVN上地址、修改說明本宏用於快速生成一個表格結構,這樣在以後使用時只要傻瓜化地向表格中添加文字就能夠了code
宏運行後的效果圖:orm
宏的用法:調用Init啓動宏,想要多加一個Sheet,只須要在Init例程中添加一個AddPage例程,後面加上添加Sheet的名稱,就能夠了。ip
Dim SheetNum As Integer '初始化EXCEL表格 Sub Init() SheetNum = 0 '建立四個表格 AddPage "MonProxy" AddPage "MonProxyTool" AddPage "MonService" AddPage "MonClient" End Sub '添加頁面 輸入:要建立的頁面名 Sub AddPage(SheetName As String) '選中最後一個表格 '規律:第一個表格叫Sheet1,更名後,新生成的表格會被默認命名爲Sheet1 '再改Sheet1名後,再新建立表格,依次會被默認命名爲Sheet二、Sheet3... If SheetNum = 0 Then Sheets("Sheet1").Name = SheetName Else Sheets("Sheet" & SheetNum).Name = SheetName End If SheetNum = SheetNum + 1 '選中更名後的表格 Sheets(SheetName).Select '設定表格內容 DecorateSheet SheetName '在本表格後建立新表格 Sheets.Add After:=ActiveSheet End Sub '設定表格內容 Sub DecorateSheet(SheetName As String) Range("A1").Select '設置列寬 Columns("A:A").ColumnWidth = 24 Columns("B:B").ColumnWidth = 45 Columns("C:C").ColumnWidth = 75 '設置行高 Rows("1:1").RowHeight = 75 '標題行配置 'A1-C1爲標題行 Range("A1:C1").Select '標題行配置 With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With '標題行字體配置 With Selection.Font .Name = "宋體" .Size = 36 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 .ThemeFont = xlThemeFontMinor End With Selection.Font.Bold = True '設定標題行文字 ActiveCell.FormulaR1C1 = SheetName '時間行配置 'A2-C2爲時間行 Range("A2:C2").Select '標題行配置 With Selection .HorizontalAlignment = xlCenter .VerticalAlignment = xlCenter .WrapText = False .Orientation = 0 .AddIndent = False .IndentLevel = 0 .ShrinkToFit = False .ReadingOrder = xlContext .MergeCells = True End With '標題行字體配置 With Selection.Font .Name = "宋體" .Size = 11 .Strikethrough = False .Superscript = False .Subscript = False .OutlineFont = False .Shadow = False .Underline = xlUnderlineStyleNone .ThemeColor = xlThemeColorLight1 .TintAndShade = 0 .ThemeFont = xlThemeFontMinor End With '設定標題行文字 ActiveCell.FormulaR1C1 = CDate(Format$(Now, "yyyy-mm-dd hh:MM")) '表格正文部分 '表格標題列1:文件名 Range("A3").Select ActiveCell.FormulaR1C1 = "文件名" With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.599993896298105 .PatternTintAndShade = 0 End With Range("A4:A33").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With '表格標題列2:SVN上地址 Range("B3").Select ActiveCell.FormulaR1C1 = "SVN上地址" With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.599993896298105 .PatternTintAndShade = 0 End With Range("B4:B33").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorAccent1 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With '表格標題列3:修改說明 Range("C3").Select ActiveCell.FormulaR1C1 = "修改說明" With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.599993896298105 .PatternTintAndShade = 0 End With Range("C4:C33").Select With Selection.Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .ThemeColor = xlThemeColorLight2 .TintAndShade = 0.799981688894314 .PatternTintAndShade = 0 End With '設置整個單元格邊框格式 Range("A1:C33").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone Selection.Borders(xlDiagonalUp).LineStyle = xlNone With Selection.Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = xlAutomatic .TintAndShade = 0 .Weight = xlThin End With '左側再加一列,爲了美觀(1.表格能夠位於居中部分;2.表格左側邊框線會得以顯示) Range("A4").Select Selection.EntireColumn.Insert , CopyOrigin:=xlFormatFromLeftOrAbove Columns("A:A").ColumnWidth = 2.63 End Sub
ENDit