本帖最后由 hessen 于 2019-12-23 17:25 编辑
点击明细表任意一列标题对明细表排序的VBA方法
- Imports SpreadsheetGear
- Imports C1.Silverlight
- Imports C1.Silverlight.C1MessageBoxButton
- Imports C1.Silverlight.C1MessageBoxIcon
- Imports Newtonsoft.Json
- Imports Newtonsoft.Json.Linq
- Imports System.Windows.forms
- Namespace ExcelViewVBDotnet
- Public Class StandardInterface
- '预留位置1
- '预留位置2
- Dim EV As SpreadsheetGear.Windows.Co**ols.WorkbookView,AW As SpreadsheetGear.IWorkbook,Range As SpreadsheetGear.IRange
- Dim ad As Integer = 0, ac As Integer = 0
- Public Sub Workbook_Open(OldRoot As Object,NewRoot As Object,Excel As SpreadsheetGear.Windows.Co**ols.WorkbookView)
- EV = Excel
- Excel.GetLock()
- AW = Excel.ActiveWorkbook
- Range = AW.ActiveWorksheet.Range
- Excel.ReleaseLock()
- End Sub'打开时执行事件
- Public Sub RangeSelection(sender As Object,e As SpreadsheetGear.Windows.Co**ols.RangeSelectionChangedEventArgs)
- ActiveWorkbook.WorkbookSet.GetLock()
- If e.RangeSelection.Intersect(Range("BTH")) IsNot Nothing Then
- Dim cc As Integer = e.RangeSelection.Column - 1
- Dim K1 As SpreadsheetGear.SortKey
- If ac = cc Then
- If ad = 0 Then
- K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal)
- ad = 1
- Else
- K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Descending, SpreadsheetGear.SortDataOption.Normal)
- ad = 0
- End If
- Else
- K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal)
- ad = 1
- ac = cc
- End If
- Range("T_73").Sort(SpreadsheetGear.SortOrientation.Rows, True, K1)
- End If
- ActiveWorkbook.WorkbookSet.ReleaseLock()
- End Sub'单元格选择后执行的事件
- Public Sub RangeChanged(sender As Object,e As SpreadsheetGear.Windows.Co**ols.RangeChangedEventArgs)
- End Sub'单元格编辑完成后执行事件
- Public Sub ButtunClick(sender As Object,e As SpreadsheetGear.Windows.Co**ols.ShapeActionEventArgs)
- End Sub'按钮/标签点击事件
- Public Sub FollowHyperlink(sender As Object)
- End Sub '暂不支持
- End Class
- ' 注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
- End Namespace
复制代码 复制上面代码整体替换模板中的命令代码即可。若还有其他的事件代码,可以选择RangeSelection事件复制,并复制声明语句Dim ad As Integer = 0, ac As Integer = 0
模板中将明细表的标题区域选中添加别名为BTH,并将代码的中的明细表区域别名T_73替换为自己的明细表别名
有同学说比较复杂
其实不复杂
超级简单
这个是通用的代码
适用于任意模板的排序
您只要如下三步:
1、复制
2、将其中的T_73替换为自己的明细表
3、模板中将明细表标题添加个别名 BTH
|
|
hessen