[英]Sharing a macro across multiple worksheets
I've created a simple macro which will work in different workbooks but only if the WORKSHEET has the same name.我创建了一个简单的宏,它可以在不同的工作簿中使用,但前提是 WORKSHEET 具有相同的名称。 In the code below, the worksheet is called "TEMPLATE."在下面的代码中,工作表称为“模板”。 How can I make this work regardless of the name of the worksheet?无论工作表的名称如何,我如何才能使这项工作? This is for a number of divisions within a tournament;这适用于锦标赛中的多个部门; each worksheet represents a scoresheet for a division with several competitors.每个工作表代表一个有几个竞争对手的部门的分数表。 At the end the division, the macro is designed to sort the competitors into 1st place, 2nd place, etc. I'm sure there's an easy solution, but I am not a coder!在分组结束时,宏旨在将竞争对手分类为第一名,第二名等。我确信有一个简单的解决方案,但我不是编码员!
Sub SORTSCORE2()
'
' SORTSCORE2 Macro
'
' Keyboard Shortcut: Ctrl+Shift+C
'
ActiveWorkbook.Worksheets("TEMPLATE").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("TEMPLATE").Sort.SortFields.Add2 Key:=Range( _
"B8:B26"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("TEMPLATE").Sort
.SetRange Range("A7:M26")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
You can use ActiveWorkbook.ActiveSheet
if you want to run one sheet at a time如果您想一次运行一张工作表,可以使用ActiveWorkbook.ActiveSheet
Alternatively you can use a loop of all sheets in the workbook或者,您可以使用工作簿中所有工作表的循环
Sub SORTSCORE2()
' Keyboard Shortcut: Ctrl+Shift+C
Dim Sheet as WorkSheet
For Each Sheet in ActiveWorkbook
Sheet.Sort.SortFields.Clear
Sheet.Sort.SortFields.Add2 Key:=Range( _
"B8:B26"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With Sheet.Sort
.SetRange Range("A7:M26")
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.