首页 > 解决方案 > 在用户表单标签中使用活动单元格值

问题描述

如何在用户窗体的标签中使用 target.value?无论我尝试了什么,我都会遇到错误。(运行时“438”:对象不支持属性或方法)

我希望用户窗体在更改单元格编号时显示:

我的工作表代码:

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range


    Set KeyCells = Range("D8:D12")

If Not Application.Intersect(KeyCells, Target) Is Nothing Then

        UserForm1.Show

End If
End Sub

我的用户窗体代码按照我想象的方式工作(但它不起作用):

Private Sub UserForm_Initialize()

Label1.Caption = Worksheets("Personal Barrier").Target.Offset(0, -1).Value

End Sub

标签: excelvba

解决方案


对象中没有Target属性Worksheet。您可以在方法中设置标签Worksheet_Change

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim KeyCells As Range

    Set KeyCells = Range("D8:D12")

    If Not Application.Intersect(KeyCells, Target) Is Nothing Then
        UserForm1.Label1.Caption = Target.Offset(0, -1).Value
        UserForm1.Show

    End If
End Sub

推荐阅读