Macro - Adding new row with data from userform + formula

cherrydee

New Member
Joined
Jan 1, 2017
Messages
13
Hi, I would like to create a macro where a new row will be inserted and the fields "name", "date_joined", and "Capital" are being pulled out from a userform (the code is also in the "command_button" of the said userform.
This is quite easy. But...there are 4 other columns that contain purely formulas.
Code:
[COLOR=#333333]<code style="margin: 0px; padding: 0px; font-style: inherit; font-weight: inherit; line-height: 12px;">Private Sub cmd_add_Click()
If txtname.Text = "" Or txtdate.Text = "" Or txtcapital.Text = "" Then
MsgBox "Some Fields Missing", vbOKOnly, "Error"
Else
ThisWorkbook.Sheets("Members").Activate
Dim lRow As Long
Dim ws As Worksheet
Set ws = Worksheets("Members")
R = ActiveCell.Row
'ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
'Cells(ActiveCell.Row + 1, 2) = "=" & Cells(Active  Cell.Row, 2).Address & "+1"
LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
        ws.Range("A" & LastRow).Value = ""
        ws.Range("B" & LastRow).Value = Me.txtname.Value
        ws.Range("C" & LastRow).Value = Me.txtdate.Value
        ws.Range("D" & LastRow).Value = Me.txtcapital.Value
        'ws.Range("E" & R + 1 & ":G" & R + 1).Formula = ws.Range("E" & R & ":G" & R).Formula
        'ws.Range("E" & R & ":G" & R).Copy Destination:=ws.Range("E" & R + 1 & ":R" & R + 1)
        ws.Range("E" & LastRow + 1).Value = ws.Range("E" & ActiveCell.Row).Formula
End If</code>[/COLOR]
As you can see, I've tried some codes from the internet hence the commented codes but they're pretty messed up.
Also, I am running the macro from an 'index' sheet so I can't use activecell

Thanks in advance!
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
try:

Code:
Private Sub cmd_add_Click()
    If txtname.Text = "" Or txtdate.Text = "" Or txtcapital.Text = "" Then
    MsgBox "Some Fields Missing", vbOKOnly, "Error"
    Else
    ThisWorkbook.Sheets("Members").Activate
    Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Members")
    r = ActiveCell.Row
    'ActiveCell.Offset(1).EntireRow.Insert Shift:=xlDown
    'Cells(ActiveCell.Row + 1, 2) = "=" & Cells(Active  Cell.Row, 2).Address & "+1"
    LastRow = ws.Range("B" & Rows.Count).End(xlUp).Row + 1
            ws.Range("A" & LastRow).Value = ""
            ws.Range("B" & LastRow).Value = Me.txtname.Value
            ws.Range("C" & LastRow).Value = Me.txtdate.Value
            ws.Range("D" & LastRow).Value = Me.txtcapital.Value
            'ws.Range("E" & R + 1 & ":G" & R + 1).Formula = ws.Range("E" & R & ":G" & R).Formula
            'ws.Range("E" & R & ":G" & R).Copy Destination:=ws.Range("E" & R + 1 & ":R" & R + 1)
            'ws.Range("E" & LastRow + 1).Value = ws.Range("E" & ActiveCell.Row).Formula
            ws.Range("E" & r & ":G" & r).AutoFill Destination:=Range("E" & r & ":G" & r + 1), Type:=xlFillDefault
    End If
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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