2017-12-08 7 views
0

이 매크로는 기록한 것을 기반으로하지만 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 
+0

, 나는 (시도하고'active'을 사용하지 않는, 그래서 대신 ActiveSheet''의 난'ThisWorkbook.Worksheets를 사용하는 것이 좋습니다 것입니다 것 "무엇이든간에 시트의 이름은 ")'이것은 길어질 수 있으며 'With'를 사용할 수 있지만이 방법으로는 Excel이 잘못된 시트를 참조하고 오류를 던지려하지 않을 가능성이 큽니다. – SilentRevolution

+0

안녕하세요 @ SilentRevolution, 조언 주셔서 감사합니다. 그러나 이것은 마스터 서브 루틴에 의해 호출되는 일련의 전체 서브 루틴의 일부입니다. 그들은 내가 아직 엑셀을 열지 않았을 때 열릴 수있는 다양한 워크 북에서 사용할 계획이므로 개인 personal.xlb에 있습니다. Salesforce 웹 데이터베이스에서 내보내기/다운로드합니다. 그래서 그것은 다양한 시트에서 실행될 것이고, 그것은 다양한 이름을 가질 것입니다. 이것이 바로 이름이 맞는 통합 문서가 아닌 ActiveSheet로 설정 한 이유입니다. –

답변

0

Range("A1").End(xlDown).End(xlToRight)A1:W162는 유사하지 단일 셀 범위를 말한다. A1:W162은 시트의 모든 데이터가있는 범위입니다. 이 줄은 W162만을 나타냅니다. Range.End 속성은 here으로 설명되어 있으며 시작 지점을 유지하지 못한다는 것을 보여줍니다.

Range("A1:"& Range("A1").End(xlDown).End(xlToRight).Address) 대신 A1:W162과 비슷한 범위를 만들어야합니다. 첫 번째 부분은 함께 그들이 내가 아래의 코드를 제안 염두에 귀하의 질문에 만든 주석으로 "A1:$W$162"

을 형성 문자열을 "A1:"Range("A1).End(xlDown).end(xlToRight).Address 반환 문자열 "$W$162"입니다.

내 대답이 포함되지 않은 한 이유는 일반적인 조언로서
Dim wsData as Worksheet 'Add worksheet variable 

Set wsData = ThisworkBook.Worksheets("Name of your data sheet") 

With wsData.Sort 'Instead of using ActiveSheet 
    .SetRange wsData.Range(wsData.Cells(1, 1), wsData.Cells(1, 1).End(xlDown).End(xlToRight)) 
+0

다시 @SilentRevolution에게 감사드립니다. 제안 된 솔루션이 어떤 이유로 작동하지 않는 것 같아서 (설정할 수 없음) 걱정됩니다. 그러나 귀하의 충고에 따라 내가 잘못하고있는 것을 깨닫게되었으며, 다음 코드 섹션을 사용하여 바로 수정할 수있게되었습니다. 'With ActiveSheet.Sort .SetRange Range ("A1"). CurrentRegion .Header = xlYes .MatchCase = 거짓 .Orientation = xlTopToBottom .SortMethod = xlPinYin .Apply 끝 With' –

+0

모든 @JamesH 없음 문제는, 그것이 실제 문서없이 작동하지 않습니다 이유없이 이야기하는 것은 매우 어렵다. 내 제안이 도움이되었지만 기쁩니다. – SilentRevolution

관련 문제