VBA运行时错误'1004':Selection.Interior.Pattern [英] VBA Run-time error '1004': Selection.Interior.Pattern

查看:886
本文介绍了VBA运行时错误'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

.Pattern = xlSolid

行。任何建议?

我也尝试过

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"

第二次点击线上的按钮

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


推荐答案

这个问题的答案是我的代码执行了一个Worksheet_Change函数,该代码执行了保护t的内部代码他在代码中的某个地方。这意味着我不会对任何对象执行任何程序,例如改变其颜色。因此,在执行这些程序之前,我不得不取消对该表进行保护。

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. 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.

这篇关于VBA运行时错误'1004':Selection.Interior.Pattern的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