VBA Insert a formula in a cell

NDMDRB

Board Regular
Joined
Jun 20, 2016
Messages
164
Office Version
  1. 2016
Platform
  1. Windows
Hello,

I have a userform to add items to sheet1 and I'm using the code below,
and inserting manually formulas to columns ("C" "E" "F" "G")

Code:
Private Sub cmdAdd_Click()Dim ws As Worksheet
Dim MsgBoxResult As Long
Set ws = Sheet1
nr = ws.Cells(Rows.Count, 1).End(xlUp).Row + 1
ws.Cells(nr, 1) = CDbl(Me.txtBarCode)
ws.Cells(nr, 2) = Me.txtName
ws.Cells(nr, 4) = CDbl(Me.txtSellPrice)
MsgBoxResult = MsgBox("New Item has been added" & vbCrLf & vbCrLf & "Do you wanna add more?", vbYesNo)
If MsgBoxResult = vbNo Then
Unload Me
ElseIf MsgBoxResult = vbYes Then
txtBarCode = ""
txtName = ""
txtSellPrice = ""
Else
End If
End Sub



What I'm looking for is to insert the following formulas to:
  • ws.Cells(nr, 3) / =IFERROR(AVERAGEIF(Purchase_BarCode,A2,Purchase_Cost),"0")
  • ws.Cells(nr, 5) / =F2-G2
  • ws.Cells(nr, 6) / =SUMIF(Purchase_BarCode,A2,Purchase_Qty)
  • ws.Cells(nr, 7) / =SUMIF(Sales_BarCode,A2,Sales_Qty)

The above formulas reference to row 2, but I need them to be in the next available row

ABCDEFG
1Bar CodeNameCostSell PriceQtyPurchase QtySales Qty
2
3
4

<tbody>
</tbody>
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try this

Code:
    Dim f As String
    f = "=IFERROR(AVERAGEIF(Purchase_BarCode,A" & nr & ",Purchase_Cost),""0"")"
    ws.Cells(nr, 3).Formula = f
    f = "=F" & nr & "-G" & nr
    ws.Cells(nr, 5).Formula = f
    f = "=SUMIF(Purchase_BarCode,A" & nr & ",Purchase_Qty)"
    ws.Cells(nr, 6).Formula = f
    f = "=SUMIF(Sales_BarCode,A" & nr & ",Sales_Qty)"
    ws.Cells(nr, 7).Formula = f
 
Upvote 0
thank you so much Yongle this works very well

I have another inquiry in another userform, if you may help me

I have the following textboxes that calculate some values

txtAmount has a calculation which I have the code for it and all the below text boxes as well, unless the "txtPaid"

I need "txtPaid" to be the same as the "txtTotal" but to be able to change it's value
ex: if txtTotal.value = 100, then, txtPaid.value = 100, and then I can change the value of the txtPaid if I need


Code:
Private Sub txtDiscount_Change()Call txtTotal_Change
End Sub

Private Sub txtTotal_Change()
Me.txtTotal = Me.txtAmount - Me.txtDiscount
Call txtPaid_Change
Call txtRest_Change
End Sub


Private Sub txtPaid_Change()
Me.txtPaid.Text = Me.txtTotal 'I've tried this but I can't change the value
Call txtRest_Change
End Sub


Private Sub txtRest_Change()
Me.txtRest = Me.txtTotal - Me.txtPaid
End Sub
 
Upvote 0
Threads should stick to the original topic
- so I suggest you begin a new thread for that question
 
Upvote 0

Forum statistics

Threads
1,213,557
Messages
6,114,293
Members
448,564
Latest member
ED38

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