本周我花了半天时间学习vba,并建立了这个宏。经过大量研究,主要是在stackoverflow上完成 - 这是我到目前为止所做的。我编译时不断定义sub或函数。然后它突出显示单元格的世界单元格部分(i,“T”)。
我可以使用记录宏并自动填充一系列非常嵌套的ifs以获得正确的结果,但我想尝试学习一些vba代码。我知道公式,但有限的vba,所以我半依赖于评估函数。在此先感谢您的帮助。我在每段代码中评论了我的逻辑。
Sub GetCost()
GetCost Macro
Range("A2").Select
Selection.End(xlDown).Select
Dim X As Integer
X = Active.cell.Value + 1
'determines last row of imported data
For i = 2 To X
'rows 2 to x, x being the highest active row imported, ignoring header (row 1)
For J = 2 To 11
'columns from 2 up to 11 are evaluated
If Evaluate("iserror(Match(""cell(i, j).value"", Z:Z, 0), 2, 0") = False Then
'check to see if cell(i,j) value exists in column Z
If Evaluate("Index(Z:AA,Match(""cell(i, j).value"", Z:Z, 0), 2, ") = "N" Then
'check to see that
If Evaluate("isblank(""cell(i, j + 1)"")") = True Then
Set cell(i, "V").Value = cell(i, "T").Value
Exit For
'sets cost of parent item/subassembly to its current standard cost
Else: Set cell(i, "V").Value = 0
'if is false; parent item is purchased in its entirety and the current item belongs to parent item
'Sets value to 0, because the cost is inclusive of parent item
Exit For
End If
ElseIf J = 11 Then
Set cell(i, "V").Value = cell(i, "T").Value
'if no items match the lookup range, then current item is not an assembly or built inhouse, use current cost
End If
End If
Next J
Next i
EndSub
答案 0 :(得分:0)
使用单元格代替单元格并删除该集合。