【问题标题】:Excel Macro: If the characters " XX " are in the middle of a string then split the string at that pointExcel 宏:如果字符“XX”在字符串中间,则在该点拆分字符串
【发布时间】:2015-02-25 13:59:22
【问题描述】:

我正在尝试根据在字符串中查找特定字符将一列分为两列。

具体来说,如果 ColumnA 包含字符串“XX”,则将 ColumnD 中“XX”之前的所有内容复制,并将“XX”和之后的所有内容复制到 ColumnF。

Sub splitit()
Dim i As Long, l As Long
l = Cells(Rows.Count, 1).End(xlUp).Row
For i = l To 1 Step -1
    If InStr(Cells(i, 1).Value, celltxt, " XX ") Then
      Cells(i, 4).Value = Left(Cells(i, 1).Value, Find(" XX ", Cells(i, 1).Value) - 1)
      Cells(i, 6).Value = " XX " & Mid(Cells(i, 1).Value, Find(" XX ", Cells(i, 1).Value) + 1, Len(Cells(i, 1).Value))
    End if
Next i
End Sub

【问题讨论】:

标签: excel vba for-loop split


【解决方案1】:
Sub splitit()
Dim i As Long, l As Long,
Dim x as variant
l = Cells(Rows.Count, 1).End(xlUp).Row
For i = l To 1 Step -1
    If InStr(Cells(i, 1).Value, celltxt, "XX") <> 0 Then
      x = split(cells(i,1),"XX")
          cells(i,4) = x(0)
          cells(i,6) = " XX " & x(1)
    End if
Next i
End Sub

【讨论】:

  • 谢谢...但是在 InStr 行上出现类型不匹配错误。
  • 也许改成这样:If InStr(1,Cells(i, 1).text, " XX ")>0 Then
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-11
  • 2012-01-04
  • 2015-08-29
  • 2012-12-14
相关资源
最近更新 更多