获取VBA Excel 2010中的所有字体列表

Nee*_*han 1 excel vba excel-vba

我在excel VBA工作,我想获得组合框中所有字体的列表

任何人都可以帮助我

我尝试了这个代码,但我在listcount中收到错误:

...

    Set FontList = Application.CommandBars("Formatting").FindControl(ID:=1728)

    ' Put the fonts into column A

    *For i = 0 To FontList.ListCount - 1*
        combobox.AddItems  FontList.List(i + 1)
    Next i

    ' Delete temp CommandBar if it exists
    On Error Resume Next
    TempBar.Delete
End Sub
Run Code Online (Sandbox Code Playgroud)

小智 5

FontList应该返回已编制索引基于1的列表.没有必要从0开始.

Dim FontList
Dim i As Long

Set FontList = Application.CommandBars("Formatting").FindControl(ID:=1728)

'Put the fonts into column A
For i = 1 To FontList.ListCount
    Debug.Print FontList.List(i)
    Cells(Rows.Count, 1).End(xlUp)(2) = FontList.List(i)
    'combobox.AddItems FontList.List(i)
    If i > 50 Then Exit For
Next i
Run Code Online (Sandbox Code Playgroud)

这应该在ActiveSheet的 A列中构建一个字体列表.当它工作时,删除注释,以便它进入您的组合框.

请注意,您将获得与Home功能区上的字体列表下拉列表完全相同的字体列表.可能会有一些重复,因为该列表在列表顶部复制了几个字体,用于默认的标题和正文类别.