2012-04-27 73 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