1、定义一个Range对象。 Dim rng As Range 2、引用Range对象 ,假设我们定义了一个工作表对象ws。 Set rng = ws.Range("A1:B2") Set rng = ws.Range("C1") Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(3, 3)). Set rng = ws.UsedRange Set rng = ws.Range("A1").CurrentRegion 3、批注...
Dim cell As Range For Each cell In Range("A1:A10") If cell.Value >= 10 And cell.Value <= 20 Then cell.Interior.Color = RGB(255, 255, 0) '黄色高亮 Else cell.Interior.ColorIndex = 0 '去除填充色 End If Next cell End Sub 三、设置条件比较逻辑 在VBA中,If…Then…Else是执行条件逻辑...
Dim cell As Range, rng As Range '声明单元格对象变量 Dim i As Long '声明计数变量 '设置rng变量的值 Set rng = Range("B2:B9") i = 0 '遍历rng对象变量代表的单元格区域 '并判断单元格中的值是否大于80 For Each cell In...
Sub Cycle()Dim i As Long Dim j As Long Dim MaxCnt As Long Dim arr As Variant Dim Cell As Range For Each Cell In Range("G9:G13")arr = Application.Transpose(Application.Transpose(Cell.Resize(1, 8)))Cells(Cell.Row, "C").Clear For i = LBound(arr) To UBound(arr)Max...
VBA中常这样写:Range(cells(y1,x1),cells(y2,x2)).Select,就是指选中以cells(y1,x1)和cells(y2,x2)两单元格为对角线的一个区域。 --- 赋值的话,如下几句都是赋值的,区别还是一样,Cells()是对一个单元格赋值,而Range()则可以对一个区域的所有单元格赋值: Range("A1:D10")....
Sub 循环工作表() Dim ws As Worksheet For Each ws In Sheets i = i + 1 Debug.Print "这是第" & i & "张表,名称为:" & ws.Name Next End Sub 2、循环单元格: Sub 循环单元格() Dim ws As Worksheet Dim rng As Range Dim cell As Range Set ws = ThisWorkbook.Sheets("表3") Set rng...
For Each cell In rng If cell.Value = 0 Then Continue For ' 跳过值为0的单元格 End If ' 在这里编写对非零单元格的操作 If cell.Value = 10 Then Exit For ' 当值为10时退出循环 End If Next cell 总结: 在Excel VBA中,将循环从每个单元格更改为特定范围,可以使用For Each循环遍历指定...
1、主体不同 CELLS(y,x)的两个参数分别为行和列。Range()则是指一个区域。2、范围不同 CELLS(y,x)是单个单元格对像。Range()可以是一个单元格,也可以是多个单元格。3、赋值不同 Cells()是对一个单元格赋值。而Range()则可以对一个区域的所有单元格赋值。注意:VBA中“Range(cells(y1,x1)...
Sheets("Sheet2").Range("A1").PasteSpecial Paste:=xlPasteValues '粘贴数值 1 2 3 4 5 6 7 8 9 4.9 设置字符格式 4.9.1设置单元格文本字符串格式 Sub CellCharacter() With Range("A1") .Clear .Value = "Y=X2+1" .Characters(4, 1).Font.Superscript = True '将第4个字符设置为上标 ...
说明:Excel VBA 在第 3 行和第 2 列交叉处的单元格中输入值 2。代码:Range(Cells(1, 1), ...