Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel VBA:将字符串中的数字和文本拆分为列_Vba_Excel - Fatal编程技术网

Excel VBA:将字符串中的数字和文本拆分为列

Excel VBA:将字符串中的数字和文本拆分为列,vba,excel,Vba,Excel,我的excel中有以下格式的列: Column A 1.2kg 100ml 2m 200 我需要运行VBA将数字和文本分别拆分为两列,如下所示: Column A | Column B 1.2 | kg 100 | ml 2 | m 200 | 我在这个网站上也发现了一个类似的问题,但是它不适用于我的VBA。有人能帮我吗 另外,我在手机上使用的是未经测试的excel 2007。它能满足你的需要吗 Option Explicit Sub Sp

我的excel中有以下格式的列:

Column A
1.2kg
100ml
2m
200
我需要运行VBA将数字和文本分别拆分为两列,如下所示:

Column A | Column B
1.2      |  kg
100      |  ml
2        |  m
200      |
我在这个网站上也发现了一个类似的问题,但是它不适用于我的VBA。有人能帮我吗


另外,我在手机上使用的是未经测试的excel 2007。它能满足你的需要吗

Option Explicit

Sub SplitValuesUnits()

' Code assumes values are on Sheet1. Change sheet name as needed.'

With thisworkbook.worksheets("Sheet1")

Dim LastRow as long
LastRow = .cells(.rows.count,"A").end(xlup).row

With .range("A1:B" & LastRow)

Dim ArrayOfValues() as variant
ArrayOfValues = .value2

Dim CharacterIndex as long
Dim RowIndex as long
For RowIndex = lbound(ArrayOfValues,1) to ubound(ArrayOfValues,1)
' Skip zero-length strings, as well as values which are already numbers and do not need splitting '
If len(ArrayOfValues(RowIndex,1)) <> 0 then
If not isnumeric(ArrayOfValues(RowIndex,1)) then

Dim CurrentValue as string
CurrentValue = ArrayOfValues(RowIndex,1)

' Loop through string backwards until we find a numeric value, at which point we assume there are no further non-numeric characters. '
For CharacterIndex = Len(CurrentValue) to 1 Step -1
If isnumeric(mid$(CurrentValue),CharacterIndex,1)) then exit for
Next CharacterIndex

If CharacterIndex >= 1 then
ArrayOfValues(RowIndex,1) = cdbl(left$(CurrentValue,CharacterIndex))
ArrayOfValues(RowIndex,2) = mid$(CurrentValue,CharacterIndex + 1)
End if

End if
End if

Next RowIndex

' Overwrite two columns with values. '
.value2 = arrayofvalues

End with

End with

End sub

张贴您正在处理的代码。它也可以通过公式来实现。除非您希望就地拆分覆盖内容,否则不需要VBA!此代码适用于我所需的工作。谢谢分享。