在表格中查找日期

时间:2018-05-02 10:22:49

标签: vba excel-vba date find excel

我有一张桌子:

Sheet name: 'DateSheet'
Table name: 'DateTable'

| Date       | First Field    | Second Field     | ...
+------------+----------------+------------------+-----
| 2018-01-01 | ...            | ...              | ...
| 2018-01-02 | ...            | ...              | ...
| 2018-01-03 | ...            | ...              | ...
| 2018-01-04 | ...            | ...              | ...

日期全部采用Custom -> YYYY-MM-DD格式。

我想选择与特定日期对应的行。我编写了以下代码,但它不起作用:

Dim DateTable As Range
Set DateTable = DateSheet.ListObjects("DateTable").Range

Debug.Print Date                            // Returns "2018-05-02", today's date
Debug.Print DateTable.Address               // Returns "$B$5:$P$300", the correct Range address
Debug.Print DateTable.Find("Date").Address  // Returns "$B$5", the correct row
Debug.Print DateTable.Cells(123,1).Value    // Returns "2018-05-02", today's date

// Everything seems to be OK, until now, where I get:
// Err: "Object variable or With block variable not set" :
Debug.Print DateTable.Find(Date).Address    
Debug.Print DateTable.Find(CDate(Date)).Address
Debug.Print DateTable.Find(Format(Date, "YYYY-MM-DD")).Address

0 个答案:

没有答案