VBA语法/代码检查

我被要求做一个Excelmacros来根据三个单词来确定点,然后根据点的范围对框进行着色,以便可以快速确定可以工作的内容。 我是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 

PS我应该在我的代码中双重缩进? 在VBA是缩进,但在这里我手动添加四个空格到每一行。 再次抱歉,谢谢你,因为我是新手。

我对“For Each”之后的“Next”部分感到困惑:

  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 

试试看,我想我明白你在干什么,但是你的解释有些模糊

 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