2016-09-27 2 views
-1

첫 번째 열의 정보가 특정 수준과 일치하는지 여부에 따라 행을 결합하려는 위치가 큰 데이터 세트가 있습니다. 내가 할 수있는 매크로가 있는지 궁금 해서요. 아래에는 비슷한 단순화 된 데이터 세트의 이미지가 포함되어 있습니다. 매크로를 새 워크 시트에 새 테이블을 만들거나 기존 데이터 아래에 행을 삽입 할 것이라고 가정하지만 확실하지 않습니다. 이 문제에 대한 도움이나 조언은 매우 유용합니다.Excel VBA 셀이 첫 번째 문자와 일치하는 경우 행 삽입

샘플 데이터 세트 :

Sample Dataset

출력 :

Output

+0

질문을 게시하기 전에 몇 가지 조사를하세요. 특히 좋은 질문을하는 방법과 비슷한 질문을 확인하십시오. 조건이 충족 될 때 행을 삽입하는 것에 대해 여기에 수백 개의 매크로가 있습니다. – teylyn

+0

음, 고맙습니다. –

답변

0

다음 (주석) 코드를 시도 할 수 있습니다 :

Option Explicit 

Sub main() 
    Dim cell As Range, cell2 As Range 

    With Worksheets("experiment").Range("A1").CurrentRegion '<--| reference data worksheet(change "experiment" to its actual name) cell "A1" contiguous range column "A" 
     .Sort key1:=Range("A1"), order1:=xlAscending, Header:=xlYes '<--| sort it by "experiment" column to have "smaller" names at the top 
     For Each cell In .Offset(1).Resize(.Rows.Count - 1, 1) '<--| loop through its 1st column cells skipping header row 
      If cell.Value <> "" Then '<--| if current cell isn't blank (also as a result of subsequent operations) 
       .AutoFilter Field:=1, Criteria1:="*" & cell.Value & "*" '<--| filter on referenced column to get cell "containing" current cell content 
       If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 2 Then '<--| if more than 2 rows has been foun: header row gets always filtered so to have at least 2 rows to consolidate we must filter at least 3 
        With .Offset(1).Resize(.Rows.Count - 1) '<--| reference filtered rows skipping header row 
         For Each cell2 In .Offset(, 1).Resize(, .Columns.Count - 1).SpecialCells(xlCellTypeVisible).Areas(1).Rows(1).Cells '<--| loop through 1st filtered row cells skipping 1st column ("experiment") 
          cell2.Value = WorksheetFunction.Subtotal(9, cell2.EntireColumn) '<--| update their content to the sum of filtered cells in corresponding column 
         Next cell2 
         With .Resize(, 1).SpecialCells(xlCellTypeVisible) '<--| reference filtered rows 1st column ("experiment") cells 
          .Value = .Cells(1, 1) '<--| have them share the same name 
         End With 
         .RemoveDuplicates Columns:=Array(1), Header:=xlNo '<--| remove duplicates, thus leaving the 1st filtered row with totals 
        End With 
       End If 
      End If 
     Next cell 
     .Parent.AutoFilterMode = False '<--| show all rows back 
    End With 
End Sub 
+0

"이 사이트는 무료 코드 작성 서비스가 아닙니다"또는 "문제가있는 코드 게시"에 어떤 변화가 있었습니까? 당신은 사람들이 일을하지 않도록 초대하고 있습니다. 왜냐하면 당신이 그들을 위해 일하고 있기 때문입니다. – teylyn

+0

효과가 있습니다! 고맙습니다. user3598756! 나는 어디서부터 시작해야하고 도움/방향이 필요한지조차 알지 못하기 때문에 코드를 게시하지 않았습니다. 나는 누군가가 완전한 코드를 작성하기를 기대하지 않는다. 대신 누군가에게 코드를 쓰거나 코드를 작성하는 데 도움을 줄 것을 기대했지만 실제로 도움을 주셔서 감사합니다! –

+0

당신은 환영합니다. 그런 다음 대답을 수락 된 것으로 표시 할 수 있습니다. – user3598756

0

는 첫 번째 열의 처음 몇 문자를 추출하는 열을 추가합니다. 그런 다음 행에 새 열이 있고 값 영역에 다른 열이있는 피벗 테이블을 만듭니다. VBA는 필요하지 않습니다.

+0

처음 몇 문자가 아니라 ""로 구분 된 행에 텍스트를 사용할 수 있습니다. –

관련 문제