返回列表 上一主題 發帖

[發問] 關於 Find用法

回復 1# Helain
  1. Sub Ex()
  2.     Dim Myrange As Range, Rng As Range
  3.     Set Myrange = Range(Cells(6, 2), Cells(8, 4))
  4.     Set Rng = Myrange.Find(Cells(3, 2), , xlValues, xlWhole)
  5.     If Not Rng Is Nothing Then
  6.         Cells(1, 1) = Rng.Row
  7.         Cells(2, 1) = Rng.Column
  8.     End If
  9. End Sub
  10. '''''''''''''''''''''''''''''''''''
  11. Sub test()
  12. Dim Myrange As Range
  13. 'Set Myrange = Range(Cells(6, 2), Cells(8, 2))  '這裡的 Cells(8, 2) 是 B8
  14. Set Myrange = Range(Cells(6, 2), Cells(8, 4))   '這裡的 Cells(8, 4) 是 D8
  15. '尋找不到   錯誤顯示: '沒有設定物件變數或with 區塊變數
  16. Cells(1, 1) = Myrange.Find(Cells(3, 2), , xlValues, xlWhole).Row
  17. Cells(2, 1) = Myrange.Find(Cells(3, 2), , xlValues, xlWhole).Column
  18. End Sub
複製代碼

TOP

本帖最後由 GBKEE 於 2011-11-10 08:22 編輯

回復 6# Helain

請任意輸入 "aaaaaaa"
RMax_Row = Cells.Find("aa", , xlValues, xlPart).Row
RMax_Row = Cells.Find("aa", , xlFormulas, xlPart).Row                  有找到:這兩程式碼沒錯誤

RMax_Row = Cells.Find("aa", , xlValues, xlWhole).Row
RMax_Row = Cells.Find("aa", , xlFormulas, xlWhole).Row           沒有找到:這兩程式碼會錯誤    陣列索引超出範圍

請不要用上面的語法 正確的寫法如下
expression.Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection, MatchCase, MatchByte, SearchFormat)
Set R_Max = R_Profit.Find(What:=Rmax,LookIn:= xlFormulas,LookAt:= xlWhole)
Set R_Max = R_Profit.Find(LookIn:=xlFormulas, LookAt:=xlWhole, What:=Rmax)  
全部有指明參數可任意放置參數的位置 但不可以 參雜著有指明參數 及沒指明的參數

Set R_Max = R_Profit.Find(Rmax, ,  xlValues, xlWhole)  
Set R_Max = R_Profit.Find(Rmax, , xlFormulas, xlWhole)

如確定 先前的搜尋不是註解   LookIn:=xlComments 可用
Set R_Max = R_Profit.Find(Rmax, ,  , xlWhole)
    If Not R_Max Is Nothing Then
        R_MaxRow = R_Max.Row
        R_Maxcolumn = R_Max.Column
    End If

TOP

本帖最後由 GBKEE 於 2011-11-10 14:10 編輯

回復 9# Helain
請檢查一下語法的符號
語法正確時 第1個字母會是大寫的      Row = QrF.Row   
不建議 用關鍵字做變數  Row,Column ...
  1. If Not QrF Is Nothing Then
  2.         Row = QrF.Row
  3.         Column = QrF.Column
  4.     End If
複製代碼

TOP

本帖最後由 GBKEE 於 2011-11-10 15:16 編輯

回復 11# Helain
Find 搜尋的條件如果沒重新設定 會依照上次的搜尋條件來尋找的
Set QrF = R_Profit.Find(Application.Max(R_Profit), , , xlWhole)    這語法沒指定是 (公式 內容 註解)  有指定儲存格的內容需完全相同

請查看7樓 Hsieh超版的圖片, 查查看  你 Excel上次的搜尋條件是 (公式 內容 註解).

TOP

本帖最後由 GBKEE 於 2011-11-10 15:26 編輯

回復 13# Helain
Find 搜尋的條件如果沒重新設定 會依照上次的搜尋條件來尋找的
Set QrF = R_Profit.Find(a, , xlFormulas, xlWhole)
你尋找的是數字 與 工作表上顯示出的的文字(數字)不一樣
  1. Sub test_3()
  2. Dim R_Profit As Range        'R's 2nd profit table
  3. Dim M_Profit As Range        'M's 2nd profit table
  4. Dim M_MaxProfit As Range     'For different Qr, M's Max profit
  5. Set R_Profit = Range(Cells(20, 2), Cells(30, 12))
  6. Set M_Profit = Range(Cells(2, 2), Cells(12, 12))
  7. Set M_MaxProfit = Range(Cells(15, 2), Cells(15, 12))
  8. MsgBox Application.Max(R_Profit)
  9. Set QrF = R_Profit.Find(Application.Max(R_Profit), , xlFormulas, xlWhole)
  10. MsgBox QrF.Text
  11.     If Not QrF Is Nothing Then
  12.         R = QrF.row
  13.         C = QrF.column
  14.     End If
  15. End Sub
複製代碼

TOP

回復 15# Helain
如果說 range 裡面的值剛好有數個相同的, 那find 可以讀嗎?是隨便讀取其中一個?
自己寫寫 試試看.

另外, 哪邊有VBA所有函數, 以及說明呢?
VBA 指令說明 從中的範例 去一一體會

TOP

        靜思自在 : 人生沒有所有權,只有生命的使用權。
返回列表 上一主題