Excel VBA从公式中删除等号

时间:2016-11-16 08:45:00

标签: excel vba excel-vba

我的vba代码存在问题。我不知道为什么,但它在公式前删除了等号......也许有人知道如何解决它?

enter image description here

以下是Excel Sheet的完整源代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim c As Range
    For Each c In Target.Cells
        If Not Intersect(c, Range("X:AI")) Is Nothing Then
            If c > Range("H" & c.Row).Value Or c < Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("H" & c.Row).Value And c >= Range("G" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If


            Dim frml1 As String
            Dim frml2 As String
            frml1 = "LARGE(X" & c.Row & ":AI" & c.Row & ";1)"
            frml2 = "SMALL(X" & c.Row & ":AI" & c.Row & ";1)"
            Range("AK" & c.Row).Value = "=AVERAGE(X" & c.Row & ":AI" & c.Row & ")"
            Range("AJ" & c.Row).Value = "=" & frml1 & " - " & frml2
        ElseIf Not Intersect(c, Range("AL:AM")) Is Nothing Then
            If c > Range("K" & c.Row).Value Or c < Range("J" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("K" & c.Row).Value And c >= Range("J" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ...
        ElseIf Not Intersect(c, Range("AU:AU")) Is Nothing Then
            If c > Range("Q" & c.Row).Value Or c < Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("Q" & c.Row).Value And c >= Range("P" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        ElseIf Not Intersect(c, Range("AY:AY")) Is Nothing Then
            If c > Range("T" & c.Row).Value Or c < Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 3
            ElseIf c <= Range("T" & c.Row).Value And c >= Range("S" & c.Row).Value Then
                c.Font.ColorIndex = 10
            End If
        End If
    Next c
End Sub

2 个答案:

答案 0 :(得分:3)

你有“;”在你的公式而不是“,”。也许是因为你的公式错误地删除了它。

答案 1 :(得分:1)

如果您在Excel中进行此类编码,请务必注意以下事项:

?application.PathSeparator
?application.DecimalSeparator
?Application.International(xlFormula)

将它们写在VB编辑器的即时窗口中,以查看结果。然后你可以在VBA代码中使用它们,这可以在德国和美国使用。