Excel VBA如果那么语句

时间:2016-06-23 07:12:32

标签: vba excel-vba excel

我想如果我的细胞(b5,c5)空白而不是细胞(f5,g5,h5)也空白,细胞(b6,c6)是空白而不是细胞(f6,g6,h6),细胞( b7,c7)比细胞(f7,g7,h7)空白,细胞(b8,c8)比细胞(f8,g8,h8)空白,细胞(b9,c9)空白而不是细胞(f9,g9,h9) ),但同样在我想要这段代码200次帮助我做一个小代码。提前谢谢。

Sub BLANK()

    If Range("B5,C5") = "" Then
    Range("F5,G5,H5") = ""
    If Range("B6,C6") = "" Then
    Range("F6,G6,H6") = ""
    If Range("B7,C7") = "" Then
    Range("F7,G7,H7") = ""
    ' same code in cell("b5:b200") make small code please
     end if
  end sub

2 个答案:

答案 0 :(得分:1)

以下将循环超过200次并产生您需要的结果

For x = 5 to 200
   If Range(Cells(x,2), Cells(x,3)).Value = "" Then
      Range(Cells(x,6), Cells(x,8)).Value = ""
   End if
Next x

答案 1 :(得分:1)

我会建议你使用这样的东西。正确声明范围和工作表的位置,并使用option explicit正确声明所有变量,以防止出现多个问题

Sub makeBlank()

Dim mySheet As Worksheet
Set mySheet = Sheets("devSheet")

Dim rowCounter As Long

With mySheet
    For rowCounter = 5 To 200
        If .Range("B" & rowCounter & ",C" & rowCounter).Value = "" Then
            .Range("F" & rowCounter & ",G" & rowCounter & ",H" & rowCounter).Value = ""
        End If
    Next rowCounter
End With

End Sub
相关问题