이 매크로는 기록한 것을 기반으로하지만 4 가지 정렬 기준 중 3 가지가없는 경우에 대비하여 조정되었습니다. 범위 기준이 구체적 일 때 매크로가 작동하는 이유를 알 수 없지만 단일 셀을 참조하고 외삽 할 때가 아닙니다. 이 라인으로Excel 개체가 Range 개체로 인해 작동하지 않음
는 정렬은 정렬하지 않습니다이 라인으로 .SetRange Range("A1:W162")
작동합니다. .SetRange Range("A1").End(xlDown).End(xlToRight)
나는 통해 강화했고 올바른 범위
나는 미래의 수출 규모 다를 수 있기 때문에 특정 세포 라인을 사용하지 않으을 선택하는 것 확인할 수 있습니다.
이것은 전체 서브 루틴입니다 (관련 비트가 바닥에 가깝습니다).
감사합니다.
Sub SortByScoreAndCost()
Dim Score As Range
Dim Cost As Range
Dim YN As Range
Dim OriginalScore As Range
Set Score = Cells.Find("Score")
Set Cost = Cells.Find("Cost")
Set YN = Cells.Find("Y/N")
Set OriginalScore = Cells.Find("Original Score")
Range("A1").CurrentRegion.Select
ActiveSheet.Sort.SortFields.Clear
ActiveSheet.Sort.SortFields.Add Key:=Range(_
Score.Offset(1, 0), Score.End(xlDown)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
If Cells.Find("Cost") Is Nothing _
Then
Else: ActiveSheet.Sort.SortFields.Add Key:=Range _
(Cost.Offset(1, 0), Cost.End(xlDown)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
If Cells.Find("Y/N") Is Nothing _
Then
Else: ActiveSheet.Sort.SortFields.Add Key:=Range _
(YN.Offset(1, 0), YN.End(xlDown)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
If Cells.Find("Original Score") Is Nothing _
Then
Else: ActiveSheet.Sort.SortFields.Add Key:=Range _
(OriginalScore.Offset(1, 0), Original.End(xlDown)), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption _
:=xlSortNormal
End If
With ActiveSheet.Sort
.SetRange Range("A1:W162") 'works with this line
.SetRange Range("A1").End(xlDown).End(xlToRight) 'doesn't work if replaced with this line
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
, 나는 (시도하고'active'을 사용하지 않는, 그래서 대신 ActiveSheet''의 난'ThisWorkbook.Worksheets를 사용하는 것이 좋습니다 것입니다 것 "무엇이든간에 시트의 이름은 ")'이것은 길어질 수 있으며 'With'를 사용할 수 있지만이 방법으로는 Excel이 잘못된 시트를 참조하고 오류를 던지려하지 않을 가능성이 큽니다. – SilentRevolution
안녕하세요 @ SilentRevolution, 조언 주셔서 감사합니다. 그러나 이것은 마스터 서브 루틴에 의해 호출되는 일련의 전체 서브 루틴의 일부입니다. 그들은 내가 아직 엑셀을 열지 않았을 때 열릴 수있는 다양한 워크 북에서 사용할 계획이므로 개인 personal.xlb에 있습니다. Salesforce 웹 데이터베이스에서 내보내기/다운로드합니다. 그래서 그것은 다양한 시트에서 실행될 것이고, 그것은 다양한 이름을 가질 것입니다. 이것이 바로 이름이 맞는 통합 문서가 아닌 ActiveSheet로 설정 한 이유입니다. –