简体   繁体   English

VBA运行时错误'1004':Selection.Interior.Pattern

[英]VBA Run-time error '1004': Selection.Interior.Pattern

I have the following code that should simply change the decoration of the named range based on there being a value within it. 我有以下代码,应根据其中的一个值简单地更改命名范围的修饰。 If its empty it turns red and if its not it should clear all decoration. 如果其为空,则变为红色;否则,应清除所有装饰。 I have looked at the other questions with similar errors and cannot seem to fix my issue. 我查看了其他类似错误的问题,似乎无法解决我的问题。 I have the following code: 我有以下代码:

    For Each section In mandatoryFields

    MsgBox (ThisWorkbook.Worksheets("Worksheet").Range(section).Value)
    If Trim(ThisWorkbook.Worksheets("Worksheet").Range(section).Value) = "" Then
        ThisWorkbook.Worksheets("Worksheet").Range(section).Select

    With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 255
        .TintAndShade = 0
        .PatternTintAndShade = 0
    End With

    Else
        ThisWorkbook.Worksheets("Worksheet").Range(section).Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With
    End If

Next section

I have stepped through the code and the runtime error is being triggered at the 我已逐步完成代码,并且在运行时错误被触发

.Pattern = xlNone

or 要么

.Pattern = xlSolid

lines. 线。 Any Suggestions? 有什么建议么?

I have also tried 我也尝试过

Worksheets("Worksheet").Activate
        Range(section).Select
            With Selection.Interior
                .Pattern = xlNone
                .TintAndShade = 0
                .PatternTintAndShade = 0
            End With

I have the following code which is triggered on a button click. 我有以下代码是在单击按钮时触发的。

    Dim wb As Workbook
Set wb = Application.Workbooks("C New Hire")
wb.Worksheets("Worksheet").Range("nameRange").Value = "r"

A runtime error will occur on the second time i click the button on the line 我第二次单击该行上的按钮时,将发生运行时错误

wb.Worksheets("Worksheet").Range("nameRange").Value = "r"

The Coloring part of the code is correct as this works: 代码的着色部分是正确的,因为它可以正常工作:

Sub luxation()
    Range("A1:A2").Clear
    Range("A1").Select
        With Selection.Interior
            .Pattern = xlSolid
            .PatternColorIndex = xlAutomatic
            .Color = 255
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
    Range("A2").Select
        With Selection.Interior
            .Pattern = xlNone
            .TintAndShade = 0
            .PatternTintAndShade = 0
        End With
End Sub

However: 然而:

Before Selecting a Range, you must Activate the "containing" worksheet. 选择范围之前,必须激活“包含”工作表。

Otherwise, just follow David's advice. 否则,请遵循David的建议。

EDIT#1: 编辑#1:

based on your updated code, immediately after: 根据您更新的代码,紧接在:

Range(section).Select

insert: 插入:

MsgBox Selection.Address(0,0)

This will shed some light on where the problem is happening. 这将为问题发生的位置提供一些启示。

The answer to this problem is that a Worksheet_Change function was triggered after my code which executed internal code that protected the sheet somewhere in the code. 这个问题的答案是在执行我的内部代码以保护代码中的工作表的代码之后触发了一个Worksheet_Change函数。 This meant that I coudn't carry out any procedures on any objects such as change its colour. 这意味着我不能对任何对象执行任何程序,例如更改其颜色。 Therefore I had to unprotect the sheet before carrying out these procedures. 因此,在执行这些步骤之前,我必须取消保护工作表的保护。

Simple fix in the end but easily missed. 最后简单修复,但很容易错过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM