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

时间:2018-03-20 23:43:45

标签: arrays vba excel-vba excel

我正在编写一个小宏来完成一项简单的任务。到目前为止我已经取得了一些进展,但是我仍然试图做一个if语句来检查一个单元格的内容是否等于一个数组中的字符串,然后它执行下一个语句。到目前为止,这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

数组没有简单的方法来查看整个组。你必须一次检查一个。 取代

static char _externalStringBuffer[MAX_BUFFER_SIZE];
char *get() {
    // put characters in _externalStringBuffer
    return _externalStringBuffer;
}

使用

{{1}}

答案 1 :(得分:0)

我不知道为什么这不起作用。

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

您也可以尝试数组的.Filter method