使用另一个范围变量设置范围变量

时间:2018-09-10 12:43:18

标签: excel vba excel-vba

对此我有点麻烦,我不确定为什么...

我的代码(例如正在进行的工作)卡在此行上:

Set starRange = .Range(Cells(title), Cells(LR, 3))

我不能使用范围变量以这种方式设置新范围吗?

Sub cellPainter()

Dim ws As Worksheet
Dim starRange, titleRange, found As Range
Dim errorList() As String
Dim i, LR As Integer

i = 0
ReDim errorList(i)
errorList(i) = ""
For Each ws In ActiveWorkbook.Worksheets
    With ws
        LR = .Cells(.Rows.Count, "C").End(xlUp).Row
        Set titleRange = .Range("C4")
        If InStr(1, titleRange, "Title", vbBinaryCompare) < 1 Then
            Set found = .Range("C:C").Find("Title", LookIn:=xlValues)
            If Not found Is Nothing Then
                titleRange = found
            Else
                errorList(i) = ws.Name
                i = i + 1
                ReDim Preserve errorList(i)
            End If
        End If
        Set starRange = .Range(Cells(titleRange), Cells(LR, 3))
        For Each cell In starRange
            If InStr(1, cell, "*", vbTextCompare) > 0 Then Range(cell, cell.Offset(0, 2)).Interior.ColorIndex = 40
            If InStr(1, cell, "*", vbTextCompare) = 0 Then Range(cell, cell.Offset(0, 2)).Interior.ColorIndex = 0
        Next cell
    End With
Next ws

If errorList(0) <> "" Then
    txt = MsgBox("The following worksheets were missing the Title row, and no colour changes could be made:" & vbNewLine)
    For j = 0 To i
        txt = txt & vbCrLf & errorList(j)
    Next j
    MsgBox txt
End If

End Sub

编辑: 罗里破解了! 在Range内使用变量时,不需要Cells属性:

Set starRange = .Range(titleRange, .Cells(LR, 3))

0 个答案:

没有答案
相关问题