VBA CODE

VBA 셀병합 Merge 쇼핑몰 동일고객 셀 병합

VBA 2023. 3. 31. 11:49

Sub mergeCells()

    Dim i As Integer
    Dim rngStart As Range
    Dim rngEnd As Range
    
    '선택된 영역 중 첫째줄 지정
    Set rngStart = Selection.Cells(2, 2)
    
    '선택된 영역 반복
    For i = 2 To Selection.Rows.Count
    
        '두줄을 비교하여 같지 않을 경우
        If Selection.Cells(i, 2) <> Selection.Cells(i + 1, 2) Then
            
            '같지 않을 경우 끝으로 지정
            Set rngEnd = Selection.Cells(i, 2)
            
            '알람 창 Off
            Application.DisplayAlerts = False
            
            '셀 병합 실시
            Range(rngStart, rngEnd).Merge
            '중앙 정렬
            Range(rngStart, rngEnd).HorizontalAlignment = xlCenter
            Range(rngStart, rngEnd).VerticalAlignment = xlCenter
            
            '알람 창 On
            Application.DisplayAlerts = True
        
            '시작 위치를 다음 줄로 설정
            Set rngStart = Selection.Cells(i + 1, 2)
        
        End If
    
    Next
    

End Sub