Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Populate information from userform to sheet

0

Hello
I want creating ranges when I fill userform . I have label1:lable6 and textbox1:textbox6
the label1 :label4 should be in firs row as header and textbox1: textbox4 should be under headers (label1:label4) when copy to sheet and the label 5:label6 should be in column C under textbox3(the name) when copy to sheet .
and the textbox5:textbox6 should be in column D for adjacent cell in column C based on ( label 5:label6)

every time fill data on userform should copy to the bottom inside the sheet, but if I repeat data on userform for the same name has already created then just replace data for the name is existed  under header based on filling on textboxes without repeat add range again for the same name  .
so I posted result in FORM sheet based on filling on userform as in second sheet how show in SS sheet with considering . the SS sheet will be empty  before copy from userform.

actually I try doing that but  my way is not practical because of depends on specific cells . so I would do that  for each range to the bottom  without specify cells as I did it .

thanks 

Answer
Discuss

Answers

0
Selected Answer

Hello Omaran,

Your existing code just needs to be made dynamic.

In my code below, it first checks it the name in TB3 already exists. If it does, it updates the data for that record. If the name does not exist, it creates a new record below the last record on the sheet.

Updated Apr. 21/24

Ah yes, I had forgotten to consider the "blank sheet" scenario.

This latest update takes into consideration that the worksheet may be blank.

File ASDD Rev2 is attached with the update code.

First the UserForm code will determine if the sheet is blank or has existing entries and will call the appropriate macrco.

Private Sub CommandButton1_Click()

' code revised by WillieD24, April 2024
' code will determine if all textboxes are filled,
'    is this the first entry to the sheet, is this data an update to
'    an existing name, or is this a new entry to add to the listings

Dim TBnbr As Long
Dim TBcnt As Long

Dim TB3name As String   ' name entered into textbox3
Dim TB3row As Long   ' row where TB3name is found
Dim LR As Long   ' last row with data in Col A

Sheets("SS").Select

' check that all textboxes are filled
For TBnbr = 1 To 6
    If UserForm1.Controls("TextBox" & TBnbr) = "" Then
        TBcnt = TBcnt + 1
    End If
Next
If TBcnt <> 0 Then   ' at least one textbox is empty
        MsgBox "All fields must be filled"
        Exit Sub   ' at least one textbox is empty
End If

LR = Cells(Rows.Count, 1).End(xlUp).Row   ' last used row in Col A

If LR = 1 Then
    Call First_Entry
    Else
    Call Additional_Entry
End If

    ' close userform
    Unload UserForm1

End Sub

Then one of two macros is called: the "First_Entry" macro, or the "Additional_Entry" macro.

Sub First_Entry()

' Written by WillieD24, April 2024
' code will format first 4 rows and add UserForm1 data

Dim TBnbr As Long
Dim TBcnt As Long
Dim TB3name As String   ' name entered into textbox3
Dim TB3row As Long   ' row where TB3name is found
Dim LR As Long   ' last row with data in Col A

' format column widths
Columns("A:B").ColumnWidth = 14
Columns("C").ColumnWidth = 24
Columns("D").ColumnWidth = 14
' format cell alignment
Range("A1:D4").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlCenter
        .WrapText = True
    End With
    ' format cell font
    With Selection.Font
        .Name = "Calibri"
        .Size = 10
    End With
    ' format cell borders
    Range("A1:D2, C3:D4").Select
    With Selection.Borders(xlEdgeLeft)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeTop)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeBottom)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlEdgeRight)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideVertical)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
    With Selection.Borders(xlInsideHorizontal)
        .LineStyle = xlContinuous
        .ColorIndex = 0
        .TintAndShade = 0
        .Weight = xlThin
    End With
        ' add cell fill colour, make font bold
        Range("A1:D1, C3:C4").Interior.Color = RGB(255, 102, 0)
        Range("A1:D1, C3:C4").Font.Bold = True
