Private Sub UserForm_Initialize() how to combine 2 codes together

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,227
Office Version
  1. 2010
Platform
  1. Windows
Hi hope you can help i want to combine the 2 vba codes below together if possible as i cant have both on same sheet as i get an error hope you can help?
VBA Code:
Private Sub UserForm_Initialize()

    cmdUpdate.Enabled = False 'Only enable the button when a row has been returned
    
    'Source for this nifty code is from here:
    'http://stackoverflow.com/questions/10763310/how-to-populate-data-from-a-range-multiple-rows-and-columns-to-listbox-with-vb
    Dim rng As Range
    Dim i As Long, j As Long, rw As Long
    Dim Myarray() As String
    
    Set rng = Range("ListBox1")

    With Me.ListBox1
        .Clear
        .ColumnHeads = False
        .ColumnCount = rng.Columns.Count

        ReDim Myarray(rng.Rows.Count, rng.Columns.Count)

        rw = 0

        For i = 1 To rng.Rows.Count
            For j = 0 To rng.Columns.Count
                Myarray(rw, j) = rng.Cells(i, j + 1)
            Next
            rw = rw + 1
        Next

        .List = Myarray
        '.TopIndex = 1
        
    End With
    
    If Val(Me.txtLBSelectionIndex) > 1 Then
        Me.ListBox1.Selected(Val(Me.txtLBSelectionIndex)) = True
    End If

End Sub

And

Code:
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 24
ListBox1.RowSource = "B2:F7"
ListBox1.ColumnHeads = True
End Sub
 
I'm sorry but I don't follow.

The code you posted uses 2 different methods, List and RowSource, and, seemingly, 2 different sets of data to populate the listbox.

You should only be using one method to populate the listbox.
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hi i used the code below (which worked) to populate the data into the listbox.
VBA Code:
Private Sub UserForm_Initialize()
ListBox1.ColumnCount = 24
ListBox1.RowSource = "B2:F7"
ListBox1.ColumnHeads = True
End Sub
Then the other code which i tried to combine with the above was to do any update/changes from the textbox.
 
Upvote 0
You wouldn't use the Initialize event to update/change the textboxes, that would be a separate event.
 
Upvote 0

Forum statistics

Threads
1,215,065
Messages
6,122,944
Members
449,095
Latest member
nmaske

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top