一、Excel VBA 表格的操作 Sub main() '把表格B2的值改為'VBA Range和Cells函數(shù)' Range('B2').Value = 'VBA Range和Cells函數(shù)' '把D4:E6范圍內(nèi)的每一個表格的值都改為'Excel VBA' Range('D4:E5').Value = 'Excel VBA' End Sub Sub main() Cells(2, 2).Value = 'VBA Range和Cells函數(shù)' 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() 5. Excel表格的刪除
'表格的刪除 Sub deleteCells() '刪除當(dāng)前工作表的表格范圍B1-D3(行數(shù)等于列數(shù),剩余表格向上移動) Range(Cells(1, 2), Cells(3, 4)).Delete End Sub Sub deleteCells() '刪除當(dāng)前工作表的表格范圍C1-D2(行數(shù)大于列數(shù),,剩余表格向左移動) 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表格內(nèi)容的清除
Sub clearCells() '清除當(dāng)前工作表B1-D3范圍表格的所有設(shè)置 Range(Cells(1, 2), Cells(3, 4)).Clear End Sub Sub clearCellContents() '清除當(dāng)前工作表A1-D4范圍表格的內(nèi)容(表格的其余設(shè)置不發(fā)生改變) Range('A1:D4').ClearContents End Sub Sub clearCellComments() '清除Sheet2工作表所有表格的注釋 Worksheets('Sheet2').Cells.ClearComments End Sub Sub clearCellFormat() '清除當(dāng)前工作表A1-D4表格的格式 Range('A1:D4').ClearFormats End Sub Sub clearCellOutline() '當(dāng)前工作表的所有組合 Cells.ClearOutline End Sub 7. Excel表格的插入
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() '把當(dāng)前工作表的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表格的剪切和粘貼
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() '把當(dāng)前工作表的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單元格數(shù)據(jù)有效性設(shè)置 Sub addValidation() With Range('A1:A20').Validation .Delete ' 刪除現(xiàn)在的有效數(shù)據(jù)設(shè)置 ' 設(shè)置新的有效數(shù)據(jù)數(shù)據(jù)(男,,或者女) .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 ' 刪除現(xiàn)在的有效數(shù)據(jù)設(shè)置 ' 設(shè)置新的有效數(shù)據(jù)數(shù)據(jù)(18-60之內(nèi)的數(shù)字) .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:=xlBetween, Formula1:='18', Formula2:='60' .InCellDropdown = False ' 隱藏下拉框 .ShowError = True ' 提示輸入錯誤 .IgnoreBlank = True ' 空白可 End With End Sub |
|