首页 > 解决方案 > Excel VBA在特定值后的空白单元格中添加值

问题描述

在下面的示例中,VBA 中有没有办法在 3 个 x 旁边添加一个“Z”?3 个 x 之后的单元格必须为空白。(就像下面的第一个ID和April一样)

Excel 示例

标签: vbaexcel

解决方案


在上一个问题的答案中添加接受的代码:

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"
        ElseIf n = 4 Then
            Cells(irow, icol) = "z"
        End If
      Else
        n = 0
      End If
    Next
  Next
End Sub

推荐阅读