VBA在Nonblank旁边填充3个空白单元格

时间:2018-05-17 02:06:12

标签: excel vba excel-vba excel-formula

这是我想要完成的一个例子:

enter image description here

我正在尝试在非空白单元格旁边的3个空白单元格中添加“x”(从左到右)。我不想覆盖任何细胞。正如你在第一行中看到的那样,只有12月和1月被填满,我没有覆盖2月。

有什么想法吗?

3 个答案:

答案 0 :(得分:2)

Sub sub1()
  Dim irow&, icol&, n&
  For irow = 2 To 6 ' rows
    n = 0
    For icol = 2 To 14 ' columns
      If Cells(irow, icol) = "" Then
        n = n + 1
        If n <= 3 Then Cells(irow, icol) = "x"
      Else
        n = 0
      End If
    Next
  Next
End Sub

答案 1 :(得分:0)

For Each ID In Range("A2:A6")   'Change your range according your ID
    For Each cell In ID.EntireRow.Cells     'Check each cell of ID's row
        If cell.Value = "" Then
          cell.Value = "x"
          No = No + 1
        Else
          No = 0  'Recount
        End If
        If No = 3 Then Exit For     'stop after mark 3 x
    Next
Next

答案 2 :(得分:0)

你可以用这个

Option Explicit

Sub main()
    Dim cell As Range, nCols As Long
    With ActiveSheet.UsedRange.SpecialCells(xlCellTypeBlanks)
        For Each cell In .Cells
            nCols = WorksheetFunction.Min(cell.Column - 1, 3)
            If Intersect(cell.Offset(, -nCols).Resize(, nCols + 1), .Cells).Count < 4 Then cell.Value = "x"
        Next
    End With
End Sub