周一至周五 : 08:30 - 17:30 客服专员电话/微信:17301649371 QQ:2627049059
微信咨询

扫码微信咨询

关注公众号

关注微信公众号

电话: 021 5161 9370
返回顶部
点击明细表任意一列标题对明细表排序的VBA方法
显示全部楼层 倒序浏览 发表日期 2019-12-23 17:13:24 2575次阅读 0次回复
hessen
2019-12-23 17:13:24
本帖最后由 hessen 于 2019-12-23 17:25 编辑

点击明细表任意一列标题对明细表排序的VBA方法

  1. Imports SpreadsheetGear
  2. Imports C1.Silverlight
  3. Imports C1.Silverlight.C1MessageBoxButton
  4. Imports C1.Silverlight.C1MessageBoxIcon
  5. Imports Newtonsoft.Json
  6. Imports Newtonsoft.Json.Linq
  7. Imports System.Windows.forms
  8. Namespace ExcelViewVBDotnet
  9.    Public Class StandardInterface
  10.        '预留位置1
  11.        '预留位置2
  12.        Dim EV As SpreadsheetGear.Windows.Co**ols.WorkbookView,AW As SpreadsheetGear.IWorkbook,Range As SpreadsheetGear.IRange
  13.            Dim ad As Integer = 0, ac As Integer = 0
  14.        Public Sub Workbook_Open(OldRoot As Object,NewRoot As Object,Excel As SpreadsheetGear.Windows.Co**ols.WorkbookView)
  15.            EV = Excel
  16.            Excel.GetLock()
  17.            AW = Excel.ActiveWorkbook
  18.            Range = AW.ActiveWorksheet.Range
  19.            Excel.ReleaseLock()
  20.        End Sub'打开时执行事件

  21.        Public Sub RangeSelection(sender As Object,e As SpreadsheetGear.Windows.Co**ols.RangeSelectionChangedEventArgs)
  22.             ActiveWorkbook.WorkbookSet.GetLock()
  23.             If e.RangeSelection.Intersect(Range("BTH")) IsNot Nothing Then
  24.                 Dim cc As Integer = e.RangeSelection.Column - 1
  25.                 Dim K1 As SpreadsheetGear.SortKey
  26.                 If ac = cc Then
  27.                     If ad = 0 Then
  28.                         K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal)
  29.                         ad = 1
  30.                     Else
  31.                         K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Descending, SpreadsheetGear.SortDataOption.Normal)
  32.                         ad = 0
  33.                     End If
  34.                 Else
  35.                     K1 = New SpreadsheetGear.SortKey(cc, SpreadsheetGear.SortOrder.Ascending, SpreadsheetGear.SortDataOption.Normal)
  36.                     ad = 1
  37.                     ac = cc
  38.                 End If
  39.                 Range("T_73").Sort(SpreadsheetGear.SortOrientation.Rows, True, K1)
  40.             End If
  41.             ActiveWorkbook.WorkbookSet.ReleaseLock()
  42.        End Sub'单元格选择后执行的事件

  43.        Public Sub RangeChanged(sender As Object,e As SpreadsheetGear.Windows.Co**ols.RangeChangedEventArgs)

  44.        End Sub'单元格编辑完成后执行事件

  45.        Public Sub ButtunClick(sender As Object,e As SpreadsheetGear.Windows.Co**ols.ShapeActionEventArgs)

  46.        End Sub'按钮/标签点击事件

  47.        Public Sub FollowHyperlink(sender As Object)

  48.        End Sub '暂不支持

  49.    End Class
  50. '  注:除事件字眼下可以自定义代码外的所有代码不允许改动,否则编译将有可能失败。
  51. End Namespace
复制代码
复制上面代码整体替换模板中的命令代码即可。若还有其他的事件代码,可以选择RangeSelection事件复制,并复制声明语句Dim ad As Integer = 0, ac As Integer = 0
模板中将明细表的标题区域选中添加别名为BTH,并将代码的中的明细表区域别名T_73替换为自己的明细表别名




有同学说比较复杂
其实不复杂
超级简单
这个是通用的代码
适用于任意模板的排序


您只要如下三步:
1、复制
2、将其中的T_73替换为自己的明细表
3、模板中将明细表标题添加个别名  BTH
电话/微信:18049989370 QQ:857188287
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则  允许回帖邮件提醒楼主

快表软件是国内较早研究表格类软件开发平台的团队之一,迄今已有十多年的行业经验.致力于为企事业单位提供实用可靠的数字化平台。
  • 微信公众号

  • 微信小商店

  • 微信客服

  • Powered by Discuz! X3.4 | Copyright © 2022-2024, XiRong Soft. | 快表软件
  • 沪ICP备13033196号 | 营业执照 |上海西戎软件科技有限公司|沪公网安备31011502002146号|沪ICP备13033196号 |