excel - VBA Excel - 函数卡住

标签 excel vba

我是 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

感谢您提前的答复

亚当

最佳答案

您不需要 Private Sub LengthBox_Change() 事件。您可以在设计模式UserForm_Initialize()事件中设置文本框LengthBox的最大字符数,就像我在下面所做的那样。

此外,如果您对 Startcell 进行硬编码,那么每次运行用户窗体时,数据将从 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

关于excel - VBA Excel - 函数卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10357215/

相关文章:

vba - 如何将数据透视表列中的所有值除以常数

VBA错误的数字参数用户表单代码,传输变量?

vba - VBComponent 通过选项卡名称引用工作表

javascript - 使用 javascript 在用户计算机上启动本地文件?

excel - 将宏分配给在运行时 VBA 中创建的按钮

excel - 如何使用 VBA for Excel 中的查找和循环来识别、删除和插入大于 6 的值的空白行?

vba - 为什么我的 VBA 代码抛出 "Invalid outside procedure"错误?

excel - 范围内的替代行颜色

excel - 如何像 Try/Catch 一样内联错误处理 block

C# (Microsoft.Office.Interop.Excel),将数据另存为 Excel 2003 文件 (xls)