제가 알아내려고 노력했던 문제에 대해 도움을 주시면 감사하겠습니다. 여러 셀이 포함된 보고서가 있는데 셀 중 하나에 불만 사항 통지를 받은 날짜가 포함되어 있습니다. 제가 달성하려고 하는 것은 월 초에 이전 달의 모든 불만사항을 복사하여 붙여넣는 것입니다. 내가 한 일은 이전 달 날짜가 포함된 모든 셀을 연한 빨간색으로 강조 표시하는 매크로를 기록하는 것입니다. 하지만 내 문제는 셀이 강조 표시된 전체 행을 선택한 다음 다른 탭으로 이동하는 다양한 변형 코드를 테스트했다는 것입니다. 아래는 제가 시도한 코드인데 조건부 서식에 추가된 색상으로 C 열의 셀을 찾도록 하고 싶습니다.
도와 주셔서 감사합니다!
Sub Test()
Dim wks As Worksheet
Dim wNew As Worksheet
Dim lRow As Long
Dim x As Long
Columns("C:C").Select
Selection.FormatConditions.Add Type:=xlTimePeriod, DateOperator:= _
xlLastMonth
Selection.FormatConditions(Selection.FormatConditions.Count).SetFirstPriority
With Selection.FormatConditions(1).Font
.Color = -16383844
.TintAndShade = 0
End With
With Selection.FormatConditions(1).Interior
.PatternColorIndex = xlAutomatic
.Color = 13551615
.TintAndShade = 0
End With
Selection.FormatConditions(1).StopIfTrue = False
Set wks = ActiveSheet
lRow = wks.Cells.SpecialCells(xlCellTypeLastCell).Row
Set wNew = Worksheets.Add
For x = 1 To lRow
If wks.Cells(x, 1).Interior.Color = vbRed Then
wks.Cells(x, 1).EntireRow.Copy wNew.Cells(x, 1)
End If
Next
End Sub
답변1
벨로우즈는 2가지 버전입니다.
- 첫 번째는 날짜에 대한 자동 필터만 사용하여 모든 불만사항을 새 시트에 복사하는 것입니다.
- 두 번째는 먼저 C 열에 조건부 서식을 적용한 다음 색상에 자동 필터를 적용합니다.
Option Explicit
Public Sub GetPreviousMonthsComplaintsFilterOnly()
Const DATE_COL = 3 'C
Dim wsSrc As Worksheet, wsDst As Worksheet
Dim wsName As String, ur As Range
Application.ScreenUpdating = False 'set complaints ws name like: "Complaints - 2017-Sep"
wsName = CleanWsName("Complaints - " & Format(DateAdd("m", -1, Now), "yyyy-mmm"))
Set wsSrc = ThisWorkbook.Worksheets("Sheet1") 'report with all dates
Set wsDst = GetComplaintsWs(wsName) 'complaints Worksheet
wsDst.Name = wsName 'rename the new complaints report
With wsSrc.UsedRange
If wsSrc.AutoFilterMode Then .AutoFilter 'clear previous filters
.AutoFilter Field:=DATE_COL, Criteria1:=xlFilterLastMonth, Operator:=xlFilterDynamic
'copy only if there are visible rows
If .Columns(DATE_COL).SpecialCells(xlCellTypeVisible).CountLarge > 1 Then
Set ur = wsSrc.UsedRange
If Not IsDate(.Cells(1, DATE_COL)) Then
Set ur = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count)
End If
ur.Copy wsDst.Cells(wsDst.Cells(wsDst.Rows.Count, 1).End(xlUp).Row + 1, 1)
wsDst.UsedRange.Columns.AutoFit
End If
.AutoFilter
'wsSrc.Activate
End With
Application.ScreenUpdating = True
End Sub
Public Sub GetPreviousMonthsComplaintsConditionalFormat()
Const DATE_COL = 3 'C
Dim wsSrc As Worksheet, wsDst As Worksheet, wsName As String, ur As Range
Dim lRed As Long, dRed As Long
lRed = RGB(255, 199, 206) 'or 13551615 (light red)
dRed = RGB(156, 0, 6) 'or -16383844 (dark red)
Application.ScreenUpdating = False 'set complaints ws name like: "Complaints - 2017-Sep"
wsName = CleanWsName("Complaints - " & Format(DateAdd("m", -1, Now), "yyyy-mmm"))
Set wsSrc = ThisWorkbook.Worksheets("Sheet1") 'report with all dates
Set wsDst = GetComplaintsWs(wsName): wsDst.Name = wsName
With wsSrc.UsedRange
With .Columns(DATE_COL) 'apply conditional formatting to column C
.FormatConditions.Delete
.FormatConditions.Add Type:=xlTimePeriod, DateOperator:=xlLastMonth
.FormatConditions(.FormatConditions.Count).SetFirstPriority
.FormatConditions(1).Font.Color = dRed
.FormatConditions(1).Interior.Color = lRed
.FormatConditions(1).StopIfTrue = False
End With
If wsSrc.AutoFilterMode Then .AutoFilter
.AutoFilter Field:=DATE_COL, Criteria1:=lRed, Operator:=xlFilterCellColor
'or .AutoFilter Field:=DATE_COL, Criteria1:=dRed, Operator:=xlFilterFontColor
If .Columns(DATE_COL).SpecialCells(xlCellTypeVisible).CountLarge > 1 Then
Set ur = wsSrc.UsedRange
If Not IsDate(.Cells(1, DATE_COL)) Then 'determine if first row are headers
Set ur = .Offset(1).Resize(.Rows.Count - 1, .Columns.Count)
End If
ur.Copy wsDst.Cells(wsDst.Cells(wsDst.Rows.Count, 1).End(xlUp).Row + 1, 1)
wsDst.UsedRange.Columns.AutoFit
wsDst.UsedRange.Columns(DATE_COL).FormatConditions.Delete
End If: .Columns(DATE_COL).FormatConditions.Delete: .AutoFilter
End With
Application.ScreenUpdating = True
End Sub
Public Function GetComplaintsWs(ByVal wsName As String) As Worksheet
Dim ws As Worksheet
With ThisWorkbook
For Each ws In ThisWorkbook.Worksheets
If ws.Name = wsName Then Set GetComplaintsWs = ws
Next
If GetComplaintsWs Is Nothing Then
Set GetComplaintsWs = .Worksheets.Add(After:=.Worksheets(.Worksheets.Count))
End If
End With
End Function
Public Function CleanWsName(ByVal wsName As String) As String
Const X = vbNullString
wsName = Trim$(wsName) 'Trim, remove [ ] / \ : ? * ., and resize to len <= 31
wsName = Replace(Replace(wsName, "[", X), "]", X)
wsName = Replace(Replace(Replace(wsName, "/", X), "\", X), ":", X)
wsName = Replace(Replace(Replace(wsName, "?", X), "*", X), ".", X)
CleanWsName = Left$(wsName, 31)
End Function