使用VBA在特定Excel行打开 [英] Opening at a specific Excel Row using VBA

查看:107
本文介绍了使用VBA在特定Excel行打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子表格,在B栏中有一个滚动日期,它总是打开,毫不奇怪,在顶部。



我已将以下代码放入ThisWorkbook,但我在Set行上收到错误...



运行时错误'91';



对象变量或没有设置块变量




有人可以告诉我我做错了吗?!?



I have a spreadsheet that has a rolling date in Column B and it always opens, unsurprisingly, at the top.

I've put the following code into ThisWorkbook but I am getting an error on the Set line ...

Run-time error '91';

Object variable or With block variable not set


Can somebody please tell me what I'm doing wrong ?!?

Private Sub Workbook_Open()

Dim myDate As Date
Dim myCell As Range

myDate = Date

With Worksheets("Sheet1")
    Set myCell = Columns(2).Find(What:=myDate, LookIn:=xlValues).Activate
End With

Application.Goto Selection, True


End Sub

推荐答案

问题在于查找未能找到日期,因此针对空对象发出 .Activate



首先尝试检查结果,例如
The problem is that the Find has failed to find the date, so the .Activate is being issued against a null object.

Try checking the result first e.g.
With Worksheets("Sheet1")
    Set myCell = Columns(2).Find(What:=myDate, LookIn:=xlValues)
    If Not myCell Is Nothing Then
        myCell.Activate
    End If
End With


这篇关于使用VBA在特定Excel行打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