VBA Fill 3 Blank Cells Next to Nonblank

荒凉一梦 提交于 2019-12-25 18:46:07

问题


Here is an example of what I am trying to accomplish:

I am trying to add an "x" in the next 3 blank cells that are next to a nonblank cell (from left to right). I do not want to overwrite any cells though. As you can see in the first row, only December and January are filled and I did not overwrite February.

Any ideas?


回答1:


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



回答2:


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



回答3:


you could use this

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


来源:https://stackoverflow.com/questions/50382341/vba-fill-3-blank-cells-next-to-nonblank

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!