六合开奖结果现场直播欢迎诸位!收藏本站注 册登 陆
常用三肖中特马:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 三肖中特马 > 文章页

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

Admin | 2008-6-15 14:34:40 | ReadNums | 12399 | 标签 六合开奖结果现场直播 | 打印本页
     

获取活动单元格的行索引或列索引(行号和列号)
  
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


问题未解决:在线咨询

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日