我在一張 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
資料表 視圖 資料表視圖
uj5u.com熱心網友回復:
我認為您在這里所做的作業比您需要做的更多 - 您可以更多地利用 listobject 屬性和方法。
簡化:
Sub FillDriverList()
Const WorkSheetName = "MaintenanceData"
Const WorkTableName = "tblDriverList"
Dim tbl As ListObject
Set tbl = ThisWorkbook.Sheets(WorkSheetName).ListObjects(WorkTableName)
'loop each cell in the "DRIVER LIST" column
For Each c In tbl.ListColumns("DRIVER LIST").DataBodyRange.Cells
DataEntry06.cmbInput05.AddItem c.Value
Next c
End Sub
uj5u.com熱心網友回復:
嘗試“tbl.DataBodyRange.Select”而不是 With / End With
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/484810.html
上一篇:跨2個域共享用戶資料
