简体   繁体   English

如何检查单元格的内容是否为数组中的值-VBA Excel

[英]How do I check if the contents of a cell is a value in an array - VBA Excel

I am writing a small macro to do a simple task. 我正在编写一个小宏来完成一个简单的任务。 I have made some progress so far, however I am stuck in trying to do an if statement to check if the contents of a cell equals a string in an array then it performs the next statement. 到目前为止,我已经取得了一些进展,但是我一直在尝试执行if语句来检查单元格的内容是否等于数组中的字符串,然后执行下一条语句。 Here is my code so far: 到目前为止,这是我的代码:

Public Sub Saturdays()

Dim i As Integer
Dim j As Integer
Dim Sat As Variant

Sat = Array("1/6/2018", "1/13/2018", "1/20/2018", "1/27/2018", "2/3/2018", "2/10/2018", "2/17/2018", "2/24/2018", "3/3/2018", "3/10/2018", "3/17/2018", "3/24/2018", "3/31/2018", "4/7/2018", "4/14/2018", "4/21/2018", "4/28/2018", "5/5/2018", "5/12/2018", "5/19/2018", "5/26/2018", "6/2/2018", "6/9/2018", "6/16/2018", "6/23/2018", "6/30/2018", "7/7/2018", "7/14/2018", "7/21/2018", "7/28/2018", "8/4/2018", "8/11/2018", "8/18/2018", "8/25/2018", "9/1/2018", "9/8/2018", "9/15/2018", "9/22/2018", "9/29/2018", "10/6/2018", "10/13/2018", "10/20/2018", "10/27/2018", "11/3/2018", "11/10/2018", "11/17/2018", "11/24/2018", "12/1/2018", "12/8/2018", "12/15/2018", "12/22/2018", "12/29/2018")


For i = 5 To 100


If Sheet1.Cells(i, 1) <> "" Then

For j = 4 To 730

    If Sheet1.Cells(4, j) = Sat Then

        Sheet1.Cells(i, j) = 0

End If


Next j

End If

Next i


End Sub

Arrays don't have a simple method to look through the entire group. 数组没有简单的方法可以查看整个组。 You'll have to check each of them one at a time. 您必须一次检查每个。 Replace 更换

If Sheet1.Cells(4, j) = Sat Then

    Sheet1.Cells(i, j) = 0
End If

With

For x = lbound(sat) To ubound(sat)
    If Sheet1.Cells(4, j) = Sat(x) Then
        Sheet1.Cells(i, j) = 0
        Exit For
    End If
Next

I don't know why this wouldn't work. 我不知道为什么这行不通。

dim dt as long, i as long, j as long

for i=5 to 100
    For j = 4 To 730
        dt = Sheet1.Cells(4, j).value2
        If weekday(dt) = 6 and dt >= sat(lbound(sat)) and dt <= sat(ubound(sat)) Then
            Sheet1.Cells(i, j) = 0
        end if
    next j
next i

You could also try the .Filter method of an array. 您也可以尝试使用数组的.Filter方法

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

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