Excel搜索并添加行

时间:2018-03-29 18:09:10

标签: excel excel-vba vba

我搜索了这个网站,我是一个新手,但试图修改一个朋友制作的代码。目前,它搜索一行(rowname),当匹配(prodcat)从一个电子表格中获取数据并将其记录到另一个(dashboardFile)时。问题是,我有多行具有相同的(prodcat)所以不是立即记录,我想继续搜索,如果它找到另一个相同的(prodcat),添加数据和记录。希望这对某人有意义。这是代码

    Windows(FYfile).Activate
    Sheets("Product by Branch").Select
    Range("A1").Select
    Do Until datayear = 7 '5
        datayear = datayear + 1

        foundsap = False
        sapRow = 5

        Do Until foundsap = True
            div = ActiveCell.Offset(sapRow - 1, 0).Range("A1").Value
            If InStr(div, sap) > 0 Then
                prodrow = sapRow + 1
                foundsap = True
                endDiv = False
                foundMatch = False
                Do Until endDiv = True Or foundMatch = True
                    rowname = ActiveCell.Offset(prodrow - 1, 0).Range("B1").Value
                    'MsgBox (rowname)
                    If prodcat = rowname Then
                        'MsgBox (rowname & " matches " & prodcat & " on row " & prodrow)
                        foundMatch = True
                        If datayear > 5 Then
                            datum = ActiveCell.Offset(prodrow - 1, 0).Cells(1, 4 + datayear).Value
                        Else
                            datum = ActiveCell.Offset(prodrow - 1, 0).Cells(1, 3 + datayear).Value
                        End If
                        'MsgBox (datum)
                    ElseIf InStr(rowname, "Prod Cat") > 0 Then
                        endDiv = True
                        'MsgBox ("Didn't find " & prodcat)
                        datum = 0
                    End If

                    prodrow = prodrow + 1
                Loop

                'switch to Charts and record datum
                Windows(dashboardFile).Activate
                Sheets("Charts").Select
                Range("A1").Select
                ActiveCell.Offset(43 * (boxes - 1) + 1, 0).Range("A1").Select
                ActiveCell.Offset(sectionrows + metric, sectionCols + datayear + 1).Range("A1").Value = datum
                'switch back to FY
                Windows(FYfile).Activate

0 个答案:

没有答案