【问题标题】:Finding the last letter in the cell查找单元格中的最后一个字母
【发布时间】:2014-06-16 20:46:30
【问题描述】:

我有多行既是文本又是数字的字符串。我想在除第一部分和最后一部分之外的列中用空格分隔文本。

如果我将文本运行到由空格分隔的列,那么它会拆分我不想拆分的字符串。

例子:

Quarterly Performance Numbers 999,999.99 12.00 1.00 2.00 3.00 4.00 Dec 09, 2013

变成:

Quarterly|Performance|Numbers|999,999.99|12.00|1.00|2.00|3.00|4.00|Dec|09,|2013

我想要什么:

Quarterly Performance Numbers|999,999.99|12.00|1.00|2.00|3.00|4.00|Dec 09, 2013

问题在于该部分的长度和字数不同(从 3 到 6 不等)。

有没有一种方法可以在 VBA 中围绕这两个字符串创建文本限定符?

Sub Macro3()


Dim i As Integer
Dim LastRow As Long

LastRow = ActiveSheet.UsedRange.Rows.Count

For i = 1 To LastRow

Cells(i, 2).Value = Mid(Cells(i, 1), 29, 37)
Cells(i, 3).Value = Right(Cells(i, 1), 12)
Cells(i, 1).Value = Left(Cells(i, 1), 37)

Next i


End Sub

【问题讨论】:

  • 这些数据是从哪里来的?你能说服来源将其导出为 CSV 吗?
  • 是的,在 VBA 中有一种方法可以做到这一点,如果您向我们展示您的尝试,我们可以帮助您调试它。
  • 查尔斯:我试过了。消息来源仅将内容放在 PDF 中,但表示将来可能会切换。暂时我只是把它复制到excel中。
  • Jean:我喜欢你的风格。给我一分钟。
  • 格式有多大不同?几乎总是这组数据,只是不同的数字?

标签: vba excel split string-concatenation


【解决方案1】:
Sub Test()
    Dim sContent, oMatch, arrParsed(), sResult
    sContent = "Quarterly Performance Numbers 999,999.99 12.00 1.00 2.00 3.00 4.00 Dec 09, 2013"
    arrParsed = Array()
    With New RegExp ' Tools - References - add "Microsoft VBScript Regular Expressions 5.5" or use With CreateObject("VBScript.RegExp")
        .Pattern = "(?:(?:[a-z ]+(?= )){3,6}|(?:-*[\d,.]+(?= ))|(?:[a-z]{3} \d{2}, \d{4}))"
        .Global = True
        .IgnoreCase = True
        For Each oMatch In .Execute(sContent)
            ReDim Preserve arrParsed(UBound(arrParsed) + 1)
            arrParsed(UBound(arrParsed)) = oMatch.Value
        Next
    End With
    ' here you can use arrParsed
    sResult = Join(arrParsed, ";")
    MsgBox sResult
End Sub

【讨论】:

  • 这适用于大多数行。你能帮我理解这个模式吗?它似乎也删除了一些东西。
  • 我的线索是从例如http://msdn.microsoft.com/en-us/library/1400241x.aspx 开始,使用特殊的Notepad++ RegEx Helper 插件或另一种工具来创建模式也很方便。对于哪些字符串该模式不起作用?我想我忘记了负数的减号?
  • 有一些第一个字符串是连字符的,它只是删除了字符串的一部分。我正在检查那个链接。谢谢。
【解决方案2】:

我想出了一个可行的方法。正如我被警告的那样,RegEx 最终成为一个问题而不是解决方案。我的方法并不漂亮,但它完成了工作。在删除空格后,我通过字符串的长度减去字符串的长度来计算空格的数量。空格中唯一的变量来自描述中的空格数,所以如果我从总空格中减去 8 个最小空格,我可以用它用“;”替换第 N 个空格。我这样做了 7 次,我的所有列都以分隔文本正确显示到列中。谢谢大家。对不起,如果我的解释很糟糕。

Sub Macro14()

Dim i As Long
Dim smaller As String
Dim spaces As Integer
Dim fixed As String
Dim LastRow As Long

LastRow = ActiveSheet.UsedRange.Rows.Count

For i = 1 To LastRow

smaller = Replace(Cells(i, 1), " ", "")
spaces = Len(Cells(i, 1)) - Len(smaller) - 8

fixed = Cells(i, 1).Value
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after desc
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after value
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after %age
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after first perf column
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after second perf column
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after third perf column
fixed = WorksheetFunction.Substitute(fixed, " ", ";", spaces) 'after fourth perf column/before the date

Cells(i, 1).Value = fixed

Next i

End Sub

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 2014-08-28
    • 2017-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多