View Single Post
 
Old 05-19-2016, 09:37 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The short answer is yes you can do this, with a couple of provisos, and it will be faster from Excel. The first is that you should have the names of the command buttons in the first column and not merely numbers.

Command Button Caption
CommandButton1 Edit Text
CommandButton2 Add Column
CommandButton3 Delete Column
CommandButton4 Delete Table
CommandButton5 Font Resize

The second is that I would only do this if the names changed frequently (otherwise it is better to name the buttons either directly or in code). That being the case, you could use the following macro (in an ordinary module) to call the userform and name the buttons.

Code:
Option Explicit
Const strWorkbook As String = "C:\Path\Forums\buttons.xlsx" 'The workbook path
Const strSheet As String = "Sheet1"        'The name of the worksheet

Sub Example()
'Graham Mayor - www.gmayor.com
Dim oFrm As New UserForm1
Dim Arr() As Variant
Dim oCtrl As Control
Dim i As Long
    Arr = xlFillArray(strWorkbook, strSheet)
    With oFrm
        For i = 0 To UBound(Arr, 2)
            For Each oCtrl In .Controls
                If oCtrl.Name = Arr(0, i) Then
                    oCtrl.Caption = Arr(1, i)
                    Exit For
                End If
            Next oCtrl
        Next i
        .Show
    End With
lbl_Exit:
    Set oCtrl = Nothing
    Exit Sub
End Sub


Private Function xlFillArray(strWorkbook As String, _
                             strWorksheetName As String) As Variant
'Graham Mayor - www.gmayor.com
Dim RS As Object
Dim CN As Object
Dim iRows As Long

    strWorksheetName = strWorksheetName & "$]"
    Set CN = CreateObject("ADODB.Connection")
    CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                              "Data Source=" & strWorkbook & ";" & _
                              "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"

    Set RS = CreateObject("ADODB.Recordset")
    RS.Open "SELECT * FROM [" & strWorksheetName, CN, 2, 1

    With RS
        .MoveLast
        iRows = .RecordCount
        .MoveFirst
    End With
    xlFillArray = RS.GetRows(iRows)
    If RS.State = 1 Then RS.Close
    Set RS = Nothing
    If CN.State = 1 Then CN.Close
    Set CN = Nothing
lbl_Exit:
    Exit Function
End Function
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote