Excel VBA文本导入直到双击单元格才能正确格式化换行。

huangapple go评论72阅读模式
英文:

Excel VBA text import not formatting line-breaks correctly until double-clicking on cell

问题

我有一个VBA例程,导入一个CSV文件,然后将其适配到特定表格的正确日期。导入功能正常,但换行符不显示正确,即使在公式栏中显示正确。

如果你双击单元格,格式就会显示正确,但表格中有很多行,我不想为每个单元格单独操作。

有人能解释为什么会发生这种情况吗?是否有一种特殊的VBA输入文本到单元格的方法?还是在导入结束时需要添加一个命令?

我正在使用自动换行和自动调整行高。

这是我输入文本到单元格的方式。

英文:

I have a VBA routine that imports a CSV and then fits it to a specific table for the correct dates. The import works correctly, but the line-breaks do not display correctly, even though they are displayed correctly in the formula bar.

If you double-click the cell then the formatting displays correctly, but there are many lines within the table and I do not want to have to do it for each cell individually.

Can anyone explain why this is happening, is there a special way to enter the text into the cell with VBA? Or is there a command that I need to add at the end of the import?

I am using wordwrap and also auto row-height.

This is how I am inputting the text into the cells.

            ws.Range("E" & LineRow).value = ReplaceCharacters(LineItems(1))
            ws.Range("F" & LineRow).value = ReplaceCharacters(LineItems(2))
            ws.Range("G" & LineRow).value = ReplaceCharacters(LineItems(3))
            ws.Range("H" & LineRow).value = ReplaceCharacters(LineItems(4))
            ws.Range("I" & LineRow).value = ReplaceCharacters(LineItems(5))
            ws.Range("J" & LineRow).value = ReplaceCharacters(LineItems(6))
            ws.Range("K" & LineRow).value = ReplaceCharacters(LineItems(7))
            ws.Range("L" & LineRow).value = ReplaceCharacters(LineItems(8))

This is how the text appears in the table upon input.

Excel VBA文本导入直到双击单元格才能正确格式化换行。

And this is how it appears in the formula bar.

Excel VBA文本导入直到双击单元格才能正确格式化换行。

Once I double-click the cell, this is how the cell appears (which is correct).

Excel VBA文本导入直到双击单元格才能正确格式化换行。

答案1

得分: 3

ws.Range("E" & LineRow).value = Replace(ReplaceCharacters(LineItems(1)), vbCr, vbCrLf, 1, , vbBinaryCompare)或在内部修改ReplaceCharacters函数以进行替换。

英文:
....
ws.Range("E" & LineRow).value = Replace(ReplaceCharacters(LineItems(1)), vbCr, vbCrLf, 1, , vbBinaryCompare)
....

or modify internally the ReplaceCharacters function, to do the replacement.

huangapple
  • 本文由 发表于 2023年6月8日 16:57:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定