简体   繁体   English

VBA Excel如果单元格包含字符,然后将文本拆分为列

[英]VBA excel if cell contains character then split text to columns

I have a fairly simple piece of code that doesn't seem to work. 我有一段相当简单的代码,似乎没有用。 I need the cells to split if a word contains the character "," but only if it does. 如果单词包含字符“,”(但仅包含字符),则需要拆分单元格。 I thought that Using IF, Then, Else would work but every time it reaches a cell with no "," then i end up with "TextToColumns method of Range class failed" 我以为使用IF,然后,Else可以工作,但是每次到达没有“,”的单元格时,我都会遇到“ Range类的TextToColumns方法失败”的情况

If InStr(1, (Range("C32").Value), ",") > 0 Then

Range("C32").TextToColumns Destination:=Range("B14"), DataType:=xlDelimited, _
Other:=True, OtherChar:=","

Else

End If

Any help would be appreciated 任何帮助,将不胜感激

Your code is ok. 您的代码还可以。 I've try it, and it works. 我已经尝试过了,而且效果很好。

Sub Split()
If InStr(1, (Range("C2").Value), ",") > 0 Then
Range("C2").TextToColumns Destination:=Range("B3"), DataType:=xlDelimited, _
Other:=True, OtherChar:=","
Else
MsgBox "kk"
End If
End Sub

With cel without "," I get msgbox "kk" - everytime. 如果使用cel而没有“”,则每次都会得到msgbox“ kk”。 I'm using MS 2010, maybe thats where the problem is? 我正在使用MS 2010,也许那是问题所在?

You do not need a program for it. 您不需要程序。 Just use Text to Columns from Data ribbon with , as data separator. 只需将带符号的从数据功能区中的文本转换为列,用作数据分隔符。 If the line does not contain a comma, then the line will not be split. 如果该行不包含逗号,则该行将不会拆分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM