【问题标题】:Splitting Cells - Application-defined or object-defined error [closed]拆分单元格 - 应用程序定义或对象定义的错误 [关闭]
【发布时间】: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).Row K 应该是 8

标签: excel vba


【解决方案1】:

根据 cmets:

  1. 应该是xlup 而不是xIUp。那是小写的L
  2. 使用 Split 而不是尝试使用 Right 和 Len 进行解析。
  3. 确保将工作表父级用于范围。
With ThisWorkbook.Worksheets("Aluminum Futures")
    Dim LR As Long
    LR = .Cells(.Rows.Count, 8).End(xlUp).Row
    
    Dim K As Long
    For K = 2 To LR
        Dim str() As String
        str = Split(.Cells(K, 8), "-")
         .Cells(K, 9).Value = str(UBound(str))
    Next K
End With

【讨论】:

  • @Bert328 ... 3.1。注意With..End With 块中的“.”前缀,系统地甚至在.Rows.Count 之前)
猜你喜欢
  • 2013-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多