Private WithEvents SpinBtn As MSForms.SpinButton Private dVal#, dShift#, num As Byte Public self As ClsSpinBtns Public Property Set OleObj(obj As OLEObject) Set SpinBtn = obj.Object dVal = SpinBtn.Parent.Range(SpinBtn.LinkedCell).Value dShift = val(Replace(Replace(SpinBtn.Name, "*", "", InStr(SpinBtn.Name, "_") + 1), "_", ".")) num = IIf(dShift \ 1 = dShift / 1, 0, Len(Trim(dShift)) - InStr(Trim(dShift), ",")) Set self = Me End Property Private Sub SpinBtn_SpinUp() If dShift = 0 Then Exit Sub Application.EnableEvents = False With SpinBtn.Parent.Range(SpinBtn.LinkedCell) Dim v#: v = Round(dVal + dShift, num) .Value = IIf(v <= SpinBtn.Max, v, .Value) dVal = .Value End With Application.EnableEvents = True End Sub Private Sub SpinBtn_SpinDown() If dShift = 0 Then Exit Sub Application.EnableEvents = False With SpinBtn.Parent.Range(SpinBtn.LinkedCell) Dim v#: v = Round(dVal - dShift, num) .Value = IIf(v >= SpinBtn.Min, v, .Value) dVal = .Value End With Application.EnableEvents = True End Sub
[/vba]
[vba]
Код
Public col As Collection Public Sub init() If Not col Is Nothing Then For Each itm In col Set itm.self = Nothing Next End If Set col = New Collection Dim Sh As Worksheet, obj As OLEObject For Each Sh In Sheets For Each obj In Sh.OLEObjects If obj.progID = "Forms.SpinButton.1" Then With New ClsSpinBtns Set .OleObj = obj col.Add .self, Sh.Range(obj.LinkedCell).Address(, , , 1) End With End If Next obj, Sh End Sub
[/vba]
[vba]
Код
Private Sub Workbook_Open() Call init End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim cell As Range On Error Resume Next For Each cell In Target If Not col(cell.Address(, , , 1)) Is Nothing Then Call init Next End Sub
[/vba]
подсказки по использованию в файле
upd. Заменил файл
еще вариант, для activex spinbutton'ов
[vba]
Код
Private WithEvents SpinBtn As MSForms.SpinButton Private dVal#, dShift#, num As Byte Public self As ClsSpinBtns Public Property Set OleObj(obj As OLEObject) Set SpinBtn = obj.Object dVal = SpinBtn.Parent.Range(SpinBtn.LinkedCell).Value dShift = val(Replace(Replace(SpinBtn.Name, "*", "", InStr(SpinBtn.Name, "_") + 1), "_", ".")) num = IIf(dShift \ 1 = dShift / 1, 0, Len(Trim(dShift)) - InStr(Trim(dShift), ",")) Set self = Me End Property Private Sub SpinBtn_SpinUp() If dShift = 0 Then Exit Sub Application.EnableEvents = False With SpinBtn.Parent.Range(SpinBtn.LinkedCell) Dim v#: v = Round(dVal + dShift, num) .Value = IIf(v <= SpinBtn.Max, v, .Value) dVal = .Value End With Application.EnableEvents = True End Sub Private Sub SpinBtn_SpinDown() If dShift = 0 Then Exit Sub Application.EnableEvents = False With SpinBtn.Parent.Range(SpinBtn.LinkedCell) Dim v#: v = Round(dVal - dShift, num) .Value = IIf(v >= SpinBtn.Min, v, .Value) dVal = .Value End With Application.EnableEvents = True End Sub
[/vba]
[vba]
Код
Public col As Collection Public Sub init() If Not col Is Nothing Then For Each itm In col Set itm.self = Nothing Next End If Set col = New Collection Dim Sh As Worksheet, obj As OLEObject For Each Sh In Sheets For Each obj In Sh.OLEObjects If obj.progID = "Forms.SpinButton.1" Then With New ClsSpinBtns Set .OleObj = obj col.Add .self, Sh.Range(obj.LinkedCell).Address(, , , 1) End With End If Next obj, Sh End Sub
[/vba]
[vba]
Код
Private Sub Workbook_Open() Call init End Sub Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range) Dim cell As Range On Error Resume Next For Each cell In Target If Not col(cell.Address(, , , 1)) Is Nothing Then Call init Next End Sub