【发布时间】:2021-02-21 11:58:03
【问题描述】:
我正在处理一个已导入并格式化到我的工作簿中的时间表。
我希望它在上层列表框中填充阶段,然后选择一个阶段时,与这些阶段关联的子任务显示在底部列表框中。
我想使用一个数组,但当列不相邻或空白单元格存在“间隙”时,我似乎遇到了问题。
我第一次尝试使用将数组分配给 currentregion 工作,但将所有列和字段都带入。Listbox1 应包含(ID、PHASE NAME、DURATION、START DATE、FINISH DATE)列表框 2 应在选择阶段时包含右侧列中的子任务(如果有),列在下一个阶段名称之前。 (ID、子任务名称、持续时间、开始日期、结束日期)
(见图)
我有代码,但它比实际的半工作脚本更多是我的故障排除。
Dim shT As Worksheet
Dim schnumrng1 As Range
Dim schnumrng2 As Range
Dim schnumrng3 As Range
Dim schnumrng4 As Range
Dim schnumrng5 As Range
Dim schpersonrng As Range
Dim schphaserng As Range
Dim schlistrng As Range
Dim maxschnum
Dim schstatus
Dim schperson
Dim schlistnum
Dim Ar() As String
Dim i As Long
Dim j As Long
Dim rng As Range
Dim cl As Range
Dim lc
'allowevents = True
''Set Screen parameters
'Application.ScreenUpdating = False
'Application.EnableEvents = False
'
Worksheets("Schedule").Visible = True
ThisWorkbook.Worksheets("Schedule").Activate
'
Set shT = Worksheets("Schedule")
maxschnum = shT.Cells(shT.Rows.Count, "A").End(xlUp).Row
Set schnumrng = Range("B5", "B" & maxschnum)
'Set Ranges for the list box
Set schnumrng1 = Range("A5", "A" & maxschnum)
Set schnumrng2 = Range("B5", "B" & maxschnum)
Set schnumrng3 = Range("D5", "D" & maxschnum)
Set schnumrng4 = Range("E5", "E" & maxschnum)
Set schnumrng5 = Range("F5", "F" & maxschnum)
'This is static and not moving to the next line in my for statement / switched to named ranges and errors
Set rng = schnumrng1, schnumrng2, schnumrng3, schnumrng4, schnumrng5
'Set rng = Range("A5,B5,D5,E5,F5")
i = 1
j = 1
For Each lc In schnumrng
If lc <> vbNullString Then
For Each cl In rng
ReDim Preserve Ar(1, 1 To i)
Ar(j, i) = cl.Value
i = i + 1
Next cl
Else
End If
j = j + 1
Next lc
With ScheduleForm.SchMainTasklt
.ColumnCount = i - 1
.ColumnWidths = "50;150;50;50;50"
.List = Ar
End With
那么我的问题是两个方面,试图使用动态范围或其他工具索引?收藏?填充第一个列表框。 2. 数据没有为组织目的分离时,如何处理空白和不连续的列。
【问题讨论】:
-
我不太明白你想做什么。用用户表单表达结果会很困难,所以最好解释一下如何获取工作表上的数据。
-
我正在尝试使用数组将 ABDEF 列放在一个列表框中,基于 B 列中具有值的行。但是似乎无法将多列添加到列表框中。目前我的代码将列的动态范围设置为另一个范围,这不起作用。然后我尝试根据第一个列表框中的选择,使用数组为 Columns ACDEF 做第二个列表框,但我遇到与第一个列表框相同的问题,使用动态范围加上 C 列中的空白导致问题当我尝试使用范围时。我希望这更清楚一点。谢谢