ExcelVBA-Range对象
Rows.Count 显⽰所有⾏的数⽬
Columns.Count 显⽰所有列的数⽬
Cells(Rows.Count, 1).End(xlUp).Row 显⽰第⼀列从下⾯数第⼀个有值的单元格的⾏号
Cells(1, Columns.Count).End(xlToLeft).Column 显⽰第⼀⾏从右⾯数第⼀个有值的单元格的列号
Cells(1, 1).BorderAround xlContinuous, xlThin 给A1单元格加⼊外边框
Range("A1:B4").Borders.LineStyle. = xlContinuous 给这个区域加⼊边框
Rows(1).AutoFit 为第⼀⾏分配合适的⾏距
Columns(1).AutoFit 为第⼀列分配合适的列宽
Rows(1).Delete 删除第⼀⾏
Columns(1).Delete 删除第⼀列
Cells(1, 1).MergeArea.Count 计算合并单元格的总单元格数
Cells(1, 1).MergeArea.Rows.Count 计算合并单元格的⾏数
Cells(1, 1).MergeArea.Columns.Count 计算合并单元格的列数
Cells(1, 1).MergeCells 返回Boolean,True和False Rnd()函数返回⼩于1,⼤于等于0的⼀个随机数
Cells(1, 1).Interior.ColorIndex = Int(56 * Rnd() + 1) 给单元格内部赋个随机的颜⾊
Cells(1, 1).EntireColumn 包含此单元格的列
Cells(1, 1).EntireRow 包含此单元格的⾏
Cells.Interior.ColorIndex = xlColorIndexNone 所有单元格五⾊
Set rng = Application.Union(Target.EntireRow, Target.EntireColumn) 设置合并区域,注意Union⽅法是在Application下⾯的Set rng = Sheet1.UsedRange.SpecialCells(xlCellTypeFormulas) 出具有公式的单元格区域
Sheet1.Range("A:A").ClearContents 清除第⼀列的所有内容
Sheet1.Range("A:A").Clear 清楚第⼀列的所有东西,包括值、格式等
For Each rng In Sheet2.Range("A1:A40") 遍历区域内的每个单元格
Range("A:A").Replace "区", "市" 替换
Range("A:A").Replace What:="区", Replacement:="市" 替换
Sheet1.Range("A1:G7").Copy Sheet2.Range("A1") 复制区域,⾼宽变化
Sheet1.Range("A1:G7").Copy 复制区域
Sheet3.Range("A1").PasteSpecial xlPasteColumnWidth 黏贴相同宽度,相同⾼度要⾃⼰设置
Range("B1:B20").Validation.Add Type:=xlValidateList, Formula1:="A,B,C,D,E,F,G" 数据有效性
Range("A1").TextToColumns Space:=True 通过不定数量的空格来分列字符串
Cells(3, 4).Top 单元格顶部距离顶部的距离 Cells(3, 4).Left 单元格左边距离左边的距离 Cells(3) 表⽰第⼀⾏的第三列的单元格ActiveWindow.VisibleRange.Cells(3) 表⽰当前页⾯第⼀⾏的第三列的单元格
Cells(1, 1).Previous.Select 向前移⼀个位置 Cells(1, 1).Next.Select 向后移动⼀个位置
公式计算
Range("C2").Formula = "= A2*B2" 简单相乘
excel的随机数函数Range("C2").Copy Range("C3:C" & r) 复制单元格公式
Cells(r + 1, 3).Formula = "=SUM(C2:C" & r & ")" 相加公式,也可以⽤Range
Range("C2").FormulaR1C1 = "=RC[-2]*RC[-1]" 按照相对位置来算
Range("C" & r + 1).FormulaR1C1 = "=SUM(R[-" & r - 1 & "]C:R[-1]C)" 同上,按相对位置来算,注意,向上是“-”
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论