首页 > 解决方案 > vbNewLine 直到双击才反映?

问题描述

我有一些通过我的代码合并的单元格,

If InStr(outSht.Cells(lRow2, 3), entry.Offset(, 1)) = 1 Then 'does name exist?
            outSht.Cells(lRow2, 4) = outSht.Cells(lRow2, 4) & vbNewLine & entry.Offset(, 1)
        End If
        outSht.Cells(lRow2, 6) = outSht.Cells(lRow2, 6) & vbNewLine & entry.Offset(, -2)
        outSht.Cells(lRow2, 7) = outSht.Cells(lRow2, 7) + entry.Offset(, 2)

但是,它最终都在同一行,直到我手动双击单元格。这是一个快速 gif,显示正在发生的事情。

https://gyazo.com/eb50d0ecb25e022b53643a65917ac835

我错过了一些非常基本的东西吗?我试过了Chr(10),,Chr(13) & Chr(10)vbCrLf无济于事。

谢谢!

标签: excelvba

解决方案


我试图重现您遇到的问题,并且在将符号与 ACSII 13 一起使用时类似的工作。当使用具有 ASCII 10 的字符时,一切正常。尝试强制.WrapText = True属性。以下是调查问题的测试代码:

Sub test1()
    a = Array(vbLf, vbCr, vbCrLf, vbNewLine, Chr(10), Chr(13))
    
    ActiveSheet.Cells.Delete    ' clear the Worksheet
    
    With ActiveSheet.Range("A1")
        For i = 0 To UBound(a)
            txt = ""
            For s = 1 To Len(a(i))
                txt = txt & "," & Asc(Mid(a(i), s, 1))
            Next
            txt = Mid(txt, 2)
            
            .Offset(i).WrapText = False
            .Offset(i).Value = "A1-101222" & a(i) & "A1-103209" & a(i) & "A1-101406"
            .Offset(i, 1).Value = "Used " & i & " symbol(s) ASCII[" & txt _
                & "] and final WrapText is: " & .Offset(i).WrapText
        Next
    End With
    With ActiveSheet.UsedRange
        .EntireColumn.AutoFit
        .EntireRow.AutoFit
    End With
End Sub

输出 在此处输入图像描述


推荐阅读