TextToColumns VBA機能を使用します - '
を保持します。このコードは、範囲ではなく単一のセルでのみ機能します。
Sub Test()
With ThisWorkbook.Worksheets("Sheet1")
TextToCols .Range("A1"), .Range("B1")
End With
End Sub
'Comments indicate how to add a blank column between words.
Public Sub TextToCols(DataRange As Range, Optional DestinationRange As Range)
Dim nElements As Long
Dim vFieldInfo As Variant
Dim x As Long
If DataRange.Cells.Count = 1 Then
'Add an extra space to each space (1 space becomes 2).
DataRange = Replace(DataRange, " ", " ")
If DestinationRange Is Nothing Then
Set DestinationRange = DataRange
End If
nElements = Len(DataRange.Value) - Len(Replace(DataRange.Value, " ", ""))
ReDim vFieldInfo(1 To nElements)
For x = 1 To nElements
vFieldInfo(x) = Array(x, 1)
Next x
'Add ConsecutiveDelimiter:=False to the TextToColumns.
DataRange.TextToColumns _
Destination:=DestinationRange, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Space:=True, _
FieldInfo:=vFieldInfo
'Remove the extra space (2 spaces becomes 1)
DataRange = Replace(DataRange, " ", " ")
End If
End Sub
戻り値この:
falseに言葉とConsecutiveDelimiter
セットの間に余分なスペースで、それはこの返します
このコードのブロックを使用すると、テキスト文字列を渡すことができます手順、TextToCols
手順に進みます。コードを組み合わせて範囲文字列またはテキスト文字列を受け入れることもできますが、それはかなりの量の余分なコードになります。
コードを元の場所に変更した箇所を示すコメントを追加しました。
Sub Test()
With ThisWorkbook.Worksheets("Sheet1")
TextToCols "Kids 'R' Kids", .Range("B1")
End With
End Sub
Public Sub TextToCols(TextToSplit As String, _
DestinationRange As Range)
Dim nElements As Long
Dim vFieldInfo As Variant
Dim x As Long
Dim wrkSht As Worksheet
Dim DataRange As Range
'Add a temporary worksheet to perform the split on.
Set wrkSht = DestinationRange.Parent.Parent.Worksheets.Add
wrkSht.Cells(1, 1) = TextToSplit
Set DataRange = wrkSht.Cells(1, 1)
'Don't need this line anymore as a text string will never be counted in cells.
'If DataRange.Cells.Count = 1 Then
'Add an extra space to each space (1 space becomes 2).
DataRange = Replace(DataRange, " ", " ")
'Can remove this code block as DestinationRange
'can't be optional with a text string - we need somewhere to paste the data.
' If DestinationRange Is Nothing Then
' Set DestinationRange = DataRange
' End If
nElements = Len(DataRange) - Len(Replace(DataRange, " ", ""))
ReDim vFieldInfo(1 To nElements)
For x = 1 To nElements
vFieldInfo(x) = Array(x, 1)
Next x
'Add ConsecutiveDelimiter:=False to the TextToColumns.
'Note: DestinationRange is always the same sheet as DataRange.
' Even if DestinationRange is pointing to another sheet, it will split
' to the address but on the DataRange sheet.
DataRange.TextToColumns _
Destination:=DestinationRange, _
DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Space:=True, _
FieldInfo:=vFieldInfo
'This line looks at the correct DestinationRange address but on the temp sheet.
'It then resizes that range to however many cells were split to and copies that
'to our real destination.
DataRange.Parent.Range(DestinationRange.Address).Resize(, x).Copy _
Destination:=DestinationRange
'Can remove this line as well - the whole sheet is deleted after the split.
'Remove the extra space (2 spaces becomes 1)
'DataRange = Replace(DataRange, " ", " ")
'End If
'Delete the temporary sheet.
Application.DisplayAlerts = False
wrkSht.Delete
Application.DisplayAlerts = True
End Sub
これがA1にあり、「KIDS 'R' KIDS」を使用していると仮定すると、結果はどのようになると思いますか? A2 = "KIDS" A3 = "R" "A4 =" KIDS "? – 99moorem
分割する前にすべての一重引用符をchr(39)に置き換えてみてください。 –
@ 99mooremはい..あなたは正しいです –