我有一个string,其中我使用一个简单的INDEX-MATCH公式从一个下拉列表中选择一个micro变量。INDEX-MATCH列在Sheet1中,数据验证列表在Sheet2中。运行微将导致错误Object doesn't support this property or method,并突出显示行cel2.Offset(0, 2).Validate = coresVal。
Sheet2有一个Private Sub Worksheet_Change(___)作为Sub,只有选中的单元格才会触发Sheet2中的更改,如果这可能会导致错误,我不知道。我是不是对下面的代码做错了什么?我尝试查看coresVal和INDEX-MATCH公式这两个变量是否属于同一类型,结果显示它们都是strings。我收到此错误的原因是什么?
Option Explicit
'using a constant to store the highlight color...
Const HIGHLIGHT_COLOR = 9894500 'RGB(100, 250, 150)'Is a cell highlighted?
EDIT: changed the function name to IsHighlighted
Sub Asign_Bided()
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim cel1 As Range
Dim cel2 As Range
Dim Bid As Range
Dim line As Range
Dim OffEmp As Range
Dim BidL8 As Range
Dim BidL8E As Range
Dim coresVal As String
'This sheet has the table from which INDEX-MATCH should be pulling the employee name
Set ws1 = Worksheets("Sheet1")
'This is the sheet where the name should be selected from Data Validation drop-down
'and it also has a "Private Sub Worksheet_Change(ByVal Target AS Range)"
Set ws2 = Worksheets("Sheet2")
'Range("Sheet2!B12:B40,Sheet2!B43:B58,Sheet2!B61:B77,Sheet2!B81:B97,Sheet2!B101:B117)
Set line = ws2.Range("All_Pos_Hilight_Mon")
Set OffEmp = ws2.Range("$B$151:$B$210")
'Below Ranges are in a Table with two columns Employee and Position in Sheet1;
Set BidL8 = ws1.Range("Bided_Pos_T[Bided_Prep_Position]")
Set BidL8E = ws1.Range("Bided_Pos_T[Employee]")
ws2.Activate
For Each cel2 In line
If IsHighlighted(cel2) Then
For Each cel1 In BidL8E
If Application.WorksheetFunction.CountIf(OffEmp, cel1.Value) > 0 Then
Else: coresVal = "=INDEX(Bided_Pos_T[Employee],MATCH(Butter_8_Prep_Mon,Bided_Pos_T[Bided_Prep_Position],0))"
Debug.Print coresVal
cel2.Offset(0, 2).Validate = coresVal
End If
Next cel1
End If
Next cel2
End Sub
Function IsHighlighted(c As Range)
'Function to check if that particular cell is highlighted
IsHighlighted = (c.Interior.Color = HIGHLIGHT_COLOR)
End Function这里的预期结果是,表中雇员的姓名应该存储在coresVal中,在cel2为offset之后,它应该在Sheet2中Validate该姓名。我也尝试过使用Evaluate函数。
示例:coresVal = Evaluate ("INDEX (" & BidL8E.Address &",MATCH(cel2.Value, " & BidL8.Address &"))")
结果是,它拍摄了错误类型不匹配,并突出显示了评估行。对此有什么解决方案吗?提前谢谢。
发布于 2019-01-14 08:56:18
不太确定cel2.Offset(0, 2).Validate = coresVal应该是什么,也许你指的是cel2.Offset(0, 2).Value = coresVal?
无论如何,您的Evaluate公式字符串中有一些语法错误。
为了设置表(ListObject)中的列,我更愿意使用如下代码中的对象设置:
Dim Tbl As ListObject
Set Tbl = ws1.ListObjects("Bided_Pos_T")
Set BidL8 = Tbl.ListColumns("Bided_Prep_Position").DataBodyRange
Set BidL8E = Tbl.ListColumns("Employee").DataBodyRange然后,对于您的Evaluate行,您需要在以下字符串中使用行:
coresVal = Evaluate("INDEX(" & BidL8E.Address(0, 0, xlA1, xlExternal) & _
",MATCH(" & cel2.Value & "," & BidL8.Address(0, 0, xlA1, xlExternal) & ",0))")由于您的两个范围BidL8E和BidL8不在"Sheet1"中,而cel2范围循环通过line在"Sheet1"中,因此您需要指定Range.Address函数的第四个参数,请参见LINK
然而,您还需要设置一个错误处理方案,以防Index中的MATCH部分失败。
https://stackoverflow.com/questions/54174315
复制相似问题