即使将Application.EnableEvents设置为False,滚动条更改事件仍然会触发

时间:2018-12-15 12:53:58

标签: excel vba excel-vba

问题在于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

0 个答案:

没有答案