【发布时间】:2022-09-26 13:48:54
【问题描述】:
我在一张 tblMaintenanceData、tblVehicleDailyLog 和 tblDriverList 上有三个表。
我使用表格来填写表格。这些表格使用组合框下拉列表,从这些表中填充数据——一个用于车辆 ID,第二个用于驾驶员姓名。
车辆 ID 下拉列表正确填写。驱动程序名称下拉列表没有;而是在开始将数据加载到下拉列表时抛出错误(下面的斜体和粗体线)。
使用 Debug.Print 我发现 iRows 和 iColumns 填充了正确的值,Cells(x, y).Value 指向页面上的第一个表 (tblMaintenanceData),而不是 tblDriverList。
Sub FillDriverList()
\' This fills the drop down list of available drivers for each of the user created forms.
Const WorkSheetName = \"MaintenanceData\"
Const WorkTableName = \"tblDriverList\"
Dim tbl As ListObject
Dim lRows As Long, lColumns As Long, lWork01 As Long
Dim i As Integer
Dim ws As Worksheet
Dim CurrentPageState As String
Dim CurrentPageProtection As String
Set ws = ThisWorkbook.Sheets(WorkSheetName)
\' this saves the Page State for Visibility and Protection, allowing to be reset after working with the page
CurrentPageState = ws.Visible
CurrentPageProtection = ws.ProtectContents
ws.Visible = xlSheetVisible
ws.Select
Set tbl = ws.ListObjects(WorkTableName)
With tbl.DataBodyRange
lRows = .Rows.Count
lColumns = .Columns.Count
End With
\'Debug.Print lRows & \" / \" & lColumns
For i = 1 To lColumns
If Cells(1, i).Value = \"DRIVER LIST\" Then lWork01 = i
\'Debug.Print Cells(1, i).Value
Next
\'Debug.Print Cells(2, 1).Value & \" - \" & Cells(3, 1).Value & \" - \" & Cells(4, 1).Value
For i = 1 To lRows
With DataEntry06
***.cmbInput05.AddItem ws.Cells(i, lWork01).Value***
End With
Next
CLOSE_SUB:
ws.Visible = CurrentPageState
If CurrentPageProtection = True Then
ws.Protect UserInterfaceOnly:=True
End If
Set ws = Nothing
Set tbl = Nothing
End Sub