Sub 循环工作表() Dim ws As Worksheet For Each ws In Sheets i = i + 1 Debug.Print "这是第" & i & "张表,名称为:" & ws.Name NextEnd Sub 2、循环单元格:Sub 循环单元格() Dim ws As Worksheet Dim rng As Range Dim cell As Range Set ws = ThisWorkbook...
Dim cell As RangeFor Each cell In rng.Cells If Not cell.Comment Is Nothing Then cell.Comment.Delete End If cell.AddComment CStr(Now)Next 4、Address:Range对象的单元格区域地址。Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(3, 3))Debug.Print rng.Address'运行结果是:$A$1...
1、For 和for each的循环 1.1 for循环 不写i=i+1, i 也会在每次循环中自动+1 DimiasintegerFori=1to10Range(“a”&i)=iNext 1.2 for each 循环 以上for循环用for each写,相当于如下: '相当于 Dim eachCell as Range Dim i as integer For each eachCell in Range(“a1:a10”) i=i+1 ‘i初...
For Each cell In Range("A1:A5") MsgBox cell.Value Next cell End Sub 示例4:下面的代码与示例3的效果相同,但我们设置了代表单元格区域的对象变量并赋值,让For Each结构在对象变量代表的区域内循环遍历。 Sub ForEach4() Dim cell...
5) For Each TemCell In myRange 以上代码将当前区域所有单元格的边框设为细红色,注意单元格的Range.Borders属性的利用,及写法。6) For Each TemCell In myRange 以上语句在单元格区域中建立一个遍历的循环,在循环中逐一判断单元格的值,如果值和myCell的值相同,则将这个单元格的颜色填充为红色。3 ...
rng As Range Set rng = Range("A2:A11") For Each cl In rng&...
Set rng = ws.Range("A1:D10") ' 修改为你的数据区域 Dim cell As Range Dim i As Integer Application.ScreenUpdating = False ' 关闭屏幕更新以加快宏运行速度 For Each cell In rng If i = 0 Then ' 如果是第一行,不打印 i = i + 1 Else ws.Range(ws.Cells(i, 1), ws.Cells(i, 4))....
一、普通循环方法 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) ...
Set col = Range("A1:A10")For Each cell In col cell.Value = cell.Row()Next cell End Sub 4. 利用do while.. loop循环 通过do wile... loop 循环,并设置循环终止条件,进行批量填充。代码如下:Sub 填充单元格4()Dim i As Integer i = 1 Do While i < 11 Range("A" & i).Value = i ...
For Each cell In rng If cell.Value <> "" Then ' 如果单元格不为空 cell.Font.Color = RGB(0, 0, 255) ' 将字体颜色设置为蓝色 End If Next cell 查找特定值 Set foundCell = rng.Find("目标值") ' 查找目标值 If Not foundCell Is Nothing Then MsgBox "找到在 " & foundCell.Address '...