我在B列中有以下数据字符串。我想使用Excel VBA 2007将文本转换为不同的列。在下面的示例中,有4组数据,除以“..”。但是,也可以有2或3组数据。
示例4组:
InstrumentBaseClass..Mega~Corporate~InstrumentBaseClass..Mid~Energy~InstrumentBaseClass..Micro~Oil Field Services
示例2组:
InstrumentBaseClass..Mega~Corporate
如何将文本设置到不同的列中,以便如果只有2组数据,则后面的两列会说“无”?如果有3组数据,我需要一列“无”,依此类推。
答案 0 :(得分:0)
一种简单的,没有编程方式的方法是简单地用无填充的所有4列,然后让文本到列的功能覆盖任何“无需它”。
答案 1 :(得分:0)
以下代码使用硬编码输入,但可以用范围引用替换,或者在调用sub时作为varibles传递。
Sub TextDotColumns()
Dim sInput As String, sMid As String, rOutput As Object 'these should be passed when calling sub
Dim lBreak As Long, lColOff As Long, sCheck As String
sInput = "InstrumentBaseClass..Mega~Corporate~InstrumentBaseClass..Mid~Energy~InstrumentBaseClass..Micro~Oil Field Services"
sCheck = ".."
Set rOutput = Range("d10")
lBreak = InStr(1, sInput, sCheck, vbTextCompare)
rOutput.Select
lColOff = 1
Do While lBreak > 0
sMid = Mid(sInput, 1, lBreak - 1)
rOutput.Offset(1, lColOff).Value = sMid
sInput = Right(sInput, Len(sInput) - lBreak - 1)
lColOff = lColOff + 1
lBreak = InStr(1, sInput, sCheck, vbTextCompare)
Loop
rOutput.Offset(1, lColOff).Value = sInput
End Sub
答案 2 :(得分:0)
试试这个:
Sub Test()
'// Assumes your range is in column A
With Range("A2", Cells(Rows.Count, "A").End(xlUp))
'// Text to columns
.TextToColumns Destination:=.Offset(, 1), DataType:=xlDelimited, _
TextQualifier:=xlNone, ConsecutiveDelimiter:=True, Other:=True, OtherChar:="."
'// Fill blank spaces with "None"
.Offset(, 1).Resize(, 4).SpecialCells(xlCellTypeBlanks).Value = "None"
End With
End Sub