返回列表 上一主題 發帖

[發問] 多條件的VLOOPUP

[發問] 多條件的VLOOPUP

各大大:

請問如何將表1的資料, 以函數放置在表2, 因為要以兩個條件使用VLOOKUP (第一組條件時段及薪金, 第二組是條件時段及加班)                       
抱歉因權限不足未能上傳檔案:(
               
表1       
A                      B                                     C
                        時段                                金額       
薪金                 2013年1月-2月        10000       
                         2013年3月-5月        20000       
                         2013年5月                        25000       
加班                 2013年1月-5月        1000       
                       
                       
表2                       
A                      B             C
時段                 薪金         加班       
2013年1月        10000        1000       
2013年2月        10000        1000       
2013年3月        20000        1000       
2013年4月        20000        1000       
2013年5月        25000        1000

回復 1# missbb

不能直接上載excel檔, 要壓成ZIP再上載.
ss

TOP

回復 1# missbb

表1的資料中,時段用區段方式記錄,不合乎資料庫準則
要做分月查詢是不能正確得到
學海無涯_不恥下問

TOP

表1        
A                      B                                     C
                        時段                                金額        
薪金                 2013年1月-2月        10000        
                         2013年3月-5月        20000        
                         2013年5月                        25000        
加班                 2013年1月-5月        1000     

紅色字 5月是否應改為 4月
{...} 表示需要用 CTRL+SHIFT+ENTER 三鍵輸入公式

TOP

回復 4# ML089

:lol 是呢, 打錯了.

TOP

這個用VLOOKUP太複雜了
用VBA處理可能會比較簡單

TOP

回復 6# nissin


   明白, 已在學習中!:P

TOP

試試這個
  1. Sub copydata()
  2. Dim s1 As Long, s2 As Long, s3 As Long, d1 As Long, d2 As Long
  3. Dim wages As Long, OT As Long

  4. i = 1
  5. While Sheets(1).Cells(i, 1) <> "薪金"
  6.     i = i + 1
  7. Wend
  8. wages = i

  9. While Sheets(1).Cells(i, 1) <> "加班"
  10.     i = i + 1
  11. Wend
  12. OT = i

  13. For i = wages To Sheets(1).UsedRange.Rows.Count
  14.     s1 = Left(Sheets(1).Cells(i, 2), 4)
  15.     s2 = Mid(Sheets(1).Cells(i, 2), 6, InStr(1, Sheets(1).Cells(i, 2), "月") - 5 - 1)
  16.     If InStr(1, Sheets(1).Cells(i, 2), "-") <> 0 Then
  17.         s3 = Mid(Sheets(1).Cells(i, 2), InStr(1, Sheets(1).Cells(i, 2), "-") + 1, Len(Sheets(1).Cells(i, 2)) - (InStr(1, Sheets(1).Cells(i, 2), "-") + 1))
  18.     Else
  19.         s3 = 0
  20.     End If
  21.     For j = 2 To Sheets(2).UsedRange.Rows.Count
  22.         d1 = Left(Sheets(2).Cells(j, 1), 4)
  23.         d2 = Mid(Sheets(2).Cells(j, 1), 6, InStr(1, Sheets(2).Cells(j, 1), "月") - 1 - 5)
  24.         
  25.         If d1 * 100 + d2 >= s1 * 100 + s2 Then
  26.             If s3 <> 0 Then
  27.                 If d1 * 100 + d2 <= s1 * 100 + s3 Then
  28.                     If i >= OT Then
  29.                         Sheets(2).Cells(j, 3) = Sheets(1).Cells(i, 3)
  30.                     Else
  31.                         Sheets(2).Cells(j, 2) = Sheets(1).Cells(i, 3)
  32.                     End If
  33.                 End If
  34.             Else
  35.                 If i >= OT Then
  36.                     Sheets(2).Cells(j, 3) = Sheets(1).Cells(i, 3)
  37.                 Else
  38.                     Sheets(2).Cells(j, 2) = Sheets(1).Cells(i, 3)
  39.                 End If
  40.             End If
  41.         End If
  42.     Next j
  43. Next i
  44. End Sub
複製代碼

TOP

回復 8# nissin
多謝您呀 !:D

TOP

本帖最後由 Hsieh 於 2013-11-26 23:43 編輯

回復 1# missbb
你是要整理資料成為資料庫型態吧
  1. Sub ex()
  2. Dim OT$, Ary(), r&, y$, a$, i%, s&
  3. Set dic = CreateObject("Scripting.Dictionary")
  4. Set dic1 = CreateObject("Scripting.Dictionary")
  5. r = 2
  6. With Sheets(1)
  7. Do Until .Cells(r, 2) = ""
  8. OT = IIf(.Cells(r, 1) <> "", .Cells(r, 1), OT)
  9. y = Split(.Cells(r, 2), "年")(0)
  10. a = Split(.Cells(r, 2), "年")(1)
  11.    If InStr(a, "-") > 0 Then
  12.    ar = Split(a, "-")
  13.    For i = Val(ar(0)) To Val(ar(1))
  14.       dic(y & "年" & i & "月" & OT) = .Cells(r, 3)
  15.       dic1(y & "年" & i & "月") = ""
  16.    Next
  17.    Else
  18.    dic(y & "年" & a & OT) = .Cells(r, 3)
  19.    n = .Cells(r, 2)
  20.    dic1(.Cells(r, 2) & "") = ""
  21.    End If
  22.    r = r + 1
  23. Loop
  24. ay = Array("時段", "薪金", "加班")
  25. ReDim Preserve Ary(s)
  26. Ary(s) = ay
  27. s = s + 1
  28. For Each ky In dic1.keys
  29. ReDim Preserve Ary(s)
  30. Ary(s) = Array(ky, dic(ky & ay(1)), dic(ky & ay(2)))
  31. s = s + 1
  32. Next
  33. With Sheets(2)
  34. .Columns("A:C") = ""
  35. .[A1].Resize(s, 3) = Application.Transpose(Application.Transpose(Ary))
  36. End With
  37. End With
  38. End Sub
複製代碼
學海無涯_不恥下問

TOP

        靜思自在 : 君子如水,隨方就圓,無處不自在。
返回列表 上一主題