VBA语法/代码检查

时间:2014-08-06 18:47:54

标签: excel vba excel-vba syntax

我被要求制作一个Excel宏来确定基于三个单词的点,然后根据点的范围对这些框进行着色,以便快速确定可以处理的内容。我是VBA的新手并且做了一些阅读,到目前为止,这是我想出的。我希望有人能告诉我,如果我走在正确的轨道上,或者我是否过度复杂化了。

这是我到目前为止所做的:

Sub Macro_Test()

    Dim TotalScore As Integer

    'Look through H to determine what word is contained and then add 
    'a value to the total score
    'When I try to add to the TotalScore the addition sign goes away 
    '(not sure if the .Value is supposed to be used)
    If (Sheet1.Columns(2, 8) = "Yes") Then
        TotalScore.Value 3
    ElseIf (H17 = "Partial") Then
        TotalScore.Value 2
    ElseIf (H17 = "No") Then
        TotalScore.Value 1

    'The 70th row in column 8(H) is equal/shows the total score
    Sheet1.Columns(70, 8) = TotalScore.Value

    'Color depending on the final score, depending on how line above 
    'works will change to the value in H70
    If (TotalScore < 86 And TotalScore > 69) Then
        'Find proper color for green
        H70.Interior.ColorIndex = 3
    ElseIf (TotalScore < 70 And TotalScore > 44) Then
        'Find proper color for yellow
        H70.Interior.ColorIndex = 2
    ElseIf (TotalScore < 45 And TotalScore > 17) Then
        'Find proper color for red
        H70.Interior.ColorIndex = 1
End Sub

这就是我现在所拥有的:

Sub Macro_Test()

    Dim TotalScore As Integer
    'Set the total score to zero
    TotalScore = 0
    Dim SrchRange As Range
    'Make a range that goes from H1 to H69
    Set SrchRange = Sheet1.Range("H1", "H69")


    'Look through H to determine what word is contained
    'and then add a value to the total score
    For Each FilledCell In SrchRange
        If (FilledCell = "Yes") Then
            TotalScore = TotalScore + 3
        ElseIf (FilledCell = "Partial") Then
            TotalScore = TotalScore + 2
        ElseIf (FilledCell = "No") Then
            TotalScore = TotalScore + 1
        End If
    Next Source


    'Make it so on sheet one the 72th row under column H
    'displays the total score
    Range("H72") = TotalScore

    'Color depending on the final score, depending on how 
    'line above works will change to the value in H72

    If (TotalScore < 86 And TotalScore > 69) Then
        'Find proper color for green
        Range("H70").Interior.ColorIndex = 3
    ElseIf (TotalScore < 70 And TotalScore > 44) Then
        'Find proper color for yellow
        Range("H70").Interior.ColorIndex = 2
    ElseIf (TotalScore < 45 And TotalScore > 17) Then
        'Find proper color for red
        Range("H70").Interior.ColorIndex = 1
    End If

End Sub

P.S。我的代码中应该双重缩进吗?在VBA中,它是缩进的,但在这里我手动为每一行添加了四个空格。再一次抱歉,谢谢你,因为我对这一切都是新手。

我很担心&#39; Next&#39;我的&#39; For Each&#39;:

之后的部分
    Sub Macro_Test()

        Dim TotalScore As Integer
        'Set the total score to zero
        TotalScore = 0
        Dim SrchRange As Range
        'Make a range that goes from H1 to H69
        Set SrchRange = Sheet1.Range("H2:H69")


        'Look through H to determine what word is contained and then add a value to the total score
        For Each FilledCell In SrchRange
            If (FilledCell = "Yes") Then
                TotalScore = TotalScore + 3
            ElseIf (FilledCell = "Partial") Then
                TotalScore = TotalScore + 2
            ElseIf (FilledCell = "No") Then
                TotalScore = TotalScore + 1
            End If
        Next FilledItem


        'Make it so on sheet one the 72th row under column H displays the total score
        Range("H72") = TotalScore

        'Color depending on the final score, depending on how line above works will change to the value in H70

        If (TotalScore < 86 And TotalScore > 69) Then
            'Find proper color for green
            Range("H70").Interior.Color = 5287936
        ElseIf (TotalScore < 70 And TotalScore > 44) Then
            'Find proper color for yellow
            Range("H70").Interior.Color = 65535
        ElseIf (TotalScore < 45 And TotalScore > 17) Then
            'Find proper color for red
            Range("H70").Interior.Color = 255
        End If

    End Sub

