问题在于Me.scrPieces.Min = frmSettings.tbxPiecesLow.Value
激活了scrPieces_Change()
事件,因为滚动条的默认值可能是滚动条的设置最小值。因此,更改滚动条的最小值通常会导致该值发生偏移,从而触发滚动条更改事件。我正在考虑在Application.EnableEvents
事件中使用UserForm_Initialize()
来禁用事件的激活。
但是尽管有Application.EnableEvents = False
,但scrPiecesChange()
事件仍在触发。
Private Sub UserForm_Initialize()
Application.EnableEvents = False
'scrPieces
Me.scrPieces.Min = frmSettings.tbxPiecesLow.Value
Me.scrPieces.Max = frmSettings.tbxPiecesHigh.Value
Me.scrPieces.SmallChange = Application.WorksheetFunction.RoundUp((frmSettings.tbxPiecesHigh.Value - frmSettings.tbxPiecesLow.Value) / 40, 0)
Me.scrPieces.LargeChange = Application.WorksheetFunction.RoundUp((frmSettings.tbxPiecesHigh.Value - frmSettings.tbxPiecesLow.Value) / 8, 0)
Application.EnableEvents = True
End Sub
滚动条更改事件:
Private Sub scrPieces_Change()
i = Me.scrPieces.Min
j = 0
Me.tbxPiecesD = Me.scrPieces.Value
Do While i <= Me.scrPieces.Max
ThisWorkbook.Worksheets("Simulation-Chart").Cells(2 + j, 1) = i
ThisWorkbook.Worksheets("Simulation-Chart").Cells(2 + j, 2) = (CostPerPiece * (Me.scrFoilMarkup.Value / BigNum) + LaborCostPerPiece * (Me.scrLaborMarkup.Value / BigNum)) * (Me.scrQuoteMarkup.Value / BigNum)
i = i + (Me.scrPieces.Max - Me.scrPieces.Min) / 10
j = j + 1
Loop
End Sub