首页 文章

列表框代码的VBA错误

提问于
浏览
0

我正在尝试更正我创建的代码的VBA错误,以将列表框值显示到Excel中 . 有人可以帮忙吗?

Dim lbValue As Long
 lbValue = Summary.ListBoxes("BusinessOwnerListBox").Value

 Dim lbtext As String
 With Summary.ListBoxes("BusinessOwnerListBox").ControlFormat
    lbtext = .List(.Value)
 End With

列表是多选单,并使用添加项添加项目 . 项目显示在表单中,但所选的列表值在运行代码时不会显示在电子表格中 .

完整代码

Private Sub CancelButton_Click()

 Unload Me

End Sub

Private Sub ClearButton_Click()

Call UserForm_Initialize

End Sub

Private Sub InitialTermListBox_Click()

End Sub


Private Sub OKButton_Click()
Dim emptyRow As Long

'Make Summary active
 Summary.Activate

 'Determine emptyRow
 emptyRow = WorksheetFunction.CountA(Range("A:A")) + 1

'Transfer information
 Cells(emptyRow, 1).Value = SupplierNameTextBox.Value
 Cells(emptyRow, 3).Value = GeneralDescriptionTextBox.Value
 Cells(emptyRow, 5).Value = DepartmentListBox.Value
 Cells(emptyRow, 6).Value = ContractStartDateTextBox.Value
 Cells(emptyRow, 7).Value = InitialTermListBox.Value
 Cells(emptyRow, 8).Value = RenewalTermListBox.Value
 Cells(emptyRow, 9).Value = PaymentTermsListBox.Value
 Cells(emptyRow, 10).Value = SelectionMechanismListBox.Value
 Cells(emptyRow, 11).Value = ValueOfContractTextBox.Value

 Dim lbtext As Variant
 lbtext = BusinessOwnerListBox.Value
 Worksheets("Summary").Cells(emptyRow, 4).Value = lbtext


If SignedContractCheckBox.Value = True Then Cells(emptyRow, 2).Value =        SignedContractCheckBox.Caption

End Sub


Private Sub PaymentTermsListBox_Click()

End Sub

Private Sub RenewalTermListBox_Click()

End Sub

Private Sub SelectionMechanismListBox_Click()

End Sub

Private Sub UserForm_Click()

 End Sub

Private Sub UserForm_Initialize()

'Empty SupplierNameTextBox
 SupplierNameTextBox.Value = ""

'Uncheck SignedContractCheckBox
 SignedContractCheckBox.Value = False

'Empty GeneralDescriptionTextBox
 GeneralDescriptionTextBox.Value = ""

 'Empty BusinessOwnerListBox
 BusinessOwnerListBox.Clear

 'Fill BusinessOwnerListBox
With BusinessOwnerListBox
    .AddItem ""
    .AddItem "Alison Gillies"
    .AddItem "Bernard Hunwick"
    .AddItem "Jon Williams"
    .AddItem "Laurent Sylvestre"
    .AddItem "Leeann McCallum"
    .AddItem "Sue Lowe"
End With

 'Empty DepartmentListBox
 DepartmentListBox.Clear

'Fill DepartmentListBox
 With DepartmentListBox
 .AddItem ""
 .AddItem "Buildings"
 .AddItem "Corporate Services"
 .AddItem "ICT"
 .AddItem "People & Culture"
 .AddItem "Transport & Logistics"
 End With

'Empty ContractStartDateTextBox
  ContractStartDateTextBox.Value = ""

 'Empty InitialTermListBox
  InitialTermListBox.Clear

 'Fill InitialTermListBox
 With InitialTermListBox
 .AddItem ""
 .AddItem "6"
 .AddItem "12"
 .AddItem "18"
 .AddItem "24"
 .AddItem "36"
End With

 'Empty RenewalTermListBox
  RenewalTermListBox.Clear

 'Fill RenewalTermListBox
  With RenewalTermListBox
 .AddItem ""
 .AddItem "6"
 .AddItem "12"
 .AddItem "18"
 .AddItem "24"
 .AddItem "36"
 End With

'Empty PaymentTermsListBox
 PaymentTermsListBox.Clear

'Fill PaymentTermsListBox
 With PaymentTermsListBox
 .AddItem ""
 .AddItem "7 days"
 .AddItem "30 days"
 .AddItem "20th month"
 .AddItem "Quarterly"
 .AddItem "Annual"
 End With

'Empty SelectionMechanismListBox
 SelectionMechanismListBox.Clear

'Fill SelectionMechanismListBox
 With SelectionMechanismListBox
 .AddItem ""
 .AddItem "RolledContract"
 .AddItem "RFP"
 .AddItem "RFQ"
 .AddItem "3 Quotes"
 .AddItem "2 Quote"
 .AddItem "Business Selection"
 End With

'Empty ValueOfContractTextBox
  ValueOfContractTextBox.Value = ""


 'Set Focus on SupplierNameTextBox
  SupplierNameTextBox.SetFocus


 End Sub

1 回答

  • 0

    (假设代码在表单的代码模块中)

    直接从表单写入工作表:

    Worksheets("some_sheet_name").Range("K9").Value = BusinessOwnerListBox.Value
    

    将选定的值放入变量,然后写入工作表

    Dim lbtext As String
    'String variables can't store "Null" so need to check
    If IsNull(BusinessOwnerListBox.Value) Then
        MsgBox "Select something!!"
        Exit Sub
    End If
    lbtext = BusinessOwnerListBox.Value
    Worksheets("some_sheet_name").Range("K9").Value = lbtext
    

    要么

    Dim lbtext As Variant
    lbtext = BusinessOwnerListBox.Value
    Worksheets("some_sheet_name").Range("K9").Value = lbtext
    

相关问题