此前发布过一个数量单价金额互算的VBA写法,之前的是按用户的需求来写的。最近有其他用户应用此VBA提出了一些问题,所以重新更新了下,希望对有大家有所参考。
单价金额数量互算
- Imports SpreadsheetGear
- Imports System.Windows.forms
- Namespace ExcelViewVBDotnet
- Public Class StandardInterface '自定义名称
- '预留位置1
- '预留位置2
- Dim AW As SpreadsheetGear.IWorkbook,Range As SpreadsheetGear.IRange
- Dim CF,TS,VW As Object,YN As String
- Public Sub Workbook_Open(OldRoot As Object,NewRoot As Object,Excel As SpreadsheetGear.Windows.Forms.WorkbookView)
- Excel.GetLock()
- AW = Excel.ActiveWorkbook
- Range = AW.ActiveWorksheet.Range
- Excel.ReleaseLock()
- '位置:2保存3新增4提交5退回6打印7预览8导出9捕获10上传图片11上传附件12插入行13插多行14删除行15查找16最后
- End Sub'打开时执行事件
- Public Sub RangeSelection(sender As Object,e As SpreadsheetGear.Windows.Forms.RangeSelectionChangedEventArgs)
- End Sub'单元格选择后执行的事件
- Public Sub RangeChanged(sender As Object,e As SpreadsheetGear.Windows.Forms.RangeChangedEventArgs)
- On error resume next
- if e.Range.Address <> YN Then'避免重复触发
- if e.Range.Intersect(Range("F_30")) IsNot Nothing then
- if e.Range.Offset(0,1).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知单价输入数量触发金额
- e.Range.Offset(0,2).Value = CType(e.Range.Offset(0,1).Value,Double) * CType(e.Range.Value,Double)
- 'end if
- elseif e.Range.Offset(0,2).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知金额输入数量触发单价
- if CType(e.Range.Value,Double) <> 0 then
- YN = E.Range.Offset(0,1).Address
- e.Range.Offset(0,1).Value = CType(e.Range.Offset(0,2).Value,Double) / CType(e.Range.Value,Double)
- end if
- end if
- Elseif e.Range.Intersect(Range("F_31")) IsNot Nothing then
- if e.Range.Offset(0,-1).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知数量输入单价触发金额
- YN = E.Range.Offset(0,1).Address
- e.Range.Offset(0,1).Value = CType(e.Range.Offset(0,-1).Value,Double) * CType(e.Range.Value,Double)
- 'end if
- elseif e.Range.Offset(0,1).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知金额输入单价触发数量
- if CType(e.Range.Value,Double) <> 0 then
- YN = E.Range.Offset(0,-1).Address
- e.Range.Offset(0,-1).Value = CType(e.Range.Offset(0,1).Value,Double) / CType(e.Range.Value,Double)
- end if
- end if
- Elseif e.Range.Intersect(Range("F_32")) IsNot Nothing then
- if e.Range.Offset(0,-2).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知数量输入金额触发单价
- if CType(e.Range.Offset(0,-2).Value,Double) <> 0 then
- YN = E.Range.Offset(0,-1).Address
- e.Range.Offset(0,-1).Value = CType(e.Range.Value,Double) / CType(e.Range.Offset(0,-2).Value,Double)
- end if
- 'end if
- elseif e.Range.Offset(0,-1).Value IsNot Nothing And e.Range.Value IsNot NotHing then'已知单价输入金额触发数量
- if CType(e.Range.Offset(0,-1).Value,Double) <> 0 then
- YN = E.Range.Offset(0,-2).Address
- e.Range.Offset(0,-2).Value = CType(e.Range.Value,Double) / CType(e.Range.Offset(0,-1).Value,Double)
- end if
- end if
- end if
- Else
- YN = ""'还原触发判断
- End if
- End Sub'单元格编辑完成后执行事件
- Public Sub ButtunClick(sender As Object,e As SpreadsheetGear.Windows.Forms.ShapeActionEventArgs)
-
- End Sub'按钮/标签点击事件
- Public Sub FollowHyperlink(sender As Object)
-
- End Sub '暂不支持
- End Class
- '注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
- End Namespace
复制代码
|
|
hessen