[英]how can i make the following code work with both ascending and descending numbers
我正在使用下面的代碼在8個[數量]字段中找到一個值,該值與[制造數量]字段中的值最接近。 如果[qty]字段中的值是遞增值,則代碼可以按我的要求工作。 例如。 10,20,30,40,50,60,70,80
如果值降序或混合,則代碼將失敗。 80,70,60,50,40,30,20,10或10,30,50,40,70,20,80等
任何人都可以建議如何更好地處理此問題
If Me![Make Quantity] <= Me![qty1] Then
materialprice = Me![raw1]
ElseIf Me![Make Quantity] <= Me![qty2] And Me![Make Quantity] > Me![qty1] Then
materialprice = Me![raw1]
ElseIf Me![Make Quantity] <= Me![qty3] And Me![Make Quantity] > Me![qty2] Then
materialprice = Me![raw2]
ElseIf Me![Make Quantity] <= Me![qty4] And Me![Make Quantity] > Me![qty3] Then
materialprice = Me![raw3]
ElseIf Me![Make Quantity] <= Me![qty5] And Me![Make Quantity] > Me![qty4] Then
materialprice = Me![raw4]
ElseIf Me![Make Quantity] <= Me![qty6] And Me![Make Quantity] > Me![qty5] Then
materialprice = Me![raw5]
ElseIf Me![Make Quantity] <= Me![qty7] And Me![Make Quantity] > Me![qty6] Then
materialprice = Me![raw6]
ElseIf Me![Make Quantity] <= Me![qty8] And Me![Make Quantity] > Me![qty7] Then
materialprice = Me![raw7]
Else
materialprice = Me![raw8]
End If
更新!! 更改了代碼,以便如果QTY <最低,則默認為最低; 如果>最高,則使用最高。
這是我放在一起的一些代碼...由於沒有您的數據,我將測試數據加載到8對文本框中(使用您的字段名)。 請注意,我在“制造數量”文本框中使用了“更改”事件來調用代碼。 您可以強制執行,但是您可以...
一旦滿意,請注釋掉Debug語句和MsgBox。
Option Compare Database
Option Explicit
Private Sub Form_Load()
Dim i As Integer
Dim MakeQuantity As Integer
Dim materialprice As Double
Me.qty1 = 30: Me.qty2 = 20: Me.qty3 = 70: Me.qty4 = 50: Me.qty5 = 10: Me.qty6 = 60: Me.qty7 = 80: Me.qty8 = 70
Me.raw1 = 3.3: Me.raw2 = 2.2: Me.raw3 = 7.7: Me.raw4 = 5.5: Me.raw5 = 1.1: Me.raw6 = 6.6: Me.raw7 = 8.8: Me.raw8 = 7.7
End Sub
Private Sub Make_Quantity_AfterUpdate()
Dim i As Integer
Dim iDiff As Integer
Dim iLow As Integer
Dim iMatch As Integer
Dim iHigh As Integer
Dim dPriceL As Double
Dim dPriceH As Double
Dim dPriceM As Double
Dim iQtyL As Integer
Dim iQtyH As Integer
iLow = 30000
iHigh = 0
iMatch = 10
Debug.Print "Find Qty of: " & Me.[Make Quantity]
For i = 1 To 8
If Int(Me("qty" & i)) <= iLow Then
iLow = Int(Me("qty" & i))
dPriceL = Me("raw" & i)
iQtyL = Int(Me("qty" & i))
End If
If Int(Me("qty" & i)) >= iHigh Then
iHigh = Int(Me("qty" & i))
dPriceH = Me("raw" & i)
iQtyH = Int(Me("qty" & i))
End If
iDiff = Abs(Me.[Make Quantity] - Me("qty" & i))
If Int(Me("qty" & i)) <= Int(Me.[Make Quantity]) Then
If iDiff <= iMatch Then
iMatch = iDiff
dPriceM = Me("raw" & i)
End If
End If
Debug.Print "i: " & i & vbTab & "Qty: " & Me("qty" & i) & vbTab & "Diff: " & iDiff & vbTab & "Raw: " & Me("raw" & i)
Next i
If dPriceM <> 0 Then ' Did we find a suitable match?
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceM
Else ' Didn't find a good match; must be < lowest or > highest
If iQtyL > Int(Me.[Make Quantity]) Then ' Greater than lowest QTY, use Lowest QTY price...
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceL
ElseIf iQtyH < Int(Me.[Make Quantity]) Then ' Greater than highest QTY, use highest QTY price...
MsgBox "Make Quantity: " & Me.[Make Quantity] & vbCrLf & "Price: " & dPriceH
Else
MsgBox "Impossible? Asked for Qty of: " & Me.[Make Quantity] & vbCrLf & _
"Lowest Qty: " & iQtyL & vbTab & _
"Highest Qty: " & iQtyH
End If
End If
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.