2010-01-27 90 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 

看起來不是很理想,但它完成工作直到我能夠知道正確的答案是什麼。