【问题标题】:Excel VBA ErrorExcel VBA 错误
【发布时间】:2017-04-15 08:48:40
【问题描述】:

我正在尝试使用 VBA 自动更改包含管道字符“|”的单元格的颜色。

当检测到时,我希望代码删除管道字符“|”并将单元格颜色更改为灰色。该代码不起作用,如下所列:

    With Sheets("DATASHEET").Range("AG1:BG53")
    Set c = .Find("|", LookIn:=xlValues)
      If Not c Is Nothing Then
          firstAddress = c.Address
          Do
          c.Value = ""
          c.Pattern = xlSolid
          c.PatternColorIndex = xlAutomatic
          c.ThemeColor = xlThemeColorDark1
          c.TintAndShade = -0.249977111117893
          c.PatternTintAndShade = 0
          Loop While Not c Is Nothing And c.Address <> firstAddress
      End If
    End With

当我运行 VBA 时,我收到以下错误:

运行时错误“91”: 对象变量或未设置块变量

这里的代码失败了:

循环 While Not c Is Nothing And c.Address firstAddress

崩溃后,我的调试手表具有以下值:

表达式firstAddress" = $AG$37(这是范围内第一个合并并居中的单元格 - 要求)

表达式 c.Address = 对象变量或未设置块变量

表达式 c.Value = 对象变量或未设置块变量

我将不胜感激。

谢谢!

jmseiver

更新:

此代码有效,谢谢 Darren!

With Sheets("DATASHEET").Range("AG1:BG53")
            Set c = .Find("|", LookIn:=xlValues)
            If Not c Is Nothing Then
                Do
                c.Value = ""
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.249977111117893
                    .PatternTintAndShade = 0
                End With
                Set c = .FindNext(c)
                Loop While Not c Is Nothing
            End If
        End With

这段代码没有:

With Sheets("DATASHEET").Range("AG1:BG53")
            Set c = .Find("|", LookIn:=xlValues)
            If Not c Is Nothing Then
                Do
                c.Value = ""
                With Selection.Interior
                    .Pattern = xlSolid
                    .PatternColorIndex = xlAutomatic
                    .ThemeColor = xlThemeColorDark1
                    .TintAndShade = -0.249977111117893
                    .PatternTintAndShade = 0
                End With
                Set c = .FindNext(c)
                Loop While Not c Is Nothing
            End If
        End With

更改单元格颜色是例程的目的。附加的 With - End With 不起作用。

它不会爆炸,只是不会改变单元格的颜色。

?

感谢大家到目前为止的时间!

jmseiver

更新 2:

此代码有效!

'color any cell with updated data to gray
        With Sheets("DATASHEET").Range("AG1:BG53")
            Set c = .Find("|", LookIn:=xlValues)
            If Not c Is Nothing Then
                Do
                c.Replace What:="|", Replacement:=""
                c.Interior.ColorIndex = 15
                Set c = .FindNext(c)
                Loop While Not c Is Nothing
            End If
        End With

总而言之,这段代码的目的是 1) 找到第一个字符是管道字符“|”的所有单元格,2) 删除管道字符“|”,以及 3) 将单元格着色为灰色。

再次感谢达伦和约翰!

jmseiver

【问题讨论】:

    标签: excel vba with-statement


    【解决方案1】:

    您必须测试c 是否在不同时(尽管隐含地)假设它不是什么的情况下什么都不是。像这样的东西(未经测试):

    With Sheets("DATASHEET").Range("AG1:BG53")
        Set c = .Find("|", LookIn:=xlValues)
        If Not c Is Nothing Then
           firstAddress = c.Address
           Do
              c.Value = ""
              Set c = .FindNext(c)
              If c is Nothing Then Exit Do
           Loop While c.Address <> firstAddress
         End If
    End With
    

    问题在于,与大多数编程语言不同,VBA 不会短路逻辑运算符。 AB总是A and B 中进行评估,即使 A 为 False。因此,您有时需要在 VBA 中以更迂回的方式编写代码。

    【讨论】:

    • 您可以使用原始代码并删除处理firstAddress的代码。一旦所有 | 被替换,代码将循环直到 c 变为空。
    • @DarrenBartrup-Cook 好点。老实说,我并没有过多关注代码试图做什么,而是专注于他们遇到的特定错误。也许您可以发布一个答案,说明如何简化代码。
    • 全部,见第二个问题更新。此颂奏效。非常感谢达伦和约翰!
    【解决方案2】:

    可以用Excel Find and Replace Format简化:

    Application.ReplaceFormat.Clear                      ' optional
    Application.ReplaceFormat.Interior.ColorIndex = 15   ' set the replacement format
    [DATASHEET!AG1:BG53].Replace "|", "", LookAt:=xlPart, ReplaceFormat:=True
    

    但这就像您更新的代码一样,将替换所有包含 | 的单元格。

    要仅查找 | 开头的单元格,您可以将代码中的查找条件替换为:

    Set c = .Find("|*", , LookIn:=xlValues, LookAt:=xlWhole)
    

    【讨论】:

      猜你喜欢
      • 2015-06-13
      • 1970-01-01
      • 1970-01-01
      • 2022-12-14
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多