
Excel 시트에서 날짜를 정렬하려고 하는데 버튼을 클릭하고 싶지만 버튼이 같은 시트에 없습니다. 같은 시트의 버튼에 매크로를 할당했는데 매우 잘 작동했습니다. 버튼을 다른 시트로 옮겼을 때 작동하지 않습니다! 이 코드에서 무엇을 편집해야 합니까?
스크린샷도 첨부했습니다. 이 VBA 코드 대신 수식이 있을 수도 있고, 버튼을 클릭하지 않고 VBA 코드를 실행하게 할 수도 있습니다. Office 365가 없어서 =FILTER 함수가 작동하지 않습니다.
Sub SortCC()
'
' SortCC Macro
'
'
Sheets("Jan_List").Select
Range("K2:R4").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Selection.End(xlUp).Select
Range("T2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Jan_List").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Jan_List").Sort.SortFields.Add2 Key:=Range( _
"T2:T1241"), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Jan_List").Sort
.SetRange Range("T2:AA1241")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
정말 감사합니다
답변1
이 코드를 일반 모듈에 배치하십시오 -~ 아니다또는 .Sheet
정렬하려는 각 시트에 버튼을 추가하고 매크로에 연결하세요.ThisWorkbook
Public Sub SortCC()
'You want the code to run on the sheet that has the button.
'To manually push the button the correct sheet must be active.
'So in this case we can use ActiveSheet. Note this is the only Sheet reference in the code.
With ActiveSheet
'Assumes Column R (18) has a value in last row of data.
Dim DataRange As Range
Set DataRange = .Range("K2", .Cells(.Rows.Count, 18).End(xlUp))
DataRange.Copy
.Range("T2").PasteSpecial Paste:=xlPasteValues
'We could figure out the new range the same way we did with the old one,
'but seeing as it's the same size as the original and one column across
'we can just use offset to figure it out.
Dim NewDataRange As Range
Set NewDataRange = DataRange.Offset(, DataRange.Columns.Count + 1)
With .Sort
.SortFields.Clear
'Sorting on first column of NewDataRange so have resized it to one column.
'For another column use, for example, .Offset(,1).Resize(,1) for second column.
.SortFields.Add2 Key:=NewDataRange.Resize(, 1), _
SortOn:=xlSortOnValues, _
Order:=xlDescending, _
DataOption:=xlSortNormal
.SetRange NewDataRange
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.Apply
End With
End With
End Sub