vba - How do I assign an Interior-Object to a Range?
问题描述
I have created an Interior variable in vba to store the style of a certain cell. Excel lets me do it but when I try to apply that style to any other cell it does not work.
Sub JustTest()
Dim X As Interior
Set X = Range("H12").Interior 'stores the Interior values into X
X.TintAndShade = -0.05 'could alter the values if needed to
Set Range("H15").Interior = X 'this line does not work
End Sub
I was expecting that I could "copy" a style from (in this example) Range H12 to Range H15 but I get a runtime error 438. Doing it this way I could alter the Interior values at runtime. I guess it's simple but I am a blockhead. Anyone an idea? Thanks in advance!
解决方案
You could use a sub to set all the (non-object) properties of interior thus achieving what you want. Instead of your Set Range("H15").Interior = X
statement you would use Call setInterior(Range("H15"),X)
.
Sub setInterior(rg As Range, rgInt As interior)
Dim rangeInt As interior
Set rangeInt = rg.interior
On Error Resume Next
With rangeInt
' .Application
.Color = rgInt.Color
.ColorIndex = rgInt.ColorIndex
' .Creator
' .Gradient
.InvertIfNegative = rgInt.InvertIfNegative
' .Parent
.Pattern = rgInt.Pattern
.PatternColor = rgInt.PatternColor
.PatternColorIndex = rgInt.PatternColorIndex
.PatternThemeColor = rgInt.PatternThemeColor
.PatternTintAndShade = rgInt.PatternTintAndShade
.ThemeColor = rgInt.ThemeColor
.TintAndShade = rgInt.TintAndShade
End With
End Sub
Object/Read only properties are commented but shown for completeness. InvertIfNegative
seems to sometimes have an error, thus the On Error Resume Next
statement to avoid problems with it.
推荐阅读
- javascript - 错误类型错误:无法读取未定义的属性“__e3_”
- android - LinearLayoutManager.findLastCompletelyVisibleItemPosition() 在滚动时跳过项目
- firebase - Firebase createUser 和 signIn 在控制台中发出警告 - react-native
- docusignapi - 文档完成时在 Docusign 中可用的触发器
- ruby - 使用 bitnami 堆栈向我的 redmine 日历添加事件功能
- angular - angular 4 post 请求不起作用,但邮递员能够将 post 请求发送到 spring boot 服务器?
- c# - 面板登录不适用于 WPF 中的实体框架
- django - django 在自己的错误页面上显示验证错误
- c# - 为什么 Form.Modal 属性没有设置器?
- java - 在 tic tac toe 应用程序中看不到获胜的动作