操作调用第二个配对工作表的列表框

问题描述 投票:0回答:1

这是一个 Axtive X 列表框,我似乎无法让 OLEForm.Object 部分不出错。我不知道我做错了什么。 我知道使用 ListBox1.Text 启动工作表的第一种方法有效,但我需要为第二个更复杂的代码调暗并设置对象,并且 OLEObject 方法不起作用。运行时错误 438 对象不支持此属性或方法。

            Sheets(ListBox1.Text).Visible = True
            Sheets(ListBox1.Text).Activate
            Me.ListBox1.ListIndex = -1
        Sub ActivateSheetFromListBox()
            Dim wsIndex As Worksheet
            Dim wsTarget As Worksheet
            Dim listBox As listBox
            Dim selectedItem As String
        
            ' Get references to the worksheets and list box
'I cannot get these form controls to work OLEFormControl.Object, Or .Format. It's an active X control, not a MS Form object.
            'Set wsIndex = ThisWorkbook.Worksheets("Index")
            'Set listBox = wsIndex.Shapes("ListBox1").OLEFormControl.Object
            'Set wsIndex.Shapes("Listbox1").OLEFormat.Object.ListIndex = 0
        
            ' Get the selected item from the list box
            selectedItem = listBox.ListIndex
        
            ' Get the corresponding sheet names based on the selected item
            ' Adjust the array below to match your sheet pairs
            Dim sheetPairs() As Variant
            sheetPairs = Array("LXXXXXXB-LXXXXXXT|LXXXXXXT-LXXXXXXB, LXXXXXXB-RXXXXXXH|RXXXXXXH-LXXXXXXB, LXXXXXXB-SXXXXXXH|SXXXXXXH-LXXXXXXB, LXXXXXXB-LXXXXXXO|LXXXXXXO-LXXXXXXB")
        
            ' Activate the paired sheets and set them to the right of the "Index" sheet
            If selectedItem >= 0 And selectedItem < UBound(sheetPairs) Then
                Dim sheetPair() As String
                sheetPair = Split(sheetPairs(selectedItem), "|")
                
                wsTarget = ThisWorkbook.Worksheets(sheetPair(0))
                wsTarget.Visible = xlSheetVisible
                
                ' Move the selected sheet to the right of the "Index" sheet
                wsTarget.Move After:=wsIndex
                
                wsTarget.Activate
        
                ' Activate the paired sheet and move it to the right of the selected sheet
                ThisWorkbook.Worksheets(sheetPair(1)).Visible = xlSheetVisible
                ThisWorkbook.Worksheets(sheetPair(1)).Move After:=wsTarget
                'ThisWorkbook.Worksheets(sheetPair(1)).Activate
                
            End If
        End Sub
excel vba forms controls
1个回答
0
投票

例如:

Dim lb As MSForms.ListBox
    
Set lb = ThisWorkbook.Worksheets("Data").Shapes("Listbox1").OLEFormat.Object.Object
Debug.Print lb.ColumnCount, lb.ListIndex
© www.soinside.com 2019 - 2024. All rights reserved.