一、Excel VBA 表格的操作
1. Excel表格的指定以及表格屬性的設置
Sub main() '把表格B2的值改為"VBA Range和Cells函數" Range("B2").Value = "VBA Range和Cells函數" '把D4:E6范圍內的每一個表格的值都改為"Excel VBA" Range("D4:E5").Value = "Excel VBA" End Sub Sub main() Cells(2, 2).Value = "VBA Range和Cells函數" Cells(4, 4).Value = "Excel VBA" Cells(4, 5).Value = "Excel VBA" Cells(5, 4).Value = "Excel VBA" Cells(5, 5).Value = "Excel VBA" End Sub Sub main() 'Worksheets("工作表名稱").Activate Worksheets("Sheet2").Activate End Sub Sub main() Worksheets("Sheet2").Activate Worksheets("Sheet2").Range("D4").Value = "Excel VBA" Worksheets("Sheet2").Cells(5, 5).Value = "Excel VBA" End Sub Sub main() Worksheets("Sheet2").Activate '修改表格的值 Worksheets("Sheet2").Range("D4").Value = "Excel VBA" '修改表格的寬度 Worksheets("Sheet2").Range("D4").ColumnWidth = 20 '修改表格的高度 Worksheets("Sheet2").Range("D4").RowHeight = 30 '修改表格的文字顏色為紅色 Worksheets("Sheet2").Range("D4").Font.ColorIndex = 3 '修改表格的背景顏色為綠色 Worksheets("Sheet2").Range("D4").Interior.ColorIndex = 4 End Sub Sub main() Worksheets("Sheet2").Activate '修改表格的值 Worksheets("Sheet2").Range("D4").Value = "Excel VBA" '修改表格的寬度 Worksheets("Sheet2").Range("D4").ColumnWidth = 20 '修改表格的高度 Worksheets("Sheet2").Range("D4").RowHeight = 30 '修改表格的文字顏色為紅色 Worksheets("Sheet2").Range("D4").Font.ColorIndex = 3 '修改表格的背景顏色為綠色 Worksheets("Sheet2").Range("D4").Interior.ColorIndex = 4 End Sub ‘功能同上 Sub main() With Worksheets("Sheet2") .Activate .Range("D4").Value = "Excel VBA" .Range("D4").ColumnWidth = 20 .Range("D4").RowHeight = 30 .Range("D4").Font.ColorIndex = 3 .Range("D4").Interior.ColorIndex = 4 End With End Sub ‘功能同上 Sub main() With Worksheets("Sheet2") .Activate With .Range("D4") .Value = "Excel VBA" .ColumnWidth = 20 .RowHeight = 30 .Font.ColorIndex = 3 .Interior.ColorIndex = 4 End With End With End Sub
2. Excel表格范圍的指定
'Rang params Sub main() Range("A3:D5").Value = "Excel VBA入門" '或者 Range("A3", "D5").Value = "Excel VBA入門" End Sub 'Cells params Sub main() Range(Cells(3, 1), Cells(5, 4)).Value = "Excel VBA入門" End Sub
3. Excel行的選擇
Sub setRowValueAndBgColor() Range("B2:D3").EntireRow.Value = "Excel VBA" Range("B2:D3").EntireRow.Interior.ThemeColor = 5 End Sub Sub setRowValueAndBgColor() Range(Cells(2, 2), Cells(3, 4)).EntireRow.Value = "Excel VBA" Range(Cells(2, 2), Cells(3, 4)).EntireRow.Interior.ThemeColor = 5 End Sub Sub hideRowAndSetRowValue() Range("A2").EntireRow.Hidden = True Range("A4").EntireRow.Value = "Excel VBA" End Sub
4. Excel列的選擇
Sub setColumnValueAndBgColor() Range("B2:D3").EntireColumn.Value = "Excel VBA" Range("B2:D3").EntireColumn.Interior.ThemeColor = 5 End Sub Sub setColumnValueAndBgColor() Range(Cells(2, 2), Cells(3, 4)).EntireColumn.Value = "Excel VBA" Range(Cells(2, 2), Cells(3, 4)).EntireColumn.Interior.ThemeColor = 5 End Sub
Sub hideColumnAndSetRowValue()
Range("A2").EntireColumn.Hidden = True
Range("A4").EntireColumn.Value = "Excel VBA"
End Sub
5. Excel表格的刪除
方法名 | 參數 | 常量 | 說明 |
---|---|---|---|
Delete | Shift | xlShiftToLeft | 向左移動 |
xlShiftUp | 向上移動 |
'表格的刪除 Sub deleteCells() '刪除當前工作表的表格范圍B1-D3(行數等於列數,剩余表格向上移動) Range(Cells(1, 2), Cells(3, 4)).Delete End Sub Sub deleteCells() '刪除當前工作表的表格范圍C1-D2(行數大於列數,剩余表格向左移動) Range("C1:D2").Delete End Sub '行和列的刪除 Sub deleteRow() '剩余表格整體向上移動 Range(Cells(1, 2), Cells(3, 4)).EntireRow.Delete End Sub Sub deleteColumn() '剩余表格整體向左移動 Range(Cells(1, 2), Cells(3, 4)).EntireColumn.Delete End Sub
6. Excel表格內容的清除
方法 | 說明 |
---|---|
ClearContents | 清除表格內容 |
ClearFormats | 清除表格格式 |
ClearComments | 清除表格注釋 |
ClearOutline | 清除表格組合 |
Clear | 清除所有(內容、格式、注釋、組合) |
Sub clearCells() '清除當前工作表B1-D3范圍表格的所有設置 Range(Cells(1, 2), Cells(3, 4)).Clear End Sub Sub clearCellContents() '清除當前工作表A1-D4范圍表格的內容(表格的其余設置不發生改變) Range("A1:D4").ClearContents End Sub Sub clearCellComments() '清除Sheet2工作表所有表格的注釋 Worksheets("Sheet2").Cells.ClearComments End Sub Sub clearCellFormat() '清除當前工作表A1-D4表格的格式 Range("A1:D4").ClearFormats End Sub Sub clearCellOutline() '當前工作表的所有組合 Cells.ClearOutline End Sub
7. Excel表格的插入
方法名 | 參數 | 常量 | 說明 |
---|---|---|---|
Insert | Shift | xlShiftToLeft | 向左移動 |
xlShiftUp | 向上移動 |
8. Excel表格的拷貝和粘貼
方法名 | 參數 | 說明 |
---|---|---|
Copy | Destination | 指定粘貼位置的Range對象 |
Sub copyCells() '把B1-D3拷貝到F5-H7 Range(Cells(1, 2), Cells(3, 4)).Copy Destination:=Cells(5, 6) End Sub Sub copyCells() '把B1-D3拷貝到F5-H7 Range("B1:D3").Copy Destination:=Range("F5") End Sub Sub copyCells() '把當前工作表的B1-D3拷貝到F5-H7 ActiveSheet.Range("B1:D3").Copy Destination:=Range("F5") End Sub Sub copyCells() '把工作表Sheet1的B1-D3拷貝到工作表Sheet2的F5-H7 Worksheets("Sheet1").Range("B1:D3").Copy Destination:=Worksheets("Sheet2").Range("F5") End Sub
9. Excel表格的剪切和粘貼
方法名 | 參數 | 說明 |
---|---|---|
Cut | Destination | 指定粘貼位置的Range對象 |
Sub copyCells() '把B1-D3剪切到F5-H7 Range(Cells(1, 2), Cells(3, 4)).Cut Destination:=Cells(5, 6) End Sub Sub copyCells() '把B1-D3剪切到F5-H7 Range("B1:D3").Cut Destination:=Range("F5") End Sub Sub copyCells() '把當前工作表的B1-D3剪切到F5-H7 ActiveSheet.Range("B1:D3").Cut Destination:=Range("F5") End Sub Sub copyCells() '把工作表Sheet1的B1-D3剪切到工作表Sheet2的F5-H7 Worksheets("Sheet1").Range("B1:D3").Cut Destination:=Worksheets("Sheet2").Range("F5") End Sub
10. VBA單元格數據有效性設置
Sub addValidation() With Range("A1:A20").Validation .Delete ' 刪除現在的有效數據設置 ' 設置新的有效數據數據(男,或者女) .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="男,女" .InCellDropdown = True ' 顯示下拉框 .ShowError = True ' 提示輸入錯誤 .IgnoreBlank = True ' 空白可 End With End Sub Sub addValidation() With Range("B2").Validation .Delete ' 刪除現在的有效數據設置 ' 設置新的有效數據數據(18-60之內的數字) .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:="18", Formula2:="60" .InCellDropdown = False ' 隱藏下拉框 .ShowError = True ' 提示輸入錯誤 .IgnoreBlank = True ' 空白可 End With End Sub