PDA

View Full Version : Macro to create a database



Sonofabhanda
12-10-2020, 01:26 AM
So, I have 2 sheets. Sheet 1 has details of grades of class students. There's a submit button. I need to assign a macro to this button that will copy the data from sheet 1 and paste it in sheet 2 in respective columns. So, every time you punch info in sheet 1 and press submit button it should add a new line item in sheet 2 table. May need to paste special values so that when to enter new info in sheet 1 data in sheet 2 gets entered in next row.
Code should also enter the date on which submit button was pressed.:help

snb
12-10-2020, 03:11 AM
Use a userfom for user input.

Logit
12-10-2020, 01:04 PM
.
Here is a different approach using a UserForm :

https://www.amazon.com/clouddrive/share/kRygsfIRxXDC21KewPVQ9iGjZ3tY8gNZGgiDOnguw12




Option Explicit
Private Sub btnCancel_Click()
Unload Me
End Sub


Private Sub btnOK_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim newRow As Long

newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1

'The next two lines can be expanded as many times as needed for all the entry fields in your project

ws.Cells(newRow, 1).Value = Me.txtFirstName.Value
ws.Cells(newRow, 2).Value = Me.txtSurname.Value

End Sub
Sub CommandButton1_Click()
Selection.EntireRow.Delete
End Sub

snb
12-11-2020, 02:47 AM
@logit

Instead of

Private Sub btnOK_Click()
Dim ws As Worksheet
Set ws = ActiveSheet
Dim newRow As Long

newRow = Application.WorksheetFunction.CountA(ws.Range("A:A")) + 1

'The next two lines can be expanded as many times as needed for all the entry fields in your project

ws.Cells(newRow, 1).Value = Me.txtFirstName.Value
ws.Cells(newRow, 2).Value = Me.txtSurname.Value

End Sub

This oneliner suffices:


Private Sub btnOK_Click()
cells(rows.count,1).end(xlup).offset(1).resize(,2)=array(txtFirstName,txtSu rname)
End Sub