如果从工作表启动,查找宏不起作用

时间:2018-09-03 12:28:11

标签: excel vba excel-vba

我正在尝试使用vba在当前日期的列中写入“ X”。
如果我从调试窗口运行代码,在该窗口中编写代码,则它按预期运行,但是一旦我尝试通过单击分配给它的矩形来运行它,它就会显示“未找到”。

在此先感谢您的帮助或想法。

代码:

Sub Button()
    Dim wb As Workbook
    Dim ws As Worksheet
    Dim col As Long
    Dim rng As Range

    Set wb = ActiveWorkbook
    Set ws = wb.Sheets("Sheet 2")

    With ws.Cells
        Set rng = .Find(what:=Format(Date))
        If rng Is Nothing Then
            MsgBox "Not found"
            col = 1
        Else
            MsgBox "Found"
            col = rng.Column
        End If
    End With
    ws.Cells(10, col).Value = "X"
    MsgBox "Wrote to Field: " & row & "," & col

End Sub

1 个答案:

答案 0 :(得分:1)

尝试用 CDate(Date)

替换 Format(Date)