我正在尝试创建一个用户窗体,该用户窗体从userform1接收输入并将其传递给userform2,后者在userform2上显示属于该数据集的信息。问题是,一旦从第1行(CATBOX)中选择了整个类别,我就需要将解析器限制为第2行中的子组件,因为同一行的其他类别中还有其他子组件。第1行中的每个类别都是一系列合并的单元格。
我已经尝试使用'find'查找userform1!CATBOX
的值并返回位置以获取起始列。然后,我尝试找到合并单元格的范围,以便获得终点。然后,我尝试将解析器限制为第2行的列范围,以收集我的信息。我包括了最后一部分代码,只是将起点和终点的值显示在userform2上,这对我的代码来说不是必需的。
With ActiveSheet
Set ra = ActiveSheet.Cells.Find(What:=UserForm1!CATBOX.Value, After:=Range("A1"), _
LookIn:=xlFormulas, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
Set rng = Range(ra)
If rng.MergeCells Then
Set rng = rng.MergeArea
Set rngStart = rng.Cells(1, 1)
Set rngEnd = rng.Cells(rng.Rows.Count, rng.Columns.Count)
End If
Set rag = UserForm2.Controls.Add("Forms.Label.1", "rag", True)
With rag
.Caption = rngStart.Address
.Left = 10
.Width = 50
.Top = 50
End With
Set rag2 = UserForm2.Controls.Add("Forms.Label.1", "rag2", True)
With rag2
.Caption = rngEnd.Address
.Left = 70
.Width = 50
.Top = 50
End With
End With
我希望获得的结果是能够分析第二行信息,该信息仅限于其上方合并类别所建立的列范围。
答案 0 :(得分:1)
欢迎来到SO。尽管要求和工作表的数据布局不清楚,但假定如下。 可以根据您的要求修改所使用的代码,并将其从CATBOX的Change Event更改为任何合适的事件
Private Sub CATBOX_Change()
Dim Rng As Range, SubRng As Range
Dim Rw As Long, ColSt As Long, ColEnd As Long, i As Long, ScatNo As Long
Dim Rag As Object
With ThisWorkbook.ActiveSheet
Set Rng = .Rows(1).Find(What:=UserForm1.CATBOX.Value, LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False)
If Rng Is Nothing Then Exit Sub
Set Rng = Rng.MergeArea
Set rngstart = Rng.Cells(1, 1)
Set rngEnd = Rng.Cells(Rng.Rows.Count, Rng.Columns.Count)
Rw = Rng.Row + Rng.Rows.Count
ColSt = Rng.Column
ColEnd = Rng.Column + Rng.Columns.Count - 1
Debug.Print Rw, ColSt, ColEnd
Set Rng = .Range(.Cells(Rw, ColSt), .Cells(Rw, ColEnd))
ScatNo = 0
For Each SubRng In Rng
If SubRng.Value <> "" Then
ScatNo = ScatNo + 1
Set Rag = UserForm2.Controls.Add("Forms.Label.1", "Scat" & ScatNo)
Rag.Caption = SubRng.Value
Rag.Left = 70
Rag.Width = 50
Rag.Top = ScatNo * 30
End If
Next
End With
UserForm2.Show
End Sub