如何从排序中排除第一行(VBA)

时间:2018-11-29 22:24:04

标签: excel vba excel-vba

我需要使用此脚本一次对多个工作表进行排序

Sub SortAllSheets()
   Dim WS      As Worksheet
   ActiveSheet.Range("a2:f2").Select
   Selection.Copy
   On Error Resume Next
   Application.ScreenUpdating = False
   For Each WS In Worksheets
      WS.Columns("A:F").Sort Key1:=WS.Columns("D"), Order1:=xlAscending
   Next WS
   ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
   Application.ScreenUpdating = True
End Sub

是否有一种方法可以定义将每个工作表中的第一行(标题)从排序中排除?我试图修改数据输入或添加一些额外的功能,但是没有成功。感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

一个例子:

Dim strDataRange As Range
Dim keyRange As Range
Set strDataRange = Range("Your Data Range")
Set keyRange = Range("Your Sort by Column")
strDataRange.Sort Key1:=keyRange, Header:=xlYes

因此使用您的代码:

Sub SortAllSheets()
   Dim WS      As Worksheet
   ActiveSheet.Range("a2:f2").Select
   Selection.Copy
   On Error Resume Next
   Application.ScreenUpdating = False
   For Each WS In Worksheets
      WS.Columns("A:F").Sort Key1:=WS.Columns("D"), Order1:=xlAscending, Header:=xlYes
   Next WS
   ActiveSheet.Range("A1").PasteSpecial Paste:=xlPasteAll
   Application.ScreenUpdating = True
End Sub