【发布时间】:2022-01-28 01:43:22
【问题描述】:
我的代码有什么问题?
我想从零件号中提取油漆代码。零件号在“H”列中,我想要在“I”列中的油漆代码。例如:对于GP231-5003-XCBK,我希望XCBK 出现在“I”列中。
这是目前为止的代码:
Dim K As Long
Dim LR As Long
LR = Cells(Rows.Count, 8).End(xIUp).Row
For K=2 to LR
Cells(K,9).Value = Right(Cells(K,8).Value, Len(Cells(K,8))-InStr(1, Cells(K,8).Value, "-"))
Next K
End sub
错误发生在LR = Cells(Rows.Count, 8).End(xIUp).Row
【问题讨论】:
-
它是
xlUp而不是xIUp。小写L不是大写I -
模块顶部需要
Option Explicit。 -
我会使用:
Cells(K,9).Value = Split(Cells(K,8).Value,"-")(uBound(Split(Cells(K,8).Value,"-"))) -
此外,我建议您完全限定您的范围引用,因为没有进一步的限定 VBA 可以解决任何当前活动的工作表,而这些工作表不需要是实际想要的参考。
-
.Cells(Rows.Count, K).End(xlUp).RowK 应该是 8