2016-05-17 182 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