如果引用单元格的唯一添加是“-LA”或“-LED”,如何获取另一个单元格的值

时间:2016-12-01 16:40:11

标签: excel vba if-statement formula formulas

所以我有一整页商品编号,需要每天更新数量。有许多项目包含两个附件作为不同的项目编号,如下所示:

1114            
1114-LA 
15894/3
1114-LED

我需要做的是两个数字的值(数量),-LA-LED等于原始项目编号(例如1114)数量。有些项目没有其他选项,有些只有一种,有些只有两种。

关于我如何做到这一点的任何想法?电子表格长达数千个数字。

这就是它的样子:

Item number     Quantity
1114               5
1114-LA            0
1114-LED           0
225896/3           2
225896-LED         0
114895             8
114895-LED         91
114895-LA          0
115938/2           91
115938/2-LED       0

1 个答案:

答案 0 :(得分:0)

OP的数据模式更改后

已编辑

根据你的例子,假设:

  • 每个商品编号的未知配件

  • 配件紧跟在料号

  • 之后
  • 数量与列相邻的项目编号

你可以试试这个:

Sub main()
    Dim iRow As Long

    iRow = 1
    Do While iRow < Cells(Rows.Count, 1).End(xlUp).Row '<--| change '1' to your actual columnn with item numbers index
        If InStr(Cells(iRow, 1).Value, "-") = 0 Then
            Do While InStr(Cells(iRow + 1, 1).Value, "-") > 0
                Cells(iRow + 1, 2).Value = Cells(iRow, 2).Value
                iRow = iRow + 1
            Loop
        Else
            iRow = iRow + 1
        End If
    Loop
End Sub

而另一种Autofilter() +“公式”方法如下:

Sub main2()
    With Range("B1", Cells(Rows.Count, 1).End(xlUp)) '<--| assuming data are in range "A1:Bx" with row 1 headers
        .AutoFilter field:=1, Criteria1:="*-*"
        With .Resize(.Rows.Count - 1, 1).Offset(1, 1)
            .SpecialCells(xlCellTypeVisible).FormulaR1C1 = "=R[-1]C"
            .Parent.AutoFilterMode = False
            .Value = .Value
        End With
    End With
End Sub

其中第1行必须是“标题”行