我的excel电子表格有点问题,我希望有人可以提供帮助。我的名字从Cell = A7开始,转到A177。所有其他信息都在列(B:H)中。理想情况下,我想在执行数据刷新后运行代码。我正在使用这张表,所以我可以查找另一张表的信息,因此它需要按字母顺序排列A-Z。信息来自网络查询。
答案 0 :(得分:0)
修改强>
将以下代码添加到标准模块。请参阅代码中的编辑,以确保它按预期正确排序所有列。
#{# app/Resources/views/default/social.html.twig #}
<ul>
{% for social in socials %}
<li>{{ social.name }}</li>
{% endfor %}
</ul>
然后创建一个类模块并插入以下代码:
Option Explicit
dim DataTable as New Class1
Sub Auto_Open()
'This will run automatically when the workbook is opened, so macros will need to be enabled.
'Select any cell in the data table.
Activesheet.Range("A7").Select
Set DataTable.qt = ThisWorkbook.ActiveSheet.QueryTables(1)
Sub
Sub AutoSort()
Dim rng As Range
Dim Nrow As Integer
Dim Ncol As Integer
Dim WS As Worksheet
'Assume that the active sheet contains the data that you want to sort
'Since it sounds like you'll be calling this from another macro, this
'is probably not a good assumption.
Set WS = ActiveWorkbook.ActiveSheet
'Get the row number of your last entry in column A, then the right most
'column of your data. Assume there is no other data on this worksheet.
Nrow = WS.Cells(Rows.Count, "A").End(xlUp).Row '177 in your case
'Replace the following line
'Ncol = WS.Cells(1, Columns.Count).End(xlToLeft).Column '8 in your case
'with this line
Ncol = WS.Cells(7, Columns.Count).End(xlToLeft).Column '8 in your case
'set all of your data in a range.
Set rng = WS.Range(Cells(7, 1), Cells(Nrow, Ncol))
'the actual sorting
rng.Sort key1:=rng, order1:=xlAscending, Header:=xlYes
End Sub
请注意,必须先运行Option Explicit
Public WithEvents qt As QueryTable
Private Sub qt_AfterRefresh(ByVal Success As Boolean)
If Success = True Then
Call Module1.AutoSort
MsgBox "Data was updated and sorted."
End If
End Sub
才能使Auto_Open()
正常运行。这应该在您打开工作簿时发生。
守则严重依赖他人的工作,即:
How to call macro after Refresh or Refresh All button pressed?由@Rory回答