2015-11-09 53 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長期與這個問題摔跤 –