简体   繁体   中英

Excel VBA Insert row if cells match first characters

So I have a large data set where I would like to combine rows depending if the information in the first column matches to a certain degree. I was wondering if there is a macro that could do this. Below I have included images of a similar simplified data set. I would assume the macro would create the new table in a new worksheet or insert a row below the existing data but I am not sure. Any help or tips on this problem would be very helpful.

Sample dataset:

样本数据集

Output:

输出

Add a column that extracts the first few characters of the first column. Then create a pivot table with that new column in the rows and the other columns in the values area. No VBA required.

you may try the following (commented) code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM