Excel 2013自动格式化分隔符


-1

我想知道如何将列中的delimeted值转换为某种格式。例如,如果我在一个单元格/列“黑色;深蓝色;浅蓝色”中有以下值,并希望将其转换为如下所示“黑色:0:0:0:1 |深蓝色:0:0: 0:2 |浅蓝色:0:0:0:3“达到此结果的最佳方法是什么?


1
我确信一些巫师可以制作一个丑陋的excel公式来做到这一点,但我认为VBA最适合这种复杂的功能。这通常不是人们只为你编写代码的网站,但我想有人可能。如果有人给你VBA代码,你知道如何运行和排除故障吗?
HackSlash

@HackSlash 一般都是轻描淡写。SuperUser 不是免费的脚本服务。如果您希望我们为您提供帮助,您应该展示您尝试过的内容以及失败的地方,然后我们可以帮助您指出哪里出了问题。
Vylix

Answers:


0

我的时间毫无价值。

Sub delimitAddNum()
    'e.g. input: Black;Dark Blue;Light Blue
    'e.g. output: Black:0:0:0:1|Dark Blue:0:0:0:2|Light Blue:0:0:0:3
    theStr = ActiveCell.Value

    If InStr(theStr, ";") = False Then Exit Sub

    theSplit = Split(theStr, ";")

    theResult = ""

    For i = 0 To UBound(theSplit)
        If i < UBound(theSplit) Then
            theResult = theResult & theSplit(i) & ":0:0:0:" & (i + 1) & "|"
        Else
            theResult = theResult & theSplit(i) & ":0:0:0:" & i + 1
        End If
    Next i

    theChk = MsgBox("Do you want to overwrite cell " & Replace(ActiveCell.Address, "$", "") & _
    " with the following result?" & vbNewLine & vbNewLine & _
    theResult, vbYesNo, "Delimiter, Number Thingy")

    If theChk = vbYes Then ActiveCell.Value = theResult
End Sub
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.