본문 바로가기
page 1 .컴퓨터/문서작성

[Excel] 매크로를 활용해서 문자열의 특정 값을 찾아서 밑의 행으로 나눠주기

by 나의나된것은 2013. 3. 29.
반응형

기분 열에 해당하는 문자가 있으면 그 문자로 나눠주는 프로그램이다.

예를들어서

 I행에 |문자를 찾아서 밑으로 나눠주고 싶을때

 이 매크로를 실행하면 아래처럼 변한다.

단지 계속 실행해야 하고, 마지막에 에러가 나긴 하지만..

그럭저럭 쓸만한거 같다.

Sub 매크로1()
'
' 매크로1 매크로
'
'
    Dim i As Integer
    Dim nFindPos As Integer
    Dim nLen As Integer
    Dim nFor As Integer
   
    Dim strSourceLabel As String
    Dim strFindLabel As String
    Dim strNextLabel As String
   
    strFindLabel = "|"
    'I 열 중에서 strFindLabel 값을 찾음.
    Range("I:I").Find(What:=strFindLabel, After:=ActiveCell, LookAt:= _
        xlPart).Activate
         
    nFor = Len(ActiveCell) - Len(Application.Substitute(ActiveCell, strFindLabel, ""))
   
    strNextLabel = ActiveCell
   
    If nFor > 0 Then
        For i = 1 To nFor Step 1
            '마지막 위치 기억
            nLen = Len(strNextLabel)
            'strFindLabel 의 위치를 찾음.
            nFindPos = InStr(strNextLabel, strFindLabel)
            '찾기 전의 값을 가져와서
            strSourceLabel = Left(strNextLabel, nFindPos - 1)
            ' 현재 위치에 저장
            ActiveCell = strSourceLabel
            ' 찾은 값 다음 값을 다시 기준으로 삼음.
            strNextLabel = Mid(strNextLabel, nFindPos + 1, nLen)
            '한줄을 삽입하고
            Cells(ActiveCell.Row + 1, ActiveCell.Column).Select
            Selection.EntireRow.Insert
            '찾은 값을 다음 행에 저장
            Cells(ActiveCell.Row, ActiveCell.Column) = strNextLabel
        Next i
    End If
   
End Sub

반응형