搜索数组并将其吐出不同的列

时间:2015-04-23 04:49:51

标签: excel excel-vba excel-2010 vba

有没有办法将不同列str1 + str2 / str3-str4 * str5-str6中的数学计算分解为str1,str2,str3 str4,str5,str6等列。没有特定的顺序,可能会多次出现。

1 个答案:

答案 0 :(得分:2)

This may do what you need:

Sub SplitOnSigns()
Dim X As Long, MyString As String, MySign As Variant, MyArr As Variant
MySign = Array("+", "-", "*", "/")
MyString = ActiveCell.Text
For X = LBound(MySign) To UBound(MySign)
    MyString = Replace(MyString, MySign(X), "|")
Next
MyArr = Split(MyString, "|")
Range(ActiveCell.Offset(0, 1).Address & ":" & ActiveCell.Offset(0, UBound(MyArr) + 1).Address) = MyArr
End Sub

You can add more entries here:

MySign = Array("+", "-", "*", "/")

If there are more signs to split on. This assumes your data doesn't have pipes in "|" We can change the split char to an unused one if needed.

It works by progressivly replacing the signs with a pipe, it then splits the string to an array using the pipe as a split, finally it posts the array to the range next to the active cell