【问题标题】:Range of object failed对象范围失败
【发布时间】:2016-07-07 21:54:39
【问题描述】:

我想根据多个条件和单元格的选择在一系列单元格中填充颜色。这是代码

Sub color()



  Dim j As Integer
  Dim testfallname As String
  Dim rng As Range
  Dim rCell As Range
  Dim UnionRange As Range
  Dim wb As Workbook
  Dim ws As Worksheet

   Set wb = ThisWorkbook
   Set ws = wb.Sheets("1-BR_Vorschlag")

  ws.Activate
  For j = 7 To 1000
If ws.Cells(1, j) = "ARB11" Or ws.Cells(1, j) = "FVB1" Or ws.Cells(1, j) = "FVB4E" Then
testfallname = Cells(5, j)
Set rng = ws.Range("G5:AQ5").Find(testfallname)
End If

   Set UnionRange = Union(ws.Range(Cells(34, rng.Column), ws.Range(Cells(39, rng.Column), ws.Range(Cells(49, rng.Column), Cells(50, rng.Column), ws.Range(Cells(53, rng.Column), Cells(54, rng.Column), ws.Range(Cells(59, rng.Column), Cells(61, rng.Column), ws.Range(Cells(66, rng.Column), Cells(77, rng.Column), ws.Range(Cells(85, rng.Column), Cells(97, rng.Column)))))))))


With ws
  For Each rCell In UnionRange
        If rCell.Value = vbNullString Then
            rCell.Interior.color = 8421504
       End If
    Next rCell
End With
Next j

这是实际代码。现在我再次收到一个错误,说错误数量的争论或无效的属性分配。它调试联合范围线。我哪里错了?

【问题讨论】:

  • 附带说明,您不需要With,您应该使用条件格式根据值对单元格着色。
  • 哪一行给出了错误?而且我很确定你必须使用ColorIndex 而不是Color
  • 我在 Set UnionRange = Sheets("1-BR_Vorschlag").Union(Range(Cells(34, 7)), Range(Cells(39, 7))) 行得到错误。我已经在具有相同工作表名称的新工作簿中的空白工作表上进行了尝试。它给了我同样的错误。

标签: excel vba


【解决方案1】:

你把目标表放错了位置。你像这样使用它(这些值是随机的,为了一个例子):

Set UnionRange = Union(sheets("1-BR_Vorschlag").Range(cells(3, 10), cells(8, 9)), sheets("1-BR_Vorschlag").Range(cells(13, 22), cells(28, 49))) 

基本上只需将目标移动到 union 内,它应该可以正常工作。我认为它以这种方式工作,因此您可以同时在不同工作表的范围上使用联合。

【讨论】:

  • 嗨,我刚刚使用了你的想法,我有一些这样的东西 Set UnionRange = Union(ws.Range(Cells(50, 10), (Cells(60, 10)), ws.Range( Cells(75, 10), (Cells(90, 10))))),现在它给了我错误的争论数量或无效的属性分配。
  • 你在“cells(60,10)”前面放了一个括号,但没有关闭它,所以它搞砸了。您实际上不需要范围内第二个“单元格”周围的额外括号,我在下面的内容将起作用Set UnionRange = Union(ws.Range(cells(50, 10), cells(60, 10)), ws.Range(cells(75, 10), cells(90, 10))) 如果可行,请单击复选标记!
  • 我又遇到了同样的问题。我已经在问题中更新了它
  • 您缺少括号。它应该是这样的格式:union(Range1,Range2...) 和范围应该是这样的格式:target.range(cells1,cells2) 所以一起应该是这样的:union(range1(cells1,cells2),range2(cells3,cells4)) 你有它作为 range(cells1,range(cells2,cells3)) 这是不正确的语法。跨度>
  • 我已经根据您指定的语法设置了它 Set UnionRange = Union(ws.Range(Cells(34, rng.Column), Cells(35, rng.Column), ws.Range (Cells(39, rng.Column), Cells(40, rng.Column)))) 但我仍然遇到同样的错误。如果语法错误,请纠正我
【解决方案2】:

您可以使用SpecialCells(xlCellTypeBlanks) 一次选择所有空白单元格。

Sub color()

    Dim r As Range
    With Worksheets("1-BR_Vorschlag")
        Set r = Union(.Cells(34, 7), .Cells(39, 7), .Cells(49, 7), .Cells(50, 7), .Range(.Cells(53, 7), .Cells(54, 7)), .Range(.Cells(59, 7), .Cells(61, 7)), .Range(.Cells(66, 7), .Cells(77, 7)), .Range(.Cells(85, 7), .Cells(97, 7)))
        Set r = r.SpecialCells(xlCellTypeBlanks)
        If Not r Is Nothing Then r.Interior.color = 8421504
    End With

End Sub

我发现使用 Union 方法有点乏味,我更喜欢创建一个字符串并使用 Range 方法。

Sub Color2()

    Dim r As Range
    Set r = Worksheets("1-BR_Vorschlag").Range("$G$34,$G$39,$G$49:$G$50,$G$53:$G$54,$G$59:$G$61,$G$66:$G$77,$G$85:$G$97")
    Set r = r.SpecialCells(xlCellTypeBlanks)
    If Not r Is Nothing Then r.Interior.color = 8421504

End Sub

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 2013-04-09
    • 2021-11-14
    相关资源
    最近更新 更多