excel - 在字符串中每n个字符后插入一个空格

标签 excel

使用 MS excel - 使用类似于 0x80808080800180808080808080808080 的 34 个字符长度的字符串 - 需要转换为类似于 80 80 80 80 80 01 80 80 80 80 80 80 80 80 80 的字符串80 - 即去掉开头的 0x 并在原始字符串的每两个字符之间添加一个空格。

第一部分很好 - REPLACE(A1,1,2,"") 做到了。

需要第二部分的帮助。

更新:-

@Jeeped 的基于 UDF 的解决方案的回答 below有效 - 但需要非 UDF 解决方案。

最佳答案

尝试这个用户定义的函数。

Option Explicit

Function spaceBigHex(str As String, _
                     Optional delim As String = " ")
    Dim i As Long, var As Variant

    'strip off the 0×
    str = Mid(str, 3)

    'insert spaces from right to left        
    For i = Len(str) - 2 To 2 Step -2
        str = Left(str, i) & delim & Mid(str, i + 1)
    Next i

    spaceBigHex = str
End Function

enter image description here

这也可以通过一系列 native 工作表 REPLACE 来完成提供静态字符串长度的函数。

=REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(
 REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(RIGHT(A3, LEN(A3)-2),
  31, 0, " "), 29, 0, " "), 27, 0, " "), 25, 0, " "), 23, 0, " "),
  21, 0, " "), 19, 0, " "), 17, 0, " "), 15, 0, " "), 13, 0, " "),
  11, 0, " "), 9, 0, " "), 7, 0, " "), 5, 0, " "), 3, 0, " ")

换行只是为了帮助您理解它。留下它们或移除它们;闻一闻并不重要。

       enter image description here


1 用户定义函数(又名 UDF)被放入标准模块代码表中。点击 Alt+F11,当 VBE 打开时,立即使用下拉菜单插入 ► 模块(Alt+I,M)。将函数代码粘贴到标题为 Book1 - Module1 (Code) 的新模块代码表中。点击 Alt+Q 返回到您的工作表。

关于excel - 在字符串中每n个字符后插入一个空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39050215/

相关文章:

excel - 尝试将值小于零的单元格格式化为逗号

python - 在 Pandas/Python 中格式化连接的列。

r - openxlsx错误: length of rows and cols must be equal

excel - 通过 pd.read_excel() 将 excel 工作表读取为多索引数据框

java - 如何在 apache POI 中读取 excel 文件的确切单元格内容

Excel 与多个 IF 的比较

java - 使用 Java 更新 Excel 中的公式

python - Pandas Left Merge 与 xlsx 和 CSV 在输出中生成空值列

excel - 我可以在 Excel 07 中将结构化引用设置为绝对吗?

java - 从现有 Excel 中提取具有 2 个精度值 (3.55) 的特定列。帮我制作一下,它有1个精度值(3.5,3.9)