Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
Vba 向excel单元格中添加以单引号开头的字符串_Vba_Excel - Fatal编程技术网

Vba 向excel单元格中添加以单引号开头的字符串

Vba 向excel单元格中添加以单引号开头的字符串,vba,excel,Vba,Excel,我试图自动执行文本到列的功能,通过VBA在excel中用空格分隔 strArray = split (currentSheet.Cells(i,1).Value), " ") For j = 0 To (UBound(strArray) - LBound(strArray)) currentSheet.Cells(i, 1 + j).NumberFormat = "@" currentSheet.Cells(i, 1 + j).Value = strArray(j)

我试图自动执行文本到列的功能,通过VBA在excel中用空格分隔

strArray = split (currentSheet.Cells(i,1).Value), " ")

For j = 0 To (UBound(strArray) - LBound(strArray))
       currentSheet.Cells(i, 1 + j).NumberFormat = "@"
       currentSheet.Cells(i, 1 + j).Value = strArray(j)
Next j
当我收到一些文本时遇到的问题,如:KIDS'R'KIDS

第二个单词'R'在单元格中变为R',但是,如果我在excel中使用空格作为分隔符构建文本到列函数,则该单词仅在单元格中作为“R”出现

如果我在开头加上一个单引号,问题就解决了,但我认为这可能会影响我对数据的进一步处理

有什么办法吗?

我想你会喜欢“孩子们的R”孩子,就像“孩子们”,“R”,“孩子们”

替换下面的

currentSheet.Cells(i, 1 + j).Value = strArray(j)

基本上,在输出之前要分析文本。因为Excel会假定第一个“'”是一个特殊字符并将其删除。

我假设您希望“KIDS'R'KIDS”像“KIDS”、“R'”和“KIDS”

替换下面的

currentSheet.Cells(i, 1 + j).Value = strArray(j)


基本上,在输出之前要分析文本。因为Excel将假定第一个“'”是特殊字符并将其删除。

使用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
返回此值:

如果单词和
连续delimiter
之间有一个额外的空格设置为false,则返回以下内容:

此代码块允许您将文本字符串传递给
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

使用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
返回此值:

如果单词和
连续delimiter
之间有一个额外的空格设置为false,则返回以下内容:

此代码块允许您将文本字符串传递给
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”?尝试在拆分之前用chr(39)替换所有单引号。@99moorem是。。你是对的,如果我们假设这是在A1中,使用“KIDS’R’KIDS”,你会期望得到什么结果?A2=“KIDS”A3=“'R'”A4=“KIDS”?尝试在拆分之前用chr(39)替换所有单引号。@99moorem是。。你说得对,你的答案对我来说非常有效。只需要一个人帮忙就行了。我试图在每个备用单元格中填充结果。在本例中,结果应在B、D、F列中出现。有什么办法吗?为此,我会在单词之间添加一个额外的空格,并告诉列中的文本不要将连续分隔符视为一个分隔符。我将更新我的代码。是否可以发送字符串而不是单元格地址?与TextToCols teststring类似,.Range(“B1”)
TextToColumns
必须接受一个Range对象,因此不能只向其传递文本字符串。但是你可以做一些修改——创建一个临时工作表,将文本粘贴到其中,拆分它,然后复制并粘贴到目标单元格。我添加了第二个代码块来显示这一点。在文本列出现之前,我必须在不更改原始单元格的情况下进行更多的数据操作。这就是我问的原因。不管怎样,我已经想出了一个办法。我只是按照我想要的方式格式化文本,然后放入目标单元格,然后从那里使用textocolumns。谢谢@darren的帮助。这帮了大忙。你的回答对我来说非常有效。只需要一个帮助就可以了。我试图在每个备用单元格中填充结果。在本例中,结果应在B、D、F列中出现。有什么办法吗?为此,我会在单词之间添加一个额外的空格,并告诉列中的文本不要将连续分隔符视为一个分隔符。我将更新我的代码。是否可以发送字符串而不是单元格地址?与TextToCols teststring类似,.Range(“B1”)
TextToColumns
必须接受一个Range对象,因此不能只向其传递文本字符串。但是你可以做一些修改——创建一个临时工作表,将文本粘贴到其中,拆分它,然后复制并粘贴到目标单元格。我添加了第二个代码块来显示这一点。在文本列出现之前,我必须在不更改原始单元格的情况下进行更多的数据操作。这就是我问的原因。不管怎样,我已经想出了一个办法。我只是按照我想要的方式格式化文本,然后放入目标单元格,然后从那里使用textocolumns。谢谢@darren的帮助。这很有帮助。