2015-11-07 72 views
0

我是VBA的新手,並創建了一個帶3個命令按鈕的多頁面用戶窗體,其中一個是將頁面中的數據添加到xl中的一個表單中。當我第一次完成它時,它工作得非常好,所以我保存了所有內容,關閉了工作簿,當我重新打開它時,add命令按鈕不起作用。我又重新開始了整個項目,又一次發生了同樣的事情。用戶表單中的命令按鈕停止工作

任何人都可以請指教?現在我已經花了兩天時間試圖解決這個

的代碼是:

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 

非常感謝/史蒂夫

+0

你能說清楚你的意思是行不通的嗎?是否拋出錯誤? – Gareth

+1

這看起來可能太明顯了,但是當你保存它時,你是否保存爲Excel工作簿(例如XLSX)? XLSX不能包含代碼,並且您需要代碼才能使其工作。如果是這種情況,請將其保存爲啓用宏的工作簿(XLSM)或二進制工作簿(XLSB)。順便說一句,如果你這樣做了,你應該收到一個警告,說明保存爲XLSX會從工作簿中剝離代碼。 – Jeeped

+0

嗨Gareth,感謝您回覆 – Steve

回答

0

嘗試在你的代碼的末尾添加Msgbox "I Worked",看它是否執行或不。

然後,如果是這樣,也許你沒有引用正確的數據,也許你的sheet1不再是你的sheet1了。它更推薦使用

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

如果代碼沒有執行,那麼你就必須檢查宏仍然分配給該按鈕或者如果您的Excel是防止被執行的宏。

+0

嗨Graham,你是對的Sheet1已經損壞,命名爲真的非常感謝你的專家幫助,太棒了Steve – Steve

+0

非常感謝Fnayou,它絕對是Sheet1的變化,真的很感謝你的意見,謝謝。 – Steve