2012-04-27 199 views
2

我新的TI VBA,我想如下我希望有人可以幫助我VBA的Excel - 功能滯留

我需要設置開始在單元格A2宏當我點擊我的函數來執行功能出現一個對話框,我可以輸入相關信息插入並將其插入到相關細胞

將數據插入到3個字段(B2,C2,D2)

然後選擇B3其中i可以再次按我的按鈕做同樣變薄再次

我的繼承人合作德到目前爲止

Dim StartCell As Integer 

Private Sub Cancel_Click() 
    Unload GarageDimensions 

End Sub 

Private Sub LengthBox_Change() 

If LengthBox.Value >= 15 Then 
    MsgBox "Are you sure? You do realise it is just a garage!" 
    Exit Sub 
End If 

End Sub 
Private Sub Submit_Click() 
'This code tells the text entered into the job reference textbox to be inserted _ 
into the first cell in the job reference column. 

StartCell = Cells(1, 2) 

Sheets("Data").Activate 
If IsBlankStartCell Then 


    ActiveCell(1, 1) = JobRef.Text 
    ActiveCell.Offset(0, 1).Select 

ActiveCell(1, 1) = LengthBox.Value 
ActiveCell.Offset(0, 1).Select 

    ActiveCell(1, 1) = ListBox1.Value 
    ActiveCell.Offset(0, 1).Select 


    ActiveCell(1, 1) = ListBox1.Value * LengthBox.Value 

Else 
    Range("A1").End(xlDown).Offset(1, 0).Select 

End If 


Unload GarageDimensions 

End Sub 
Private Sub UserForm_Initialize() 

With ListBox1 
    .AddItem "2.2" 
    .AddItem "2.8" 
    .AddItem "3.4" 
End With 

    ListBox1.ListIndex = 0 

End Sub 

謝謝您的回答提前

亞當

回答

2

你不需要Private Sub LengthBox_Change()事件。您可以在Design ModeUserForm_Initialize()事件作爲我在下面做了設置TextBox LengthBox的MAX字符。

此外,如果您對Startcell進行硬編碼,則每次運行UserForm時,數據都將從A2開始,如果有數據,則將被覆蓋。請嘗試查找可寫入的最後一行。

順便說一句,這是你正在嘗試(未測試)?

Option Explicit 

Dim StartCell As Integer 
Dim ws As Worksheet 

Private Sub UserForm_Initialize() 
    Set ws = Sheets("Data") 

    With ListBox1 
     .AddItem "2.2" 
     .AddItem "2.8" 
     .AddItem "3.4" 
     .ListIndex = 0 
    End With 

    LengthBox.MaxLength = 14 
End Sub 

Private Sub Submit_Click() 
    With ws 
     '~~> Find the first empty row to write 
     StartCell = .Range("A" & Rows.Count).End(xlUp).Row + 1 

     .Range("A" & StartCell).Value = Val(Trim(ListBox1.Value)) _ 
     * Val(Trim(LengthBox.Value)) 

     .Range("B" & StartCell).Value = JobRef.Text 
     .Range("C" & StartCell).Value = LengthBox.Value 
     .Range("D" & StartCell).Value = ListBox1.Value 
    End With 

    Unload Me 
End Sub 

Private Sub Cancel_Click() 
    Set ws = Nothing 
    Unload Me 
End Sub 
+0

謝謝你的忠告 – 2012-04-28 10:39:25

+0

不用擔心:)只是讓我們知道,如果代碼工作或沒有? – 2012-04-28 11:18:54