매크로 조언 - 특정 셀에 "NO"라고 표시된 경우 행을 새 워크시트에 붙여넣기

매크로 조언 - 특정 셀에 "NO"라고 표시된 경우 행을 새 워크시트에 붙여넣기

VBA와 매크로를 사용하여 조언을 듣고 싶습니다.

워크시트를 연결하는 방법을 원합니다(워크시트 1~6)를 마스터 시트(워크시트 7).

행에 다음이 포함된 경우"아니요"I(전체워크시트 1~6), 코드에서 해당 행을 복사하여 붙여넣을 수 있나요?워크시트 7?

그런 다음 행(in워크시트 1~6)로 변경되었습니다."예" 다른 코드가 해당 행을 삭제할 수 있습니까?워크시트 7?

어떤 맥락에서,워크시트 1~6직업 목록과'예'&'아니요'고객이 지불한 경우입니다. 만약에'아니요'채무자 목록에 추가됩니다.워크시트 7. 만약에'예'채무자 명단에서 삭제되어야 합니다.

답변1

이 코드는 다음을 수행하는 데 도움이 됩니다.

Public Sub debtors()
    Dim wkb As Workbook
    Dim wks As Worksheet
    Dim wksdest As Worksheet
    Set wkb = ThisWorkbook
    Set wksdest = wkb.Sheets("Sheet7")
    wksdest.Rows.Clear 'Clear the contents of Sheet7
    destRow = 1 'First row on Sheet7
    For i = 1 To 6 'Loop through Sheets 1 to 6
        newIndex = Right(Str(i), 1)
        thisSheet = "Sheet" + newIndex
        Set wks = wkb.Sheets(thisSheet)
        wks.Activate
        'Selects column I
        Columns("I:I").Select
        'Find a coincidence with the string "NO"
        Set cell = Selection.Find(What:="NO", After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
        'If there is a coincidence (is Not Nothing)
        If Not cell Is Nothing Then
            firstRow = cell.Row
            newRow = cell.Row
            'Copy the row and paste on Sheet7
            wks.Rows(newRow).Copy
            wksdest.Rows(destRow).PasteSpecial xlPasteValues
            destRow = destRow + 1
            foundValue = True
            'Find next coincidences in the same sheet
            While foundValue
                Set cell = Selection.FindNext(cell)
                If Not cell Is Nothing Then
                    newRow = cell.Row
                    If newRow <> firstRow Then
                        wks.Rows(newRow).Copy
                        wksdest.Rows(destRow).PasteSpecial xlPasteValues
                        destRow = destRow + 1
                    Else
                        foundValue = False
                    End If
                Else
                    foundValue = False
                End If
            Wend
        End If
    Next i
    wksdest.Activate
End Sub

ALT+를 사용하여 VBA/매크로를 엽니다 F11.이워크북새 모듈을 삽입하고 오른쪽에 코드를 붙여넣습니다.

녹색 삼각형을 클릭하여 실행하세요.

코드가 어떻게 작동하는지 이해할 수 있도록 코드에 주석을 달았습니다.

첫 번째 줄을 클릭한 다음 를 눌러 각 단계를 진행하여 단계별로 실행할 수도 있습니다 F8.

관련 정보