VBA - Populating grid from UserForm

Gemini774

New Member
Joined
Oct 18, 2021
Messages
6
Office Version
  1. 365
Platform
  1. Windows
Dear all,

I am trying from a userform containing BOTH comboboxes and textboxes to populate a grid line by line.
I am able to populate the grid based on both the comboboxes and the textboxes, however, the 2 textboxes always fill in the 2 first columns of the grid, instead of simply filling in the corresponding columns of the grid.
The textboxes are the 2 last fields of the userform.
I succeeded in moving to the right cells that the textboxes should be populating by using the Offset function.
In the iNextRow dimension I tried replacing "Row" by "Column" but it does not affect anything.

Below you can find below the code I am using (I am filling in 8 combboxes and then 2 textboxes in th esame userform):

VBA Code:
Private Sub CommandButton1_Click()

    Dim iComboBox As Long
    Dim iNextRow As Long
    Dim iTextBox As Long

    With Worksheets("Contact")
        ' find next row to populate. Contact is the sheet to be populated as of row 5 which is blank at the beginning.
        iNextRow = .Cells(.Rows.Count, 4).End(xlUp).Row + 1
        For iComboBox = 1 To 8
        .Cells(iNextRow, iComboBox).Value = Me.Controls("ComboBox" & iComboBox).Text
        Next

 iNextRow = .Cells(.Rows.Count, 4).End(xlUp).Row
 ActiveCell.Select
 Selection.Offset(0, 10).Select
          
        For iTextBox = 1 To 2
        .Cells(iNextRow, iTextBox).Value = Me.Controls("TextBox" & iTextBox).Text
        Next
End With

' Now clearing and hiding the userform:

Unload Me
Createprofiles.Hide
 
End Sub

Thanks a lot for your kind help.
 
Last edited by a moderator:

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Should the combos be going into columns A:H & the textboxes into I:J?
 
Upvote 0
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()

    Dim iComboBox As Long
    Dim iNextRow As Long
    Dim iTextBox As Long

    With Worksheets("Contact")
        ' find next row to populate. Contact is the sheet to be populated as of row 5 which is blank at the beginning.
        iNextRow = .Cells(.Rows.Count, 4).End(xlUp).Row + 1
        For iComboBox = 1 To 8
            .Cells(iNextRow, iComboBox).Value = Me.Controls("ComboBox" & iComboBox).Text
        Next
        For iTextBox = 1 To 2
            .Cells(iNextRow, iTextBox + 8).Value = Me.Controls("TextBox" & iTextBox).Text
        Next
    End With

' Now clearing and hiding the userform:

Unload Me
Createprofiles.Hide
 
End Sub
 
Upvote 0
Ok, how about
VBA Code:
Private Sub CommandButton1_Click()

    Dim iComboBox As Long
    Dim iNextRow As Long
    Dim iTextBox As Long

    With Worksheets("Contact")
        ' find next row to populate. Contact is the sheet to be populated as of row 5 which is blank at the beginning.
        iNextRow = .Cells(.Rows.Count, 4).End(xlUp).Row + 1
        For iComboBox = 1 To 8
            .Cells(iNextRow, iComboBox).Value = Me.Controls("ComboBox" & iComboBox).Text
        Next
        For iTextBox = 1 To 2
            .Cells(iNextRow, iTextBox + 8).Value = Me.Controls("TextBox" & iTextBox).Text
        Next
    End With

' Now clearing and hiding the userform:

Unload Me
Createprofiles.Hide
 
End Sub
it is working :) I am just struggling to understand why you wrote Textbox + 8 thanks a lot
 
Upvote 0
You're iTextBox counter starts at 1, so to get the data into col 9 you need to add 8 to it.
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,071
Latest member
cdnMech

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