Private Sub UserForm_Initialize() how to combine 2 codes together

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,231
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
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
What is stopping you adding the 3 lines in the 2nd 1 to the 1st one?

Where do you place them and what error do you get? I've put the lines where I think they should go below

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
        'Set up listbox first
        .ColumnCount = 24
        .RowSource = "B2:F7"
        .ColumnHeads = True

        .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
 
Upvote 0
HI i get a error on the line below - method or data not found
VBA Code:
 If Val(Me.txtLBSelectionIndex) > 1 Then
 
Upvote 0
But if you don't include the three lines in the separate sub you don't get the error?



VBA Code:
ListBox1.ColumnCount = 24
ListBox1.RowSource = "B2:F7"
ListBox1.ColumnHeads = True

Naturally you can't have 2 subs named the same nor should you ever need to
 
Upvote 0
The reason you get the error on this line is because you don't have a control named txtLBSelectionIndex on the userform.
VBA Code:
 If Val(Me.txtLBSelectionIndex) > 1 Then

By the way, regarding your original question - why do you want to combine the 2 sets of code?

Part of this code is populating the listbox from a range,
Code:
        'Set up listbox first
        .ColumnCount = 24
        .RowSource = "B2:F7"
        .ColumnHeads = True
but then you have this code that's trying to populate it using an array.
VBA Code:
        .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
 
Upvote 0
yes i need to put the 2 codes together as i cant have 2 userform initialize on same sheet, but the one code isnt working either which i need to fix somehow. What do you mean by Control?
 
Upvote 0
But the 2 sets of code essentially 'cancel' each other.

Do you want to change the way the listbox is populated based on some criteria?
 
Upvote 0
yes thats what i want, i have data in sheet1 and when you click on the userform it shows that data in the listbox, then i have textboxes fr when you click on the list it populates the data in the textboxes and this is where you can change the info if required, then i click on the update buton and it should update the data then in sheet1. thats what i am trying to do :)
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,934
Members
449,275
Latest member
jacob_mcbride

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