Dzwebs.Net

撰写电脑技术杂文十余年

VBA操作Excel工作表行列、单元格代码示例

Admin | 2008-6-15 14:34:40 | 被阅次数 | 16567

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

获取活动单元格的行索引或列索引(行号和列号)
  
Dim RowIndex As Integer
  RowIndex = Target.Row
  MsgBox "当前单元格所处的行是第:" & RowIndex&“行“

  Dim ColIndex As Integer
  ColIndex = Target.Column
  MsgBox "当前单元格所处的列是第:" & ColIndex&“ 列“
-------------------------------------------------------------------------------

 隐藏行
  Sub HideRow()
  Dim iRow As Long
  MsgBox "隐藏当前单元格所在的行"
  iRow = ActiveCell.Row
  ActiveSheet.Rows(iRow).Hidden = True
  MsgBox "取消隐藏"
  ActiveSheet.Rows(iRow).Hidden = False
End Sub
--------------------------------------------------------------------------------

隐藏列
Sub HideColumn()
  Dim iColumn As Long
  MsgBox "隐藏当前单元格所在列"
  iColumn = ActiveCell.Column
  ActiveSheet.Columns(iColumn).Hidden = True
  MsgBox "取消隐藏"
  ActiveSheet.Columns(iColumn).Hidden = False
End Sub
--------------------------------------------------------------------------------

插入行
Sub InsertRow()
  Dim rRow As Long
  MsgBox "在当前单元格上方插入一行"
  rRow = Selection.Row
  ActiveSheet.Rows(rRow).Insert
End Sub
--------------------------------------------------------------------------------

插入列
Sub InsertColumn()
  Dim cColumn As Long
  MsgBox "在当前单元格所在行的左边插入一行"
  cColumn = Selection.Column
  ActiveSheet.Columns(cColumn).Insert
End Sub
--------------------------------------------------------------------------------

插入多行
Sub InsertManyRow()
  MsgBox "在当前单元格所在行上方插入三行"
  Dim rRow As Long, i As Long
  For i = 1 To 3
    rRow = Selection.Row
    ActiveSheet.Rows(rRow).Insert
  Next i
End Sub
--------------------------------------------------------------------------------

设置行高
Sub SetRowHeight()
  MsgBox "将当前单元格所在的行高设置为25"
  Dim rRow As Long, iRow As Long
  rRow = ActiveCell.Row
  iRow = ActiveSheet.Rows(rRow).RowHeight
  ActiveSheet.Rows(rRow).RowHeight = 25
  MsgBox "恢复到原来的行高"
  ActiveSheet.Rows(rRow).RowHeight = iRow
End Sub
--------------------------------------------------------------------------------

设置列宽
Sub SetColumnWidth()
  MsgBox "将当前单元格所在列的列宽设置为30"
  Dim cColumn As Long, iColumn As Long
  cColumn = ActiveCell.Column
  iColumn = ActiveSheet.Columns(cColumn).ColumnWidth
  ActiveSheet.Columns(cColumn).ColumnWidth = 30
  MsgBox "恢复至原来的列宽"
  ActiveSheet.Columns(cColumn).ColumnWidth = iColumn
End Sub
--------------------------------------------------------------------------------

恢复行高列宽至标准值
Sub ReSetRowHeightAndColumnWidth()
  MsgBox "将当前单元格所在的行高和列宽恢复为标准值"
  Selection.UseStandardHeight = True
  Selection.UseStandardWidth = True
End Sub


该杂文来自: Excel杂文

上一篇:用Vba实现选择单元格的表名,自动显示相应Excel数

下一篇:最简单的Excel工作薄、工作表新建、删除、复制、

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计