2012-09-19 51 views
-1

哈哈!我會幫你破壞Excel的大師們。 ; -D如何在列中查找目標值?

我想根據一些分隔文本設置選擇範圍邊界。

如果我做了Cells.Find,則搜索整個工作表並找到多個實例。 我想要找到的人可能是分隔符的第3或第4個實例。實際上,它是在一個特定的列中,但是它是一個非連續的範圍,並且列中的實際搜索開始是幾百個單元格。

如何在該列中搜索並設置我的可重用範圍開始變量,設置爲分隔符單元格(不包括分隔符單元格)? 我試過這個:

Dim selectionStart As Range, selectionEnd As Range 
Dim currentCell As Range, dataRange As Range 
Dim lastRow As Range, insertRows As Range, destinationCell As Range 

Range("b1", Range("b65536").End(xlUp)).Select 

Set selectionStart = Selection.Find(What:="<-RANGE START->", After:=ActiveCell, LookIn _ 
    :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
    xlNext, MatchCase:=False, SearchFormat:=False) 

它選擇的範圍,但沒有設置變量。

我正在嘗試所有這些煩人的事情,所以我可以看到他們沒有太多的眼睛疲勞。優雅不是必需的。

TIA

+2

也許它不設置變量,因爲它不能找到你想要的?這將有助於顯示更多的代碼,在Tim Find Williams指出'Find()' –

回答

1

您的意思是?

Option Explicit 

Sub test() 

Dim selectionStart As Range, selectionEnd As Range 
Dim currentCell As Range, dataRange As Range 
Dim lastRow As Range, insertRows As Range, destinationCell As Range 
Dim rngtoSearch As Range 
Dim foundValue As Variant 
Dim foundAddress As String 
Dim foundRow As Long 

With sheetWhatever 'change to whatever sheet codename required 
    Set rngtoSearch = .Range("b1", .Range("b65536").End(xlUp)) 

    Set selectionStart = rngtoSearch.Find(What:="<-RANGE START->", LookIn _ 
     :=xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _ 
     xlNext, MatchCase:=False, SearchFormat:=False) 

    'check it actually found a range 
    If Not selectionStart Is Nothing Then 
     'If found set the variable 
     foundValue = selectionStart.Value 'set as value 
     foundAddress = selectionStart.Address 'set as address string 
     foundRow = selectionStart.Row ' set as row 
    End If 

End With 


End Sub 
+0

的調用之後,我在發佈之後發現了它。 有一個初步的宏,我還沒有運行。 現在工作。再一次,我沒能阻止你。 而這一個是一個非自願的技巧問題.... –