VBA Excel-函数被卡住

VBA Excel-函数被卡住,excel,vba,Excel,Vba,我是新的ti VBA,我想执行以下功能,希望有人能帮助我 我需要设置一个宏,该宏从单元格A2开始,当我单击我的函数时,会出现一个对话框,我可以在其中输入相关信息,并将其插入相关单元格 将数据插入3个字段(B2、C2、D2) 然后选择B3,在那里我可以再次按下我的按钮再次做同样的事情 这是到目前为止我的密码 Dim StartCell As Integer Private Sub Cancel_Click() Unload GarageDimensions End Sub Pri

我是新的ti VBA,我想执行以下功能,希望有人能帮助我

我需要设置一个宏,该宏从单元格A2开始,当我单击我的函数时,会出现一个对话框,我可以在其中输入相关信息,并将其插入相关单元格

将数据插入3个字段(B2、C2、D2)

然后选择B3,在那里我可以再次按下我的按钮再次做同样的事情

这是到目前为止我的密码

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
提前谢谢你的回答


Adam

您不需要
专用子长度框\u Change()
事件。您可以在
设计模式
UserForm\u Initialize()
事件中设置文本框
LengthBox
的最大字符数,如下所述

此外,如果您硬编码
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