Excel vba - 对象'_Global'的方法'范围'失败。错误

时间:2015-12-10 06:59:46

标签: excel vba excel-vba vbscript vb6

我是excel VBA的新手,在这个项目中,如果单元格B4不为空且单元格C4为空,则应该突出显示C4。同样,代码应循环到B16单元格。 “AMA79”是工作表名称。我得到对象'_Global'的方法'范围'失败了。使用此代码时出错。能告诉我怎么做才能纠正这个问题。

Dim i As Integer
Dim rng As Range
Set rng = Range("B4:B16")

For i = 4 To 16
With ThisWorkbook.Worksheets("AMA79")
If Range(B, i).Value <> "" And Range(D, i).Value = "" Then
Range(c, i).Interior.Color = vbYellow
End If
End With
Next i

1 个答案:

答案 0 :(得分:1)

试试这个: 修改

   Sub test()

Dim i As Integer
Dim wb As Workbook
Dim ws As Worksheet

Set wb = ThisWorkbook
Set ws = wb.Sheets("AMA79")

For i = 4 To 16

If ws.Range("B" & i).Value <> "" And ws.Range("D" & i).Value = "" Then

ws.Range("C" & i).Interior.Color = vbYellow
ws.Range("B29").Value = "Please validate the cells highlighted"

End If

Next i

End Sub