2016-05-17 183 views
0

關於excel格式的簡短問題。單元格格式

我目前正在使用基於用戶表單的協議工具。用戶窗體基本上由兩個輸入窗口組成,一個用於加載現有的子彈點,另一個用於添加新的點。

此外,我希望將粗體字的日期添加到每個項目符號點。我通過搜索日期出現的字符串中的位置(通過instrrev),然後將接下來的10個字符的字體更改爲粗體字體來實現該功能。

現在,當創建一個新的項目符號點時,它的工作原理非常好,但是當我向現有主題添加一個額外的點或者當我更改舊的項目符號點(然後整個文本是粗體)時,它總是會出現混亂。任何人都知道這是爲什麼發生?

Private Sub Fertig_Click() 
    Dim neu As String 
    Dim i As Integer 
    neu = Date & ": " & mitschrieb_neu.Value 


    'No Changes 
    If mitschrieb_neu.Value = "" And mitschrieb_alt.Value = ActiveCell.Value Then 
     Unload Me 
     Exit Sub 
    End If 

    'First bullet point 
    If mitschrieb_neu.Value <> "" And ActiveCell.Value = "" Then 
     ActiveCell.Value = neu 
     i = InStrRev(ActiveCell.Value, Date) 
     ActiveCell.Characters(i, 10).Font.Bold = True 
     Unload Me 
     Exit Sub 
    End If 

    'New bullet point 
    If mitschrieb_neu.Value <> "" And ActiveCell.Value <> "" Then 
     ActiveCell.Value = ActiveCell.Value & Chr(10) & neu 
     i = InStrRev(ActiveCell.Value, Date) 
     ActiveCell.Characters(i, 10).Font.Bold = True 
     Unload Me 
     Exit Sub 
    End If 

    'Changed an old bullet point 
    If mitschrieb_neu.Value = "" And mitschrieb_alt.Value <> ActiveCell.Value Then 
     ActiveCell.Value = mitschrieb_alt.Value 
     Unload Me 
     Exit Sub 
    End If 

End Sub 

回答

0

一旦執行此:

ActiveCell.Value = ActiveCell.Value & Chr(10) & neu 

Bold設定爲細胞變得均勻 - 它會刪除串格式的任何知識。

因此,解決方案是解析循環中的完整值,並確定所有日期並使其變爲粗體。

同時,我建議一些方法來減少代碼重複,合併所有不同的情況(第一顆子彈,而不是第一發子彈,只能修改)到一個通用的方法:

Private Sub Fertig_Click() 
    Dim neu As String 
    Dim i As Integer 

    'No Changes 
    If mitschrieb_neu.Value = "" And mitschrieb_alt.Value = ActiveCell.Value Then 
     Unload Me 
     Exit Sub 
    End If 

    ' Join the old value with the new value and put a linefeed 
    ' in between only if both are not empty. 
    ' Also insert the date before the new value, if it is not empty 
    ActiveCell.Value = mitschrieb_alt.Value _ 
     & IIf(mitschrieb_alt.Value <> "" And mitschrieb_neu.Value <> "", Chr(10), "") _ 
     & IIf(mitschrieb_neu.Value <> "", Date & ": " & mitschrieb_neu.Value, "") 

    ActiveCell.Font.Bold = False ' start with removing all bold 
    ' Search for all colons and put prededing date in bold (if it is a date) 
    i = InStr(ActiveCell.Value, ": ") 
    Do While i 
     ' Make sure to only put in bold when it is a date, otherwise skip this ":" 
     If i > 10 And IsDate(Mid(ActiveCell.Value, i - 10, 10)) Then 
      ActiveCell.Characters(i - 10, 10).Font.Bold = True 
     End If 
     ' find next 
     i = InStr(i + 1, ActiveCell.Value, ": ") 
    Loop 

    Unload Me 
End Sub 
+0

美麗。非常感謝你。 – Maverick13