2017-12-18 5 views
0

엑셀 열, 나는 Col C의 첫 번째 요소에 대해 매핑되는 Col B의 첫 번째 요소가 필요한 곳입니다.여러 열의 여러 값을 Excel의 행으로 분할합니다.

Column A  Column B    Column C 
Electrical Lighting,Thunder  Bad,Good 
Mechanical Nut, Bolt   Bad,Good 

결과는 내가 원하는 :

Column A  Column B  Column C 
Electrical Lighting  Bad 
Electrical Thunder  Good 
Mechanical Nut   Bad 
Mechanical Bolt   Good 

답변

0

모든 값이 Sheet1에있는 경우, 단순히 결과에 대한 시트 2를 가지고 있는지 확인은,이 코드는 당신이 무엇을 기대 할 것입니다 :

편집 :

:

그것은 지금 너무 단일 값에 대해 작동합니다

Sub foo() 
Dim LPosition As Integer 'declare variables 
Dim LPosition2 As Integer 
Dim LastRow As Long 
Dim NextEmptyRow As Long 
Dim strName As String 
Dim TempArray1() As String 
Dim TempArray2() As String 

LastRow = Sheet1.Cells(Sheet1.Rows.Count, "A").End(xlUp).Row 'find the last row on column A on Sheet1 
For i = 2 To LastRow 'loop from row 2 to the last row 
    strName = Sheet1.Cells(i, 1).Value 'get the value of Column A into a variable 
    LPosition = InStr(Sheet1.Cells(i, 2).Value, ",") ' check if Columm B has a comma in it 
    If LPosition > 0 Then 
     TempArray1 = Split(Sheet1.Cells(i, 2).Value, ",") 'if comma found put values into an array 
    Else 
     TempArray1(0) = Sheet1.Cells(i, 2).Value 
    End If 

    LPosition2 = InStr(Sheet1.Cells(i, 3).Value, ",") 'check for a comma on Column C 
    If LPosition2 > 0 Then 
     TempArray2 = Split(Sheet1.Cells(i, 3).Value, ",") 'place values into a separate Array 
    Else 
     TempArray2(0) = Sheet1.Cells(i, 3).Value 
    End If 
    For x = 0 To UBound(TempArray1) 'loop through the array 
     NextEmptyRow = Sheet2.Cells(Sheet2.Rows.Count, "A").End(xlUp).Row + 1 'check the next free row on Sheet2 
     Sheet2.Cells(NextEmptyRow, 1).Value = Trim(strName) 'place appropriate values 
     Sheet2.Cells(NextEmptyRow, 2).Value = Trim(TempArray1(x)) 
     Sheet2.Cells(NextEmptyRow, 3).Value = Trim(TempArray2(x)) 
    Next x 
    ReDim TempArray1(0) 
    ReDim TempArray2(0) 
Next i 
End Sub 
+0

이 @Xabier에서 도와 주셔서 감사합니다. Col B와 Col C가 각 행에 여러 값을 가지고있을 때이 기능이 잘 작동합니다. Col B와 Col C에 대한 단일 값만있는 행이 있으며 그대로 존재해야합니다. 거기에서 일할 수있는 방법이 있습니까? –

+0

@ M.Rafi 내 대답을 업데이트했습니다. 이제는 단일 값에 대해서도 작동해야합니다. – Xabier

관련 문제