區(qū)域也是Excel工作表的基本操作對象,,其操作也比較簡單,,看下面的代碼
Sub test() Range("A1:B2").Select '選中A1到B2單元格 Range("A1:B2").Interior.Color = vbRed '設置A1到B2單元格的底色為紅色 Range("A1:B2").Merge '合并A1到B2單元格 End Sub
下面的代碼是設置邊框用的
Sub Macro1()
Range("A1:B2").Select Selection.Borders(xlDiagonalDown).LineStyle = xlNone '斜線無 Selection.Borders(xlDiagonalUp).LineStyle = xlNone '斜線無 With Selection.Borders(xlEdgeLeft) '左邊線 .LineStyle = xlContinuous '線條類型 .ColorIndex = 0 '顏色 .TintAndShade = 0 '漸變色 .Weight = xlThin '線條粗細 End With With Selection.Borders(xlEdgeTop) '頂邊線 .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeBottom) '底邊線 .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlEdgeRight) '右邊線 .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideVertical) '里堅線 .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With Selection.Borders(xlInsideHorizontal) '里橫線 .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With End Sub
代碼中selection代表表格中被選中的區(qū)域,在上面的代碼中即range("A1:B2"),。對于一些設置格式的代碼通常是不用自己來寫的,只要錄制一個宏就可以了,,關于怎么錄制宏請參見錄制宏,。
|