简体   繁体   English

Excel VBA:基于另一个单元格的格式,获取方法错误

[英]Excel VBA: Formatting Based on The Format of Another Cell, Getting Method Error

I'm working on a spreadsheet that breaks out the tasks in a given department. 我正在处理一个电子表格,可以分解给定部门中的任务。 Each task is then assigned to a person in that department using a data validation drop down that dynamically fetches values from the tables that list each employee/role in the department. 然后,使用数据验证下拉菜单将每个任务分配给该部门的人员,该下拉菜单会动态地从列出部门中每个员工/角色的表中获取值。 The tables are on a different sheet than the task breakdown. 这些表与任务细分位于不同的表上。

Each individual needs their own background color that automatically fills in when their name is selected from the drop down on the tasks sheet. 每个人都需要自己的背景色,当从任务表的下拉列表中选择他们的名字时,这些背景色会自动填充。 What I'm hoping to do is write a macro that looks for that person's name in the table (note that I'm using named ranges) and then matches the formatting of the selected cell to their row in the table. 我希望做的是编写一个宏,在表中查找该人的名字(请注意,我使用的是命名范围),然后将所选单元格的格式与表中的行进行匹配。 I'm a super beginner with VBA and have hit the end of my abilities. 我是VBA的超级初学者,已经达到了极限。 Several answers that come close to what I want to do, but in terms of adapting it for my specific use case, I'm stuck. 几个答案与我想做的事情很接近,但是在针对我的特定用例进行调整方面,我陷入了困境。

I grabbed code from this thread, which is essentially the result I want to achieve, except that their key is on the same sheet and mine can't be: https://superuser.com/questions/472918/excel-conditionally-format-a-cell-using-the-format-of-another-content-matching 我从该线程中获取了代码,这实质上是我想要实现的结果,除了它们的密钥在同一张纸上并且我的代码不能是: https : //superuser.com/questions/472918/excel-conditionally-format -a-细胞使用最格式的-另一个内容匹配

So far, I've compiled this: 到目前为止,我已经对此进行了编译:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

If VarType(Target) > vbArray Then Exit Sub
' if multiple cells are changed at once, then exit

Dim kr1 As Range
Dim kr2 As Range
Dim KeyRange As Range
Dim TargetRange As Range
Dim lCell As Object
Dim kCell As Object

Set kr1 = Application.Range("ESFormattingRange")
Set kr2 = Application.Range("CSFormattingRange")
Set KeyRange = Application.Union(Range("kr1"), Range("kr2"))
' formatting key is here
Set TargetRange = ThisWorkbook.Worksheet("Sheet3").Range("A:X")
' changing cells in this area

For Each kCell In KeyRange.Cells
 If kCell.Value <> "" Then
  For Each lCell In TargetRange.Cells
    If lCell.Value = kCell.Value Then
    ' only change cells that match the edited cell
        lCell.Font.Color = kCell.Font.Color
        lCell.Interior.Color = kCell.Interior.Color
        ' copy whatever you feel needs to be copied
    End If
  Next
  End If
Next

End Sub

When I run this, I get the following method error, but no lines are highlighted when I try to debug: 运行此命令时,出现以下方法错误,但是在尝试调试时没有突出显示任何行:

Compile error: Method or data member not found 编译错误:找不到方法或数据成员

I'm thinking maybe there's something wrong with the way I've constructed my range variables, but I've googled everything I can think of and I'm at a loss. 我在想,也许我构造范围变量的方式出了问题,但是我已经搜寻了所有我能想到的东西,我茫然了。

Any help is greatly appreciated! 任何帮助是极大的赞赏!

The VBE should be highlighting the word Worksheet in ThisWorkbook.Worksheet("Sheet3").Range("A:X") . VBE应该在ThisWorkbook.Worksheet("Sheet3").Range("A:X")突出显示单词Worksheet

"Method or data member not found" means Worksheet isn't a member of ThisWorkbook . “未找到方法或数据成员”表示Worksheet不是ThisWorkbook的成员。

You can fix this by pressing CTRL + SPACE to autocomplete the member name to Worksheets . 您可以通过按CTRL + SPACE将成员名称自动填写为Worksheets来解决此问题。

Alternatively, you can re-type the . 或者,您可以重新输入. dereferencing operator after ThisWorkbook , and press TAB when the names list dropdown is shown, highlighting the Worksheets member - that will also autocomplete the member name to Worksheets . ThisWorkbook之后取消引用操作员,然后在显示名称列表下拉列表时按TAB键,突出显示Worksheets成员-这还将自动将成员名填写为Worksheets

Rule of thumb, if you type a . 经验法则(如果输入) . dereferencing operator and then type something that isn't in the names list, you can expect this compile error. 解引用运算符,然后键入名称列表中未包含的内容,则可能会出现此编译错误。

When you make this typo against a Variant or Object variable, the code will happily compile, but error 438 will be thrown at run-time - like this: 当您针对VariantObject变量进行错字输入时,代码会很高兴地进行编译,但是在运行时会抛出错误438-如下所示:

ThisWorkbook.Worksheets("Sheet3").Ragne("A:X") ' <~ typo compiles. Option Explicit can't save you.

The reason is that Worksheets returns an Object reference, so any member calls chained to it are resolved at run-time. 原因是Worksheets返回一个Object引用,因此链接到该Object任何成员调用都在运行时解决。 A safer way to code is to declare a local Worksheet variable to hold this object, and then work with that object instead: 一种更安全的编码方式是声明一个本地Worksheet变量来保存该对象,然后使用该对象:

Dim sourceSheet As Worksheet
Set sourceSheet = ThisWorkbook.Worksheets("Sheet3")

sourceSheet.Rnage("A:X") ' <~ typo throws at compile-time now!

If the worksheet object exists in ThisWorkbook at compile-time however, then it's probably a better idea to work with its code name instead. 但是,如果工作表对象在编译时存在于ThisWorkbook中,则最好改用其代号 Locate Sheet3 in the Project Explorer ( CTRL +R), then find its (Name) property - change it to eg SourceSheet . Project ExplorerCTRL + R)中找到Sheet3 ,然后找到其(Name)属性-将其更改为例如SourceSheet And then you can do this: 然后您可以执行以下操作:

SourceSheet.Range("A:X") ' <~ a typo wouldn't compile here.

...without needing to explicitly declare a SourceSheet . ...无需显式声明SourceSheet

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

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