Populating ListBoxes Into Corresponding Excel Sheet Rows

rameezl17

Board Regular
Joined
Mar 6, 2018
Messages
105
Hi All,

NOTE: Row 1 On UserForm Should Populate In Row 7 In Worksheet "Capital"
Row 2 On UserForm Should Populate In Row 8 In Worksheet "Capital"
I Have 2 Rows Of ListBoxes & TextBoxes & ComboBoxes. When A User Click On CommandButton1, My Code Populates Data From The UserForm Into An Excel Sheet Called "Capital". I Made A Code That Works And Does This Correctly...BUT lets Just Say A User Populates Just Row 2 On The UserForm And Not Row 1, The TextBoxes & Combo Boxes Populate Into The Correct Row (8) In "Capital" Sheet, BUT The ListBoxes Populate In Row (7) In "Capital" Sheet, Opposed To Row 2 Where It Was Populated In The UserForm. I'm Not Sure Why This Is Happening, I Tried Playing Around With My Code But Can't Figure It Out.

Each Row In UserForm Looks Like This:

Row 1 On UserForm: TextBox1 ListBox1 TextBox2 ComboBox1 ListBox2 TextBox4
Row 2 On UserForm: TexxtBox5 ListBox3 TextBox6 ComboBox2 ListBox3 TextBox5
Below Is The Code:

Code:
Private Sub CommandButton1_Click()
Dim erow As Integer
 Dim ws As Worksheet
 Set ws = Worksheets("Capital")
erow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(3, 0).Row
Me.TextBox1.SetFocus

'Row1 On UserForm 

 ws.Cells(7, 3).Value = Me.TextBox1.Value
 ws.Cells(34, 5).Value = Me.ComboBox1.Value
 ws.Cells(7, 6).Value = Me.TextBox4.Value
 'ListBox1
 Dim lngSelected As Long, lngRows As Long, lngColumn As Long
     Dim myArray(1 To 9)
     
     For lngSelected = 0 To Me.ListBox1.ListCount - 1
         If Me.ListBox1.Selected(lngSelected) Then
             lngRows = lngRows + 1
             For lngColumn = 1 To 9
                 myArray(lngColumn) = Me.ListBox1.List(lngSelected, lngColumn - 1)
             Next lngColumn
            ws.Cells(7, 4).End(xlUp).Offset(1).Resize(, 1) = myArray
         End If
 Next lngSelected
'ListBox2
     For lngSelected = 0 To Me.ListBox2.ListCount - 1
         If Me.ListBox2.Selected(lngSelected) Then
             lngRows = lngRows + 1
             For lngColumn = 1 To 9
                 myArray(lngColumn) = Me.ListBox2.List(lngSelected, lngColumn - 1)
             Next lngColumn
            ws.Cells(7, 5).End(xlUp).Offset(1).Resize(, 1) = myArray
         End If
 Next lngSelected

 'Row2 On UserForm

 ws.Cells(8, 3).Value = Me.TextBox5.Value
 ws.Cells(35, 5).Value = Me.ComboBox2.Value
 ws.Cells(8, 6).Value = Me.TextBox8.Value
     
 'ListBox3
       For lngSelected = 0 To Me.ListBox3.ListCount - 1
         If Me.ListBox3.Selected(lngSelected) Then
             lngRows = lngRows + 1
             For lngColumn = 1 To 9
                 myArray(lngColumn) = Me.ListBox3.List(lngSelected, lngColumn - 1)
             Next lngColumn
            ws.Cells(8, 4).End(xlUp).Offset(1).Resize(, 1) = myArray
         End If
 Next lngSelected
'ListBox4
      For lngSelected = 0 To Me.ListBox4.ListCount - 1
         If Me.ListBox4.Selected(lngSelected) Then
             lngRows = lngRows + 1
             For lngColumn = 1 To 9
                 myArray(lngColumn) = Me.ListBox4.List(lngSelected, lngColumn - 1)
             Next lngColumn
            ws.Cells(8, 5).End(xlUp).Offset(1).Resize(, 1) = myArray
         End If
 Next lngSelected
End Sub
 
Last edited by a moderator:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest

Forum statistics

Threads
1,215,965
Messages
6,127,969
Members
449,414
Latest member
sameri

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