2014-07-22 3 views
0

주제가 밝혀지면서 가변 조건에 따라 여러 셀과 행의 데이터를 복사하려고합니다. 내 입력 내용은 다음과 같습니다.가변 조건에 따라 여러 행의 여러 값을 하나의 셀로 복사하십시오.

product size type  length shoulder 
82HTHSG S  shirt 69  38.5  
82HTHSG M  shirt 74  39.5  
82HTHSG L  shirt 74  41.5  
82HTHSG XL  shirt 77  43  
84HTFSG S  tShirt 66  41   
84HTFSG M  tShirt 67.5 41   
84HTFSG L  tShirt 70  44.5  
84HTFSG XL  tShirt 73.5 46   

제품 내의 모든 크기의 데이터 (변수)를 열의 문자열과 결합하고 싶습니다. 그것은이 (예)과 같아야합니다

Sub CombineText() 
Dim i As Integer 
i = 1 

Do While Cells(i, 1).Value <> "" 

If Cells(i, 5) = "shirt" Then 

Cells(i, 22).Value = "Shoulder x" & Chr(10) & "Length" & Chr(10) & _ 
" - " & Cells(i, 3) & " (" & Cells(i, 9) & "cm x " & Cells(i, 16) & "cm)" 
i = i + 1 

ElseIf Cells(i, 5) = "tShirt" Then 

Cells(i, 22).Value = "Shoulder x" & Chr(10) & "Length" & Chr(10) & _ 
" - " & Cells(i, 3) & " (" & Cells(i, 9) & "cm x " & Cells(i, 16) & "cm)" 
i = i + 1 

Else 

Cells(i, 22).Value = 2 
i = i + 1 

End If 

Loop 

End Sub 

코드는 다음과 같은 결과를 제공합니다 (예) :

82HTHSG S  shirt 69  38.5  Shoulder x 
              Length 
              - S (69cm x 38.5cm) 
82HTHSG M  shirt 74  39.5  Shoulder x 
              Length 
              - M (74cm x 39.5cm) 

모든 나는 다음과 같은 코드를 작성했습니다 지금까지

product size type  length shoulder result 
82HTHSG S  shirt 69  38.5  Shoulder x 
              Length 
              - S (69cm x 38.5cm) 
              - M (74cm x 39.5cm) 
              - L (Xcm x Xcm) 
              - XL (Xcm x Xcm) 
82HTHSG M  shirt 74  39.5  Shoulder x 
              Length 
              - S (69cm x 38.5cm) 
              - M (74cm x 39.5cm) 
              - L (Xcm x Xcm) 
              - XL (Xcm x Xcm) 
(etc)          

을 동일한 제품 내의 나머지 크기에서 데이터를 복사하는 방법에 대한 생각을하고 인접한 셀에 모두 붙여 넣을 수 있습니까?

모든 도움을 주시면 대단히 감사하겠습니다.

답변

0

매크로에서 한 제품 코드의 모든 행을 거쳐 모든 데이터를 수집하는 사이클이 누락되었습니다. 다음은 이전 코드와 다른 제품 코드를 찾았는지 확인하는 코드입니다. 그렇다면 동일한 제품 코드를 사용하여 필요한 모든 데이터를 얻을 수 있습니다.

Sub CombineText() 
Dim i, j As Integer 
Dim result As String 
i = 2 

    Do While Cells(i, 1).Value <> "" 

     'check if you are on first line mentioning new product code 
     If Cells(i, 1).Value <> Cells(i - 1, 1).Value Then 

      If Cells(i, 5) = "shirt" Then 
       result = "Shoulder x" & Chr(10) & "Length" & Chr(10) 
       j = i 

       'loop through following lines until you reach different product code 
       While Cells(j, 1).Value = Cells(i, 1).Value 
        result = result & " - " & Cells(j, 3) & " (" & Cells(j, 9) & "cm x " & Cells(j, 16) & "cm)" & Chr(10) 
        j = j + 1 
       Wend 

      ElseIf Cells(i, 5) = "tShirt" Then 
       result = "Shoulder x" & Chr(10) & "Length" & Chr(10) 
       j = i 

       'loop through following lines until you reach different product code 
       While Cells(j, 1).Value = Cells(i, 1).Value 
        result = result & " - " & Cells(j, 3) & " (" & Cells(j, 9) & "cm x " & Cells(j, 16) & "cm)" & Chr(10) 
        j = j + 1 
       Wend 

      Else 
       result = 2 

      End If 

      'fill in the first line for given product code 
      Cells(i, 22).Value = result 

     'for second and additional lines of the same product there is already correct result in previous line so just copy it 
     Else 
      Cells(i, 22).Value = Cells(i - 1, 22).Value 

     End If 

     i = i + 1 

    Loop 

End Sub 

btw 시작 0을 1에서 2로 옮겼습니다. 그렇지 않으면 라인 0의 값을 확인하고 오류가 발생하기 때문입니다.

"셔츠"와 "tshirt"를 기준으로 분류 한 이유는 확실하지 않습니다. 코드에서 차이가 나타나지 않기 때문에 나는 그것을 놓쳤거나 뭔가 다른 것을 필요로 할 때를 대비해 보관했습니다. 도움이 되었으면 좋겠다.

Pavel

+0

Worked - 많이 고마워! – Magnus

관련 문제