userform中的命令按钮停止工作

时间:2015-11-07 23:03:30

标签: excel vba command excel-2010 userform

我是VBA的新手,并创建了一个带有3个命令按钮的多用户表单,一个是将页面中的数据添加到xl中的工作表中。当我第一次完成它时,它工作得非常好,所以我保存了所有内容,关闭了工作簿,当我重新打开它时,添加命令按钮不起作用。我重新启动整个项目,完全相同的事情再次发生。

任何人都可以提出建议吗?我现在花了两天时间试图解决这个问题

代码是:

Private Sub cmdAdd_Click()
    Dim addme As Range
    Dim s As Integer
    Dim t As Integer
    Dim u As Integer
    Dim v As Integer

    Set addme = Sheet1.Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

For s = 0 To Me.ListRegularOrder.ListCount - 1
        If Me.ListRegularOrder.Selected(s) Then
            addme = Me.ListRegularOrder.List(s)
            addme.Offset(0, 1) = Me.ListRegularOrder.List(s, 1)
            addme.Offset(0, 2) = Me.ListRegularOrder.List(s, 2)
            addme.Offset(0, 3) = Me.ListRegularOrder.List(s, 3)
            addme.Offset(0, 4) = Me.ListRegularOrder.List(s, 4)
            addme.Offset(0, 5) = Me.ListRegularOrder.List(s, 5)
            addme.Offset(0, 6) = Me.ListRegularOrder.List(s, 6)
            addme.Offset(0, 7) = Me.ListRegularOrder.List(s, 7)
            addme.Offset(0, 8) = Me.ListRegularOrder.List(s, 8)
            addme.Offset(0, 9) = Me.ListRegularOrder.List(s, 9)
            addme.Offset(0, 10) = Me.ListRegularOrder.List(s, 10)
            addme.Offset(0, 11) = Me.ListRegularOrder.List(s, 11)
            addme.Offset(0, 12) = Me.ListRegularOrder.List(s, 12)
            addme.Offset(0, 13) = Me.ListRegularOrder.List(s, 13)
            addme.Offset(0, 14) = Me.ListRegularOrder.List(s, 14)
            addme.Offset(0, 15) = Me.ListRegularOrder.List(s, 15)
            addme.Offset(0, 16) = Me.ListRegularOrder.List(s, 16)

            Set addme = addme.Offset(1, 0)
        End If
    Next s
    For s = 0 To Me.ListRegularOrder.ListCount - 1
        If Me.ListRegularOrder.Selected(s) Then Me.ListRegularOrder.Selected(s) = False

    Next s

For t = 0 To Me.ListLastOrder.ListCount - 1
        If Me.ListLastOrder.Selected(t) Then
            addme = Me.ListLastOrder.List(t)
            addme.Offset(0, 1) = Me.ListLastOrder.List(t, 1)
            addme.Offset(0, 2) = Me.ListLastOrder.List(t, 2)
            addme.Offset(0, 3) = Me.ListLastOrder.List(t, 3)
            addme.Offset(0, 4) = Me.ListLastOrder.List(t, 4)
            addme.Offset(0, 5) = Me.ListLastOrder.List(t, 5)
            addme.Offset(0, 6) = Me.ListLastOrder.List(t, 6)
            addme.Offset(0, 7) = Me.ListLastOrder.List(t, 7)
            addme.Offset(0, 8) = Me.ListLastOrder.List(t, 8)
            addme.Offset(0, 9) = Me.ListLastOrder.List(t, 9)

            Set addme = addme.Offset(1, 0)
        End If
    Next t
    For t = 0 To Me.ListLastOrder.ListCount - 1
        If Me.ListLastOrder.Selected(t) Then Me.ListLastOrder.Selected(t) = False

    Next t


    For u = 0 To Me.ListHistory.ListCount - 1
        If Me.ListHistory.Selected(u) Then
            addme = Me.ListHistory.List(u)
            addme.Offset(0, 1) = Me.ListHistory.List(u, 1)
            addme.Offset(0, 2) = Me.ListHistory.List(u, 2)
            addme.Offset(0, 3) = Me.ListHistory.List(u, 3)
            addme.Offset(0, 4) = Me.ListHistory.List(u, 4)
            addme.Offset(0, 5) = Me.ListHistory.List(u, 5)
            addme.Offset(0, 6) = Me.ListHistory.List(u, 6)
            addme.Offset(0, 7) = Me.ListHistory.List(u, 7)
            addme.Offset(0, 8) = Me.ListHistory.List(u, 8)
            addme.Offset(0, 9) = Me.ListHistory.List(u, 9)

            Set addme = addme.Offset(1, 0)
        End If
    Next u
    For u = 0 To Me.ListHistory.ListCount - 1
        If Me.ListHistory.Selected(u) Then Me.ListHistory.Selected(u) = False

    Next u


    For v = 0 To Me.ListPriceList.ListCount - 1
        If Me.ListPriceList.Selected(v) Then
            addme = Me.ListPriceList.List(v)
            addme.Offset(0, 1) = Me.ListPriceList.List(v, 1)
            addme.Offset(0, 2) = Me.ListPriceList.List(v, 2)
            addme.Offset(0, 3) = Me.ListPriceList.List(v, 3)
            addme.Offset(0, 4) = Me.ListPriceList.List(v, 4)
            addme.Offset(0, 5) = Me.ListPriceList.List(v, 5)
            addme.Offset(0, 6) = Me.ListPriceList.List(v, 6)
            addme.Offset(0, 7) = Me.ListPriceList.List(v, 7)
            addme.Offset(0, 8) = Me.ListPriceList.List(v, 8)
            addme.Offset(0, 9) = Me.ListPriceList.List(v, 9)
            addme.Offset(0, 10) = Me.ListPriceList.List(v, 10)
            addme.Offset(0, 11) = Me.ListPriceList.List(v, 11)
            addme.Offset(0, 12) = Me.ListPriceList.List(v, 12)
            addme.Offset(0, 13) = Me.ListPriceList.List(v, 13)
            addme.Offset(0, 14) = Me.ListPriceList.List(v, 14)
            addme.Offset(0, 15) = Me.ListPriceList.List(v, 15)

            Set addme = addme.Offset(1, 0)
        End If
    Next v
    For v = 0 To Me.ListPriceList.ListCount - 1
        If Me.ListPriceList.Selected(v) Then Me.ListPriceList.Selected(v) = False
    Next v

End Sub

非常感谢/史蒂夫

1 个答案:

答案 0 :(得分:0)

尝试在代码末尾添加Msgbox "I Worked"以查看代码是否执行。

然后,如果确实如此,也许你没有引用正确的数据,也许你的sheet1不再是你的sheet1了。它更推荐使用

    Set addme = Sheets("NAME_SHEET").Cells(Rows.Count, 3).End(xlUp).Offset(1, 0)

如果代码没有执行,那么你必须检查宏是否仍然分配给按钮,或者你的excel是否阻止执行宏。