我必须将单元格中的数字范围转换为列中的数字序列,如下所示:
10&-2&&-5&-8
到
10
12
13
14
15
18
我对此一无所知。请帮忙给我一些想法。 非常感谢你。
答案 0 :(得分:0)
解决方案假设一个四部分字符串
假设你有一个固定模式,总是有四个部分,而你的字符串是由"-"
分隔的,我使用Split
函数和用户定义的辅助函数推导出以下逻辑:
示例代码
以下代码将有点奇怪的字符串" 10& -2& -5& -8" 分成四部分,计算主要数字,分配定义的序列包括数组的第一个和最后一个元素,并将值写回一列。
Sub WriteSequence()
Dim s As String, i As Integer, ii As Integer
Dim first As Long, last As Long, start As Long
Dim vParts, v ' variant
' string example
s = "10&-2&&-5&-8"
' tokenize string parts
vParts = Split(s, "-") ' split at delimiter "-"
' get main numbers
first = getNumber(vParts(0)) ' get first number
start = getNumber(vParts(1)) + first ' calculate start number (from)
ends = getNumber(vParts(2)) + first ' calculate ends numnber (to)
last = getNumber(vParts(3)) + first ' calculate last number
' write to array
ReDim v(0 To last - first + 1)
v(0) = first
For i = 1 To ends - start + 1
v(i) = start + i - 1
Next i
v(i) = last
' write back to sheet
ThisWorkbook.Worksheets("Sheet1").[A2].Resize(i + 1, 1) = WorksheetFunction.Transpose(v)
End Sub
辅助功能
Function getNumber(ByVal s As String) As Long
' Purpose: return a number from a string ending with "&" or "&&"
getNumber = Val(Split(s, "&")(0))
End Function