gpt4 book ai didi

excel - 如何使用 VBA 在工作表中创建 MSForms 列表框?

转载 作者:行者123 更新时间:2023-12-04 20:04:00 25 4
gpt4 key购买 nike

我正在尝试创建类型为 MSForms.ListBox 的列表框以编程方式使用 VBA。

我做不到 Set ListBox = New MSForms.ListBox因为它抛出编译错误: Invalid use of the New keyword .

在下面的代码中,当我创建 OLEObject Macro1它创建 VBAProject.Sheet1.ListBox1 (或其他数字),然后我可以(在执行结束后)在 Macro2 中分配到 MSForms.ListBox 类型的变量但它只有在我一次运行一个宏时才有效。

MSForms.ListBox然后我可以更改 ListBox.ColumnHeads = True 等属性(尽管除了将列表值寻址到 ListBox.ListFillRange = RangeAddress 的范围之外,我不知道如何更改 head 值)。

如果我尝试逐步执行代码,我会收到消息 Can't enter break mode at this time .

我得到了OLEObject从录制宏和inserting a List Box ActiveX Control .

' Microsoft Excel 2013 built-in references:
' Excel - Microsoft Excel 15.0 Object Library
' VBA - Visual Basic For Applications

' VBA project library:
' VBAProject

' Aditional references:
' MSForms - Microsoft Forms 2.0 Object Library

Private Sub Macro1()

Dim Worksheet As Excel.Worksheet
Dim ListBox As Excel.ListBox
Dim Shape As Excel.Shape
Dim OLEObject As Excel.OLEObject

Set Worksheet = VBAProject.Sheet1
Worksheet.Range("A1").Value = "Header"
Worksheet.Range("A2").Value = "Value 1"
Worksheet.Range("A3").Value = "Value 2"
Worksheet.Range("A4").Value = "Value 3"

For Each Shape In Worksheet.Shapes
Shape.Delete
Next Shape

Set ListBox = Worksheet.ListBoxes.Add(60, 10, 100, 100)
ListBox.List = Array("Header", "Value 1", "Value 2", "Value 3")
ListBox.ListFillRange = "A1:A4"

Set OLEObject = Worksheet.OLEObjects.Add(ClassType:="Forms.ListBox.1", Link:=False, Left:=170, Top:=10, Width:=100, Height:=100)
OLEObject.ListFillRange = "A1:A4"

Set Shape = Worksheet.Shapes.AddOLEObject(ClassType:="Forms.ListBox.1", Link:=False, Left:=280, Top:=10, Width:=100, Height:=100)

End Sub

Private Sub Macro2()

Dim Worksheet As Excel.Worksheet
Dim ListBox As MSForms.ListBox

Set Worksheet = Excel.Application.ActiveSheet

Set ListBox = VBAProject.Sheet1.ListBox1
ListBox.ListFillRange = ""
ListBox.List = Array("Header", "Value 1", "Value 2", "Value 3")
ListBox.ColumnHeads = True
ListBox.ListFillRange = "A1:A4"
ListBox.BorderStyle = MSForms.fmBorderStyle.fmBorderStyleSingle

End Sub

编辑:

使用已接受答案中给出的解决方案的简单工作示例:
Private Function CreateListBox( _
Optional ByVal Worksheet As Excel.Worksheet = Nothing, _
Optional ByVal Width As Long = 100, _
Optional ByVal Height As Long = 100, _
Optional ByVal Left As Long = 0, _
Optional ByVal Top As Long = 0 _
) As MSForms.ListBox

Const ClassType As String = "Forms.ListBox.1"

If Worksheet Is Nothing Then
Set Worksheet = Excel.Application.ActiveSheet
End If

Set CreateListBox = Worksheet.OLEObjects.Add( _
ClassType, _
Left:=Left, _
Top:=Top, _
Width:=Width, _
Height:=Height).Object

End Function

Private Sub Test()

Dim ListBox As MSForms.ListBox

Set ListBox = CreateListBox
Stop ' Able to stop/suspend code execution here but not inside the function when creating the OLEObject

End Sub

最佳答案

当您按 .With lb...End With 内代码块,您将看到智能感知不会显示某些属性,例如 .ColumnHeads , .BorderStyle.List .您可以通过在其前面加上 .Object 来访问这些属性。

这是你正在尝试的吗?

Sub Sample()
Dim lb As OLEObject
Dim ws As Worksheet

Set ws = Sheet1

For Each lb In ws.OLEObjects
lb.Delete
Next lb

Set lb = ws.OLEObjects.Add(ClassType:="Forms.ListBox.1", _
Top:=60, _
Left:=10, _
Height:=100, _
Width:=100)

With lb
.ListFillRange = "'" & ws.Name & "'!A1:A16" '<~~ Change range here
'.Object.List = Array("Header", "Value 1", "Value 2", "Value 3")
.Object.ColumnHeads = True
.Object.BorderStyle = MSForms.fmBorderStyle.fmBorderStyleSingle
End With
End Sub

在行动

enter image description here

关于excel - 如何使用 VBA 在工作表中创建 MSForms 列表框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59770317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com