• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Help with vba code

Ady Zavala

New Member
Hello, I am a new user I am almost 47 years old and I decided to learn a little from vba. I want to enter to this amazing world-
I am practicing, I made a simple form in which I want to enter data from the form to a excel sheet and keep on entering data.
Is there a simple code that I can use to enter data ?
I would appreciate the help. Thank you.
 

Attachments

  • NEW DATA.xlsm
    20.6 KB · Views: 2
Hello. Wow, I was able to enter the data, how exciting this world is. Query, how do I make sure that after I enter the data, it orders the sheet where the data is from the newest to the oldest?
 

Attachments

  • NEW DATA.xlsm
    23.4 KB · Views: 1
Hi,
try thiss code.
Code:
Private Sub CommandButton1_Click()
Dim lrow_v As Long
With Sheets("NEW DATA")
    lrow = .Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1
    .Cells(lrow, 1).Resize(, 3).Value = Array(TextBox1.Value, TextBox2.Value, TextBox3.Value)
    .Range("A2").CurrentRegion.Sort key1:=Range("A2"), order1:=xlDescending, Header:=xlYes
End With
For Each Ctrl In Controls
    If TypeName(Ctrl) = "TextBox" Then Ctrl.Value = ""
Next Ctrl
TextBox1.SetFocus
End Sub
 
To enter data from a form into an Excel sheet, you can use VBA to write a macro. Here's a simple example of VBA code that will take data from a user and enter it into an Excel worksheet:
>>> use code - tags <<<
Code:
Sub EnterData()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Worksheets("Sheet1") ' Change "Sheet1" to the name of your worksheet
   
    ' Find the next empty row in column A
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row + 1
   
    ' Get data from the userform
    Dim data1 As String
    Dim data2 As Integer
    ' Add more variables for additional fields as needed
   
    data1 = UserForm1.TextBox1.Value ' Change UserForm1 to the name of your userform
    data2 = UserForm1.TextBox2.Value ' Change TextBox1 and TextBox2 to the actual control names
   
    ' Enter data into the worksheet
    ws.Cells(lastRow, 1).Value = data1
    ws.Cells(lastRow, 2).Value = data2
    ' Enter more data fields as needed
   
    ' Clear the userform
    UserForm1.TextBox1.Value = ""
    UserForm1.TextBox2.Value = ""
    ' Clear more fields as needed
   
    ' Optionally, save the workbook
    ThisWorkbook.Save
   
    ' Notify the user
    MsgBox "Data entered successfully!", vbInformation
End Sub
 
@ antonio,welcome to Chandoo.
please use code tags if you post code. Click the 3 dot and use </> to put your code between code tags.
 
Sure
I made a table from your sheet (with the name newdata_tbl)
I changed your textboxes to T_00 ,T_01 en T_02 and your listbox to Lb_00
(then is easy to use them in a loop,as you can see in the lisbox (Lb_00) click event.
Click in the listbox and see what happens.
I forgot to tell, a listbox always starts from column 0, so your column A in your sheet is not 1 but 0 in a listbox.
(same for a combobox)
 

Attachments

  • NEW DATA.xlsm
    25.4 KB · Views: 8
Last edited:
Back
Top