2017-10-08 86 views
0

我正在嘗試更正我創建的用於向Excel顯示列表框值的代碼的VBA錯誤。任何人都可以幫忙嗎?列表框代碼的VBA錯誤

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 
+0

可能擺脫.ControlFormat'的'? – YowE3K

+0

'lbtext = .List(.Value)'所有這些都是設置字符串變量的值? –

+0

我對陳述「List is multi select single」感到困惑。是多選還是單選? (但是我很少(即從不?)在Excel本身中使用列表框,所以也許「多選單個」是真的,我只是不知道我在說什麼。) – YowE3K

回答

0

(假設代碼是窗體的代碼模塊中)

要直接從表到工作表寫:

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

將選定的值放入一個變量中,然後寫入th Ë工作

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