VBA基础之Excel VBA 表格的操作(一)
一、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


浙公网安备 33010602011771号