返回列表 上一主題 發帖

[發問] 多條件的VLOOPUP

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

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

        靜思自在 : 甘願做、歡喜受。
返回列表 上一主題