krunal123

Board Regular
Joined
Jun 26, 2020
Messages
177
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
1684730715624.png



Option Explicit

Private Sub cmdSave_Click()
Dim LR As Integer, Master As Worksheets
Set Master = ThisWorkbook.Worksheets("Master")

LR = Master.Cells(Rows.Count, "A").End(xlUp).Row

If LR = 2 And Cells(LR, "A").Value = "" Then
LR = LR
Else
LR = LR + 1
End If

Master.Cells(LR, "A").Value =ID.Value
Master.Cells(LR, "B").Value = Name.Value
Master.Cells(LR, "C").Value = GENDER.Value
Master.Cells(LR, "D").Value = Address.Value
Master.Cells(LR, "E").Value = CONTACTNo.Value
Master.Cells(LR, "F").Value = DEPARTMENT.Value

End Sub
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi

If you are posting your record to an Excel Table then the code needs a different approach

See if this update does what you want

Rich (BB code):
Private Sub cmdSave_Click()
    Dim tblMaster     As ListObject
    Dim NewRecord   As ListRow
  
    Set tblMaster = ThisWorkbook.Worksheets("Master").ListObjects(1)
  
    'Add New row to the table
    Set NewRecord = tblMaster.ListRows.Add(AlwaysInsert:=True)
  
     With NewRecord
        .Range(1).Value = Me.ID.Value
        .Range(2).Value = Me.txtName.Value
        .Range(3).Value = Me.Gender.Value
        .Range(4).Value = Me.Address.Value
        .Range(5).Value = Me.ContactNo.Value
        .Range(6).Value = Me.Department.Value
    End With

    MsgBox "New Record Added To Table", 64, "Record Added"

End Sub

Note that I have renamed your control Name to txtName.

Name object is a member of the Names collection for the Application, Workbook, and Worksheet objects and using it as a control name may give rise to conflicts – to avoid this, you can use a common naming convention for your controls by prefixing them with their data type.

You can read more here: User Form object naming conventions

Hope Helpful

Dave
 
Upvote 0
Hi

If you are posting your record to an Excel Table then the code needs a different approach

See if this update does what you want

Rich (BB code):
Private Sub cmdSave_Click()
    Dim tblMaster     As ListObject
    Dim NewRecord   As ListRow
 
    Set tblMaster = ThisWorkbook.Worksheets("Master").ListObjects(1)
 
    'Add New row to the table
    Set NewRecord = tblMaster.ListRows.Add(AlwaysInsert:=True)
 
     With NewRecord
        .Range(1).Value = Me.ID.Value
        .Range(2).Value = Me.txtName.Value
        .Range(3).Value = Me.Gender.Value
        .Range(4).Value = Me.Address.Value
        .Range(5).Value = Me.ContactNo.Value
        .Range(6).Value = Me.Department.Value
    End With

    MsgBox "New Record Added To Table", 64, "Record Added"

End Sub

Note that I have renamed your control Name to txtName.

Name object is a member of the Names collection for the Application, Workbook, and Worksheet objects and using it as a control name may give rise to conflicts – to avoid this, you can use a common naming convention for your controls by prefixing them with their data type.

You can read more here: User Form object naming conventions

Hope Helpful

Dave
1684757950237.png



Error Showing This ..................................... Not working this
 
Upvote 0
That is different to code I posted & means cannot find a control with that name

Rich (BB code):
With NewRecord
        .Range(1).Value = Me.ID.Value
        .Range(2).Value = Me.txtName.Value
        .Range(3).Value = Me.Gender.Value
        .Range(4).Value = Me.Address.Value
        .Range(5).Value = Me.ContactNo.Value
        .Range(6).Value = Me.Department.Value
    End With

If you have renamed any of the controls you will need to update your code accordingly

Dave
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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