在用户表单

时间:2015-09-23 02:46:08

标签: vba excel-vba combobox userform excel

嗨,目前我的用户表单看起来像这样添加新项目: enter image description here

在组合框中,用户可以选择“大项目”,“中项目”和“小项目”大小。每个项目大小由下表中的特定范围组成: enter image description here

用户可以将项目详细信息添加到用户表单中,并为项目大小选择组合框下拉列表,新项目详细信息将添加到每个特定项目大小下的空行中。不幸的是我不确定如何在组合框下拉列表中的每个项目大小的范围内做到这一点,以便当用户已经选择项目大小并单击添加命令按钮时,新项目详细信息将被添加到在所选项目大小上找到的新行。另外,我对下面显示的代码中的“应用程序定义或对象定义错误”的命令添加按钮有一些问题,并且不确定代码是否有助于添加到特定项目大小的lastrow中。我很想知道如何为组合框和命令添加按钮做什么。

Private Sub CommandAddButton1_Click()

lastrow = Sheets("Program status summary").Range("B").End(xlDown).Row 'shows the above mention error'

    Cells(lastrow + 1, "B").Value = TextBoxProjCode.Text
    Cells(lastrow + 1, "C").Value = TextBoxProjName.Text
    Cells(lastrow + 1, "D").Value = TextBoxSector.Text
    Cells(lastrow + 1, "E").Value = TextBoxObjective.Text
    Cells(lastrow + 1, "H").Value = TextBoxProjSponsor.Text
    Cells(lastrow + 1, "G").Value = TextBoxProjSponsorNew.Text
    Cells(lastrow + 1, "F").Value = TextBoxProjM.Text
    Cells(lastrow + 1, "T").Value = TextBoxRegulatory.Text
    Cells(lastrow + 1, "N").Value = TextBoxRiskLvl.Text
    Cells(lastrow + 1, "M").Value = TextBoxDatePar.Text
    Cells(lastrow + 1, "J").Value = TextBoxCostPar.Text
    Cells(lastrow + 1, "O").Value = TextBoxAffectCust.Text
    Cells(lastrow + 1, "Q").Value = TextBoxCustNonRetail.Text
    Cells(lastrow + 1, "P").Value = TextBoxCustRetail.Text
    Cells(lastrow + 1, "S").Value = TextBoxOutsourcingImp.Text
    Cells(lastrow + 1, "R").Value = TextBoxKeyStatus.Text
    Cells(lastrow + 1, "K").Value = TextBoxSchStart.Text
    Cells(lastrow + 1, "L").Value = TextBoxSchEnd.Text
    Cells(lastrow + 1, "V").Value = TextBoxRagSchedule.Text
    Cells(lastrow + 1, "U").Value = TextBoxRagFinancial.Text
    Cells(lastrow + 1, "W").Value = TextBoxRagBenefit.Text
    Cells(lastrow + 1, "I").Value = TextBoxCost.Text

Unload AddProject

End Sub

我真的希望任何人都可以帮助我使用项目大小的组合框插入每个项目大小的范围和添加命令按钮上的错误。任何帮助,将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

Private Sub CommandAddButton1_Click()
  Dim sh As Worksheet: Set sh = ThisWorkbook.Sheets("Program status summary")
  Dim emptyRow As Integer: emptyRow = 1 + sh.UsedRange.Find(ComboBoxProjSizes.Text).End(xlDown).row

  With sh
    .Cells(emptyRow, "A").Value = 1 + Application.Max(.Columns(1)) ' to generate a new identifier in column 1

    .Cells(emptyRow, "B").Value = TextBoxProjCode.Text
    .Cells(emptyRow, "C").Value = TextBoxProjName.Text
    .Cells(emptyRow, "D").Value = TextBoxSector.Text
    .Cells(emptyRow, "E").Value = TextBoxObjective.Text
    .Cells(emptyRow, "H").Value = TextBoxProjSponsor.Text
    .Cells(emptyRow, "G").Value = TextBoxProjSponsorNew.Text
    .Cells(emptyRow, "F").Value = TextBoxProjM.Text
    .Cells(emptyRow, "T").Value = TextBoxRegulatory.Text
    .Cells(emptyRow, "N").Value = TextBoxRiskLvl.Text
    .Cells(emptyRow, "M").Value = TextBoxDatePar.Text
    .Cells(emptyRow, "J").Value = TextBoxCostPar.Text
    .Cells(emptyRow, "O").Value = TextBoxAffectCust.Text
    .Cells(emptyRow, "Q").Value = TextBoxCustNonRetail.Text
    .Cells(emptyRow, "P").Value = TextBoxCustRetail.Text
    .Cells(emptyRow, "S").Value = TextBoxOutsourcingImp.Text
    .Cells(emptyRow, "R").Value = TextBoxKeyStatus.Text
    .Cells(emptyRow, "K").Value = TextBoxSchStart.Text
    .Cells(emptyRow, "L").Value = TextBoxSchEnd.Text
    .Cells(emptyRow, "V").Value = TextBoxRagSchedule.Text
    .Cells(emptyRow, "U").Value = TextBoxRagFinancial.Text
    .Cells(emptyRow, "W").Value = TextBoxRagBenefit.Text
    .Cells(emptyRow, "I").Value = TextBoxCost.Text
    .Rows(emptyRow+1).Insert
  End With
  Unload AddProject
End Sub