“<application-defined or =”“object-defined =”“error =”“>错误1004”

时间:2017-03-21 20:27:14

标签: excel vba excel-vba

我有一个简单的for-next来填充用户表单上的一些标签,但是无法弄清楚为什么不断获得“错误1004”当我输入Contr.Caption时,.Caption函数丢失了?

Dim Control_Name As String
Dim DSlot As Long
Dim DLName As Long
Dim Contr As Control

With Worksheets("apartments").Range("depositslot")
    DSlot = .Find("Slot").Row + 1
    DLName = .Find("Last").Column
End With

For Each Contr In Layout.Controls
    If TypeName(Contr) = "Label" Then
        Control_Name = Mid(Contr.Name, 1, 6)
            If Control_Name = "LBName" Then
                Contr.Caption = Worksheets("apartments").Range(DSlot, DLName).Value
                DSlot = DSlot + 1
            End If
    End If
Next

2 个答案:

答案 0 :(得分:5)

你的话说:

Contr.Caption = Worksheets("apartments").Range(DSlot, DLName).Value

应该是

Contr.Caption = Worksheets("apartments").Cells(DSlot, DLName).Value

Range属性不接受Long类型的两个参数。

答案 1 :(得分:2)

尝试更改&#34;范围&#34;对象由#34; Cells&#34;,因为你有&#34;行,列&#34;的引用。你应该有这样的东西:

Contr.Caption = Worksheets("apartments").Cells(DSlot, DLName).Value