iT邦幫忙

0

excel vba 多條件查詢,有相同條件時,如何找下一筆

  • 分享至 

  • xImage

請教各位前輩,用vba多條件查詢,有相同條件時,如何找下一筆,將資料[拍賣價格]轉到[目的檔][拍賣價格].
https://ithelp.ithome.com.tw/upload/images/20200529/20080211RDg4JPWowV.jpg

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-05-30 15:50:07
最佳解答

請參考
VBA 找下一筆的官方教學

我抄上面的教學
寫了 TypeOne 和 TypeTwo 兩種用法
看你喜歡那一個就拿去用

'只用 SetOnePrice 就可以設定所有拍賣價格
Sub TypeOne()
    For nI = 3 To 10
        sCodeKind = Range("A" & nI).Value
        sPersonNo = Range("B" & nI).Value
        sPieces = Range("C" & nI).Value
        sTotalWeight = Range("D" & nI).Value
        sBidPrice = Range("E" & nI).Value
        Call SetOnePrice(sCodeKind, sPersonNo, sPieces, sTotalWeight, sBidPrice)
    Next nI
End Sub
'用 FindNext 找出並設定數值
Sub TypeTwo()
    Call FindAll("F1", "005", 1, 20)
End Sub
'找到第一筆沒有符合條件並且沒有拍賣價格的, 把拍賣價格設定上去
Sub SetOnePrice(ByVal CodeKind, ByVal PersonNo, ByVal Pieces, ByVal TotalWeight, ByVal bidPrice)
    Dim Found As Range, Firstfound As String
    Dim rngSearch As Range
    Dim Criteria As Variant
    
    Set rngSearch = Sheets("Sheet1").Range("G:G")
            
    Set Found = rngSearch.Find(What:=CodeKind, _
                               LookIn:=xlValues, _
                               LookAt:=xlWhole, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlNext, _
                               MatchCase:=False)
        
    If Not Found Is Nothing Then
        
        Firstfound = Found.Address
        
        Do
            If Found.EntireRow.Range("H1").Value = PersonNo And _
               Found.EntireRow.Range("I1").Value = Pieces And _
               Found.EntireRow.Range("J1").Value = TotalWeight And _
               Found.EntireRow.Range("K1").Value = "" Then
               
               Debug.Print Found.Address, bidPrice
               Found.EntireRow.Range("K1").Value = bidPrice
               Exit Do
            End If 'Match found
            
            Set Found = rngSearch.FindNext(After:=Found)
            If Found.Address = Firstfound Then Set Found = Nothing
            
        Loop Until Found Is Nothing
        
    End If
'    If Not Found Is Nothing Then
'        Debug.Print "Found"
'    Else
'        Debug.Print "Nothing matched all four criteria. ", , "No Match Found"
'    End If
End Sub
'找出所有符合條件的, 然後去目的檔搜尋並且設定拍賣價格
Sub FindAll(ByVal CodeKind, ByVal PersonNo, ByVal Pieces, ByVal TotalWeight)
    Dim Found As Range, Firstfound As String
    Dim rngSearch As Range
    Dim Criteria As Variant
    
    Set rngSearch = Sheets("Sheet1").Range("A:A")
    
    Set Found = rngSearch.Find(What:=CodeKind, _
                               LookIn:=xlValues, _
                               LookAt:=xlWhole, _
                               SearchOrder:=xlByRows, _
                               SearchDirection:=xlNext, _
                               MatchCase:=False)
        
    If Not Found Is Nothing Then
        
        Firstfound = Found.Address
        
        Do
            If Found.EntireRow.Range("B1").Value = PersonNo And _
               Found.EntireRow.Range("C1").Value = Pieces And _
               Found.EntireRow.Range("D1").Value = TotalWeight Then
               
               bidPrice = Found.EntireRow.Range("E1").Value
               'Debug.Print Found.Address, bidPrice
               Call SetOnePrice(CodeKind, PersonNo, Pieces, TotalWeight, bidPrice)
               'Exit Do
            End If 'Match found
            
            Set Found = rngSearch.FindNext(After:=Found)
            If Found.Address = Firstfound Then Set Found = Nothing
            
        Loop Until Found Is Nothing
        
    End If
    
    If Not Found Is Nothing Then
        Debug.Print "Found"
    Else
        Debug.Print "Nothing matched all four criteria. ", , "No Match Found"
    End If
End Sub
看更多先前的回應...收起先前的回應...
dou10801 iT邦新手 5 級 ‧ 2020-06-01 08:13:28 檢舉

海綿大大是否能舉一個範本作參考,因為有相同要找下一筆相同多條件的值,感恩.

dou10801 iT邦新手 5 級 ‧ 2020-06-17 12:49:07 檢舉

大大,我的困擾是,農戶出貨經常有些現象,[小代號]+[品種]+[件數]+[總重]一樣(相同多條件),但價格不同,我只會簡單的多條件取值,但如果[小代號]+[品種]+[件數]+[總重]一樣,有二筆以上資料,[拍賣價格]不同時,要如何取值(我遇過有五筆相同條件,拍賣價格不一樣,如何把五筆拍賣格對應出來),懇求指點,謝謝.
https://ithelp.ithome.com.tw/upload/images/20200617/20080211D3a0E66geM.jpg

先Find一個值F1得到位址為 Found
其他的用相對位置做比對,即
Found.EntireRow.Range("B1").Value = "005"
Found.EntireRow.Range("C1").Value = 1
Found.EntireRow.Range("D1").Value = 20

詳細程式範例請看這篇
應該可以解決你的問題

dou10801 iT邦新手 5 級 ‧ 2020-06-18 16:26:57 檢舉

海綿寶寶前輩,非常抱歉,我測試該網頁,因本人對VBA語法知識太淺,無法融會貫通,請大大進一步指點,感恩.

本人對VBA語法知識太淺

如果你不會 VBA 的話
那我在上面寫這一堆 VBA 有幫助嗎?

dou10801 iT邦新手 5 級 ‧ 2020-06-19 09:03:19 檢舉

非常非常感謝海綿寶寶前輩,我只會簡單的VBA基本對應常識,老師給我的經典範本,我會好好學習,慢慢消化,不止是語法,[邏輯思維]才是我應加強的地方,再次感謝.

我要發表回答

立即登入回答