Add Formula With Userform

Trueblue862

Board Regular
Joined
May 24, 2020
Messages
160
Office Version
  1. 365
Platform
  1. Windows
Hi, I have this code which adds values to a range of data from a userform. Is there a way to get it to also add formulas to Columns F, I, J, L, M, and N? I have tried copy and paste codes which really don't seem to play nice with the rest of the data in the sheet. I have the following formulas in the sheet:

Column F: =IFERROR((E3/C3),"")
Column I: =((C3*D3)+G3)-H3
Column J: =IFERROR((E3/C3),"")
Column L: =IFERROR((K3-J3),"")
Column M: =IFERROR(H3*L3,"")
Column N: =IFERROR(((M3)-(I3*J3)),"")

All of these formulas are from row 3, I need them to be input into the sheet in the correct columns and in such a way they will become dynamic in the sheet and adjust to their new location when the sheet sorts alphabetically.

VBA Code:
Private Sub StoreValues()
    Dim rw As Integer
    Dim ws As Worksheet
   
    Set ws = Worksheets("Order Sheet")
    rw = ws.Cells.Find(what:="*", searchorder:=xlRows, Searchdirection:=xlPrevious, LookIn:=xlValues).Row + 1

    ws.Cells(rw, 1).Value = Me.TbxProd.Value
    ws.Cells(rw, 2).Value = Me.CbxUnit.Value
   
End Sub

I really appreciate any help with this.
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
Try this:
VBA Code:
Private Sub StoreValues()
    Const cstrMk As String = "~"
    Dim rw As Long
    Dim strRw As String
    Dim ws As Worksheet

    Set ws = Worksheets("Order Sheet")
    With ws
        rw = .Cells.Find(What:="*", SearchOrder:=xlRows, SearchDirection:=xlPrevious, LookIn:=xlValues).Row + 1

        .Cells(rw, "A").Value = Me.TbxProd.Value
        .Cells(rw, "B").Value = Me.CbxUnit.Value
        strRw = Format(rw)
        .Cells(rw, "F").Formula = Replace("=IfError((E~ / C~), """")", cstrMk, strRw)
        .Cells(rw, "I").Formula = Replace("=((C~ * D~) + G~) - H~", cstrMk, strRw)
        .Cells(rw, "J").Formula = Replace("=IfError((E~ / C~), """")", cstrMk, strRw)
        .Cells(rw, "L").Formula = Replace("=IfError((K~ - J~), """")", cstrMk, strRw)
        .Cells(rw, "M").Formula = Replace("=IfError(H~ * L~, """")", cstrMk, strRw)
        .Cells(rw, "N").Formula = Replace("=IfError((M~ - (I~ * J~)), """")", cstrMk, strRw)

    End With
End Sub
There are other ways to approach this, but I think this way is the easiest to maintain.
 
Upvote 0

Forum statistics

Threads
1,214,878
Messages
6,122,062
Members
449,064
Latest member
scottdog129

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