Excel VBA从缩进的BOM中查找零件号

时间:2017-09-27 15:22:57

标签: excel vba excel-vba

我们有一个软件(Solidworks),我们在Excel电子表格中提取物料清单。 它返回以下数据:
BOM Export result

我想创建一个VBA宏,用父部件号填充C列(父)。例如,单元格C6将显示:101-07798-111。

我设法直接在工作表中使用Excel公式,但是我想用VBA宏来做。

excel公式需要2列。 " D栏和#34;我把一个字母和#A;列A"的数据连接在一起。 " E列和#34;这是一个索引(匹配)搜索"列A"数据返回" B列"。

的值

D栏公式:= CONCATENATE(" A&#34 ;; A3)*没有这一步,主公式有错误

E栏公式:= INDEX($ B $ 1:$ B $ 250; MATCH((IFERROR)(LEFT(D3; FIND)(" $&#34 ;; SUBSTITUTE(D3;"。&# 34 ;;" $&#34 ;; LEN(D3)-LEN(SUBSTITUTE(D3;"。&#34 ;;"")))) - 1); " - &#34)); $ d $ 1:$ d $ 250; 0))

我找到了让VBA脚本用公式填充行的方法;但是因为这个公式含有很多"它会导致脚本出错。

在"列a"中使用数据的最佳方法是什么?获得" B列和34号的价值;在vba脚本中?

谢谢

1 个答案:

答案 0 :(得分:0)

我想到了什么,我想弄清楚这一点,所以我就是这样做的。

Dim splitVariable As Variant
Dim level As Integer
Dim stringToFind As String

For Each cell In Range("A1:A" & [A1].End(xlDown).Row)

    splitVariable = Split(cell.Value, ".") 'split the cell on the period to find the level

    level = UBound(splitVariable) + 1 'add one because array is 0 indexed

    If level > 1 Then 'don't do anything for the first level

        stringToFind = Left(cell, level - 3 + level) 'get the level of the parent

        For Each parentCell In Range("A1:A" & [A1].End(xlDown).Row)  'loop through rang again to find level

            If parentCell.Value = stringToFind Then 'when the parent is found then paste it to column C
                cell.Offset(0, 2) = parentCell.Offset(0, 1)
                Exit For
            End If
        Next
    End If
Next

根本不知道这是否有帮助。