2015-11-09 55 views
2

我有一个.find命令有问题。我想创建一个宏与find命令找到一个特定的细胞后复制范围并将其粘贴(偏移到活动单元格移动到数据值):使用excel查找时间序列号

Sub value() 
Dim today As String 
Dim lookfor As Range 

Sheets(1).Range("C3:C19").Copy 
today = "11.nov" 

Set lookfor = Cells.Find(What:=today, _ 
      After:=ActiveCell, _ 
      LookIn:=xlValues, _ 
      LookAt:=xlPart, _ 
      SearchOrder:=xlByColumns, _ 
      SearchDirection:=xlNext, _ 
      MatchCase:=False, _ 
      SearchFormat:=False).Activate 

lookfor.Offset(rowOffset:=1, columnOffset:=3).Paste 
End Sub 
+0

您的代码似乎这样的伎俩,只是改变'otsitav.Offset(...)''来lookfor.Offset(...) – R3uK

+0

这是'翻译错误。对不起,但代码仍然给出错误nr91:Object variable或With block variable not set。我试图让工作簿成为一个对象,但它仍然给出了错误 –

+2

摆脱'SetFile'末尾的'.Activate',你的代码应该正常工作! ;) – R3uK

回答

0

在事实,你必须检查在使用它之前,有一个Find方法与If Not LookFor Is Nothing Then的结果。

所以我的猜测是Find方法没有找到你想要的值的任何东西。

这是您的修改代码:

Sub test_Veiko_Aunapuu() 
Dim FirstAddress As String, _ 
    ToDay As String, _ 
    LookFor As Range 

ToDay = "11.nov" 

Sheets(1).Activate 
Sheets(1).Range("C3:C19").Copy 

With Sheets(1).Cells 
    '----First, define properly the Find method 
    Set LookFor = .Find(What:=ToDay, _ 
       After:=ActiveCell, _ 
       LookIn:=xlValues, _ 
       LookAt:=xlPart, _ 
       SearchOrder:=xlByColumns, _ 
       SearchDirection:=xlNext, _ 
       MatchCase:=False, _ 
       SearchFormat:=False) 

    If Not LookFor Is Nothing Then 
    '----If there is a result, 
     FirstAddress = LookFor.Address 
     'LookFor.Activate 
     MsgBox "The row containing " & ToDay & " is : " & LookFor.Row 
     'Keep looking with FindNext method : Not usefull for your example 
     Do 
      '------------------------------------------------------------- 
      '----Place instructions to execute on the matched cell/row/... 
      LookFor.Offset(rowOffset:=1, columnOffset:=3).Paste 

      '------------------------------------------------------------- 
      Set LookFor = .FindNext(LookFor) 
     'Loop and keep looking until you find again the first result 
     Loop While Not LookFor Is Nothing And LookFor.Address <> FirstAddress 
    Else 
    '----If there is no results, say it 
     MsgBox "No matches were found for : " & ToDay, vbCritical + vbOKOnly, "No results" 
    End If 
End With 

End Sub 
+0

删除了激活但仍然是相同的错误。问题是它没有设置单元格值。 @BruceWayne我试图将其设置为地址,但它表示类型不匹配(我检查了拼写)。 –

+0

@VeikoAunapuu:我编辑我的答案,这可能会帮助你确定你的问题!如果没有匹配,尝试更改'Find'参数,以'LookIn:= xlFormulas'开始。让我知道! ;) – R3uK

+0

Tnahk你@ R3uK长期与这个问题摔跤 –