简体   繁体   English

excel宏cells.find由于某种原因不会使用变量

[英]excel macro cells.find won't use variable for some reason

I have this code as an excel macro. 我将此代码作为excel宏。

  Dim MyData As DataObject
  Set MyData = New DataObject
  MyData.GetFromClipboard

  MsgBox MyData.GetText

  On Error GoTo NotFound
  Cells.Find(What:=MyData.GetText, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
  :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
  False, SearchFormat:=False).Activate

  NotFound:

MsgBox writes the value correctly, but the What:=MyData.GetText just won't work. MsgBox正确写入值,但What:=MyData.GetText不起作用。 Why? 为什么? Or, more importantly, how to fix it? 或者,更重要的是,如何解决它?

If it is not clear, I am trying to find the next value equal to the one in clipboard at the moment. 如果不清楚,我试图找到下一个等于剪贴板中的值。 I have referenced the MSForms so that is not the problem. 我引用了MSForms,这不是问题所在。

If I assign MyData.GetText to a variable and use the variable, the same happens, MsgBox works, BUT What:=myvariable doesn't. 如果我将MyData.GetText分配给变量并使用该变量,同样的情况发生, MsgBox工作,但是What:=myvariable没有。

Not sure why this works, but it does: 不知道为什么会这样,但确实如此:

Sub test()
Dim MyData As DataObject
Dim rng As Excel.Range

Set MyData = New DataObject
MyData.GetFromClipboard
Set rng = ActiveSheet.Cells.Find(What:=MyData.GetText, After:=ActiveCell, LookIn:=xlFormulas, LookAt _
                                 :=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
                                 False, SearchFormat:=False)

rng.Activate
End Sub

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

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