' add UserForm data
        ' enter headers
        Range("A1").Value = UserForm1.Label1.Caption
        Range("B1").Value = UserForm1.Label2.Caption
        Range("C1").Value = UserForm1.Label3.Caption
        Range("D1").Value = UserForm1.Label4.Caption
        Range("C3").Value = UserForm1.Label5.Caption
        Range("C4").Value = UserForm1.Label6.Caption
        ' enter data
        Range("A2").Value = UserForm1.TextBox1.Value
        Range("B2").Value = UserForm1.TextBox2.Value
        Range("C2").Value = UserForm1.TextBox3.Value
        Range("D2").Value = UserForm1.TextBox4.Value
        Range("D3").Value = UserForm1.TextBox5.Value
        Range("D4").Value = UserForm1.TextBox6.Value

Range("A1").Select

    ' close userform
    Unload UserForm1

End Sub
Sub Additional_Entry()

' Written by WillieD24, April 2024
' code will update data for existing name or
    ' create new entry and add the new data

Dim LR As Long   ' last row with data in Col A
Dim TB3name As String   ' name entered into textbox3
Dim TB3row As Long   ' row where TB3name is found
Dim CopyRow As Long

If Application.CountIf([C:C], UserForm1.TextBox3.Value) > 0 Then
        ' name in textbox3 already exists in sheet
        ' update data for existing name
        LR = Cells(Rows.Count, 1).End(xlUp).Row   ' last used row in Col A
        TB3name = UserForm1.TextBox3.Value
        ' get row number for existing name
        TB3row = 1 + WorksheetFunction.Match(TB3name, Range("C2:C" & LR), 0)
        ' update data from TB1, TB2, TB4, TB5, TB6
        Cells(TB3row, 1).Value = UserForm1.TextBox1.Value
        Cells(TB3row, 2).Value = UserForm1.TextBox2.Value
        Cells(TB3row, 4).Value = UserForm1.TextBox4.Value
        Cells(TB3row + 1, 4).Value = UserForm1.TextBox5.Value
        Cells(TB3row + 2, 4).Value = UserForm1.TextBox6.Value
    Else   ' create new entry for new name
        LR = Cells(Rows.Count, 1).End(xlUp).Row   ' last used row in Col A
        CopyRow = LR + 4
        ' copy data block structure/formatting
        Rows("1:4").Select
            Selection.Copy
        Rows(CopyRow).Select
            ActiveSheet.Paste
        Cells(LR + 9, 1).Select
        Application.CutCopyMode = False
        ' enter data
        Cells(LR + 5, 1).Value = UserForm1.TextBox1.Value
        Cells(LR + 5, 2).Value = UserForm1.TextBox2.Value
        Cells(LR + 5, 3).Value = UserForm1.TextBox3.Value
        Cells(LR + 5, 4).Value = UserForm1.TextBox4.Value
        Cells(LR + 6, 4).Value = UserForm1.TextBox5.Value
        Cells(LR + 7, 4).Value = UserForm1.TextBox6.Value
                
    End If
    ' close UserForm
    Unload UserForm1
    
End Sub

If this solves things for you, please mark my answer as Selected.

Cheers   :-)

Discuss

Discussion

thanks Willie,
unfortunately doesn't work as I expect . you can test with my file. the formatting is untidy, and doesn't copy to th bottom when I write another name !
Omaran (rep: 8) Apr 20, '24 at 4:07 am
@Omaran,

Please see my update above

Cheers   :-)
WillieD24 (rep: 557) Apr 20, '24 at 12:44 pm
may you tell me what's my bad? !
when I implement your code when the SS sheet is empty then  pops mismatch error in this line 
  Rows(LR - 1 & ":" & LR + 2).Select

when I run the macro  from the first tim will not be any data in SS sheet .
Omaran (rep: 8) Apr 21, '24 at 3:48 am
@Omaran,

Ah, yes, the blank sheet scenario. I had forgotten to consider this. I have re-written the code and updated my answer above. I have also updated the attached file (ASDD Rev2) which has the new code.

Cheers   :-)
WillieD24 (rep: 557) Apr 21, '24 at 7:44 pm
really great !
thank you so much .
Omaran (rep: 8) Apr 22, '24 at 5:41 pm

Happy to help. Thanks for selecting my answer.

Cheers   :-)
WillieD24 (rep: 557) Apr 22, '24 at 10:32 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login