VBA Macro/VBA Excel
엑셀에서 특정 문자로 나열된 셀 내용을 새로운 행을 추가하여 구분하기
루아흐뉴마
2018. 3. 9. 01:10
반응형
매크로 설명
- 지난 번 포스팅을 약간 수정하여 보다 보편화된 상황에서 사용할 수 있도록 만든 매크로이다.
- (http://ruahneuma.tistory.com/57 "강제 줄바꿈 셀 분리하기")
- 특정 구분자(예, 콤마, 띄어쓰기, 콜론 등)로 나열된 셀 내용을 행을 추가하여 별도로 분리하는 매크로이다.
- targetChr 변수에 원하는 구분자를 입력하여 쉽게 분리할 수 있다.
VBA Code
Sub forced_enter_separation()
Dim k As Integer, cnt As Integer
Dim varSize As Integer
Dim lastRow As Integer
Dim myColumn As String
Dim targetChr As String
With ActiveSheet.UsedRange
lastRow = .Row + .Rows.Count '마지막 셀 번호 + 1
End With
myColumn = "F" '대상 열, 필요시 변경
targetChr = ", " '대상 구분자, 필요시 변경
For k = lastRow To 2 Step -1 '셀 삽입/삭제 코드는 역방향으로 탐색
varSize = UBound(Split(Cells(k - 1, myColumn), targetChr)) '구분자가 있는 셀의 내용을 배열로 반환
If InStr(1, Cells(k - 1, myColumn), targetChr) > 0 Then '구분자가 있는 경우
For cnt = 0 To varSize - 1 '배열 크기의 -1만큼 순환
Cells(k, myColumn).EntireRow.Insert shift:=xlShiftDown '행 삽입
Cells(k, myColumn) = Split(Cells(k - 1, myColumn), targetChr)(varSize - cnt) '배열 내용 할당
Next
Cells(k - 1, myColumn) = Split(Cells(k - 1, myColumn), targetChr)(varSize - cnt) '최초의 셀에 첫번째 내용 할당
End If
Next k
End Sub
매크로 실행 동영상(사용방법 포함)

반응형