2010-01-27 89 views
1

我有一个宏用于从InputBox接收数据,然后将该数据插入到单元格中。在下面的宏运行之后,我对数据有一些格式问题。Excel Word Wrap在宏后出现错误

Sub InsertNotes() 
' 
' insertnotes Macro 
' 

' 
    Dim UserNotes As String 

    UserNotes = InputBox(Prompt:="Please enter your note below:", Title:="Note input", Default:="Notes") 
    If UserNotes = "" Then Exit Sub 
    ActiveSheet.ListObjects("Notes").ListRows.Add (1) 
    ActiveSheet.Range("Notes").Cells(1, 1) = Date 
    ActiveSheet.Range("Notes").Cells(1, 2) = UserNotes 

End Sub 

表单元格被设置为具有自动换行功能,但是当笔记插入到表格中时,单元格不会被打包。但是,如果我再次运行宏并插入新笔记,则插入的先前笔记将显示为包装,即使没有发生任何事情发生,除非向下移动一行。有什么我可以在代码或格式中做到让它正确包装?

+0

我在这里发布了一个相关的答案,使用临时形状而不是临时行:http://stackoverflow.com/questions/1718874/predict-text-wrapping-in-cell-excel-2000-using-vba/15370350# 15370350 – SimpleSam5 2013-03-12 19:29:38

回答

0

线

ActiveSheet.ListObjects("Notes").ListRows.Add (1) 

增加了新的细胞来你的表从列表标题继承其格式。所以你必须做的是确保标题单元格也启用了自动换行功能。作为替代方案,你可以在后面加上了自动换行属性,像这样:

ActiveSheet.Range("Notes").Cells(1, 2).WrapText = true 
+0

没有骰子文件。标题已启用WordWrap。我尝试添加WrapText属性,但似乎也没有工作。 – 2010-01-27 22:04:24

+0

“笔记”究竟是如何定义的? – 2010-01-28 18:30:04

+0

好吧,我在Excel中突出显示了一个范围,并使用格式作为表格功能来创建并命名它。 – 2010-01-29 15:08:17

0

到目前为止,这是插入我需要的行,然后插入和删除一个多行我发现的唯一的解决方法之后。由于某些原因,wrap属性将会在插入后开始工作(然后在不需要时将其删除)。

Sub InsertNotes() 
' 
' insertnotes Macro 
' 

' 
    Dim UserNotes As String 
    ' Turn off screen updating 
    Application.ScreenUpdating = False 
    UserNotes = InputBox(Prompt:="Please enter your note below:", Title:="Note input", Default:="Notes") 
    If UserNotes = "" Then Exit Sub 
    ActiveSheet.ListObjects("Notes").ListRows.Add (1) 
    ActiveSheet.Range("Notes").Cells(1, 1) = Date 
    ActiveSheet.Range("Notes").Cells(1, 2) = UserNotes 
    ActiveSheet.Range("Notes").Cells(1, 2).WrapText = True 
    ' Crap fix to get the wrap to work. I noticed that after I inserted another row the previous rows 
    ' word wrap property would kick in. So I just add in and delete a row to force that behaviour. 
    ActiveSheet.ListObjects("Notes").ListRows.Add (1) 
    ActiveSheet.Range("Notes").Item(1).Delete 
    Application.ScreenUpdating = True 

End Sub 

看起来不是很理想,但它完成工作直到我能够知道正确的答案是什么。