繁体   English   中英

使用循环清除内容

[英]Using Loop to Clear contents

我已经编写了以下代码,以检查A行中的值是否等于“ Forecast”,则应该清除Range D5:D53的内容。

第1行是vlookup,因此有一个公式可以得出“实际”或“预测”

Dim k As Integer

For k = 1 To 13
    If Sheets("2017 Forecast").Cells(k, 1).Value = "Forecast" Then
        Sheets("2017 Forecast").Range("D5:D53").Select.ClearContents
    End If
Next k

在使用ClearContents之前,无需使用Select

另外,请尝试添加UCase ,以确保文本中间没有大写字母。

Dim k As Integer

With ThisWorkbook.Sheets("2017 Forecast")
    For k = 1 To 13
        If UCase(.Cells(k, 1).Value2) = "FORECAST" Then
            .Range("D5:D53").ClearContents
        End If
    Next k
End With

也许这对你有用吗?

Option explicit

Sub Compare skies()

Dim k As long
Dim ValueRead as variant

With Sheets("2017 Forecast")
For k = 1 To 13

ValueRead = .Cells(k, 1).Value

' Slow, case insensitive string comparison '
If strcomp(ValueRead,"Forecast",vbtextcompare) = 0 Then
    .Range("D5:D53").ClearContents ' You want to clear the exact same range 13 times? '
Elseif strcomp(ValueRead,"Actual",vbtextcompare) <> 0 then
Msgbox("VLOOKUP returned a value outside of Actual and Forecast")

End if

Next k
End with

End sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM