VBA:让每个20个单元格变大胆

时间:2017-02-03 17:46:11

标签: excel vba

我有一个循环,在Excel表格的每第10个条目上放置粗体字,但是我想在每个第20个条目上加粗字体。我该如何解决这个问题?非常感谢任何帮助。

以下是我正在使用的代码:

'insert xxxx9 after xxxx8 if needed
For i = 3 To counter - 1
    If Cells(i, 1).Value Mod 10 = 8 Then
   ' counter = counter + 1
        If Cells(i + 1, 1) <> Cells(i, 1) + 1 Then

            i = i + 1
            Rows(i & ":" & i).Select
            Selection.Insert Shift:=xlDown

            Cells(i, 1) = Cells(i - 1, 1) + 1
            Range("A" & 20).Select
            Selection.Font.Bold = True
            Cells(i, 2) = "900"
            Range("B" & i).Select
            Selection.Font.Bold = True
            Cells(i, 3) = "chk"
            Range("C" & i).Select
            Selection.Font.Bold = True
            Cells(i, 6) = "1"
            Range("F" & i).Select
            Selection.Font.Bold = True
        Else
            i = i + 1
            Rows(i & ":" & i).Select
            Range("A" & i).Select
            Selection.Font.Bold = True
        End If
    End If
Next


 'count number of entries
counter = 2
While (Cells(counter, 1).Value <> "" Or Cells(counter, 1).Value <> Null)
    counter = counter + 1
Wend

 'insert xxxx9 after xxxx8 if needed
For i = 3 To counter - 1
    If Cells(i, 1).Value Mod 10 = 8 Then
   ' counter = counter + 1
        If Cells(i + 1, 1) <> Cells(i, 1) + 1 Then

            i = i + 1
            Rows(i & ":" & i).Select
            Selection.Insert Shift:=xlDown

            Cells(i, 1) = Cells(i - 1, 1) + 1
            Range("A" & 20).Select
            Selection.Font.Bold = True
            Cells(i, 2) = "900"
            Range("B" & i).Select
            Selection.Font.Bold = True
            Cells(i, 3) = "chk"
            Range("C" & i).Select
            Selection.Font.Bold = True
            Cells(i, 6) = "1"
            Range("F" & i).Select
            Selection.Font.Bold = True
        Else
            i = i + 1
            Rows(i & ":" & i).Select
            Range("A" & i).Select
            Selection.Font.Bold = True
        End If
    End If
Next

1 个答案:

答案 0 :(得分:0)

Mod 10在除以10后返回余数。要使条件仅为每20行一次为TRUE,请改用Mod 20。相应地调整它相等,以确保它在正确的位置开始。