我差不多完成了。我想这样做,以便每当在列H中为是时,每个列的K都是彩色的。我可以单独制作For Each吗?或者有没有办法将它与我现有的那个相提并论?

    Sub Color_Macro()

        Dim TotalScore As Integer
        'Set the total score to zero
        TotalScore = 0
        Dim SrchRange As Range
        'Make a range that goes from H20 to H69
        Set SrchRange = Sheet1.Range("H20:H69")
        'Dim SrchRange2 As Range
        'Range for the For Each for colors
        'Set SrchRange2 = Sheet1.Range("K20:K69")


        'Look through H to determine what word is contained
        'and then add a value to the total score
        For Each FilledCell In SrchRange
            If (FilledCell = "Yes") Then
                TotalScore = TotalScore + 5
                'I am thinking of putting it in this for each
                'and from there set the R cell of the same row to green
                'so do I make a new range and implement it or what
            ElseIf (FilledCell = "Partial") Then
                TotalScore = TotalScore + 3
            ElseIf (FilledCell = "No") Then
                TotalScore = TotalScore + 1
            End If
        Next FilledCell



        'Make it so on sheet one the 70th row under
        'column H displays the total score
        Range("H70") = TotalScore

        If (TotalScore < 86 And TotalScore > 69) Then
            'Find proper color for green
            Range("K70").Interior.Color = 5287936
        ElseIf (TotalScore < 70 And TotalScore > 44) Then
            'Find proper color for yellow
            Range("K70").Interior.Color = 65535
        ElseIf (TotalScore < 45 And TotalScore > 17) Then
            'Find proper color for red
            Range("K70").Interior.Color = 255
        End 

    End Sub

我非常感谢所有的帮助!

如果其他人有这样的问题,他们想看到成品:     Sub Color_Macro()

    Dim TotalScore As Integer
    'Set the total score to zero
    TotalScore = 0
    Dim SrchRange As Range
    'Make a range that goes from H20 to H69
    Set SrchRange = Sheet1.Range("H20:H69")
    'Dim SrchRange2 As Range
    'Range for the For Each for colors
    'Set SrchRange2 = Sheet1.Range("K20:K69")


    'Look through H to determine what word is contained
    'and then add a value to the total score
    For Each FilledCell In SrchRange
        If (FilledCell = "Yes") Then
            TotalScore = TotalScore + 5
            'Offset it to go three to the
            'right and fill in a color
            FilledCell.Offset(0, 3).Interior.Color = 5287936
        ElseIf (FilledCell = "Partially") Then
            TotalScore = TotalScore + 3
            FilledCell.Offset(0, 3).Interior.Color = 65535
        ElseIf (FilledCell = "No") Then
            TotalScore = TotalScore + 1
            FilledCell.Offset(0, 3).Interior.Color = 255
        End If
    Next FilledCell


    'Make it so on sheet one the 70th row under
    'column H displays the total score
    Range("H70") = TotalScore

    If (TotalScore < 86 And TotalScore > 69) Then
        'Find proper color for green
        Range("K70").Interior.Color = 5287936
    ElseIf (TotalScore < 70 And TotalScore > 44) Then
        'Find proper color for yellow
        Range("K70").Interior.Color = 65535
    ElseIf (TotalScore < 45 And TotalScore > 17) Then
        'Find proper color for red
        Range("K70").Interior.Color = 255
    End If

End Sub

1 个答案:

答案 0 :(得分:1)

试试这个,我想我明白你要做什么,但你的解释有点模糊

Sub test()

Dim SrchRng As Range
Set SrchRng = ActiveSheet.Range("H2:H69")
Dim TotalScore As Integer
TotalScore = 0

For Each Source In SrchRng
    If Source = "yes" Then
        TotalScore = TotalScore + 3
        Source.Offset(0, 3).Interior.Color = 5287936
    ElseIf Source = "partial" Then
        TotalScore = TotalScore + 2
    ElseIf Source = "no" Then
        TotalScore = TotalScore + 1
    End If
Next Source

If (TotalScore < 86 And TotalScore > 69) Then
        Range("H70").Interior.Color = 5287936
    ElseIf (TotalScore < 70 And TotalScore > 44) Then
        Range("H70").Interior.Color = 65535
    ElseIf (TotalScore < 45 And TotalScore > 17) Then
        Range("H70").Interior.Color = 255
End If

End Sub