Add formulas using VB in Excel 2016

REV AJR

New Member
Joined
May 2, 2012
Messages
5
Hi

I have created a form in VB to enter data into a table in excel, part of the macro adds formulas to the relevant cells, it almost works but I cannot get the equation to pickup the row reference for that cell.


Any help would be appreciated
Code:
Private Sub cmdenter_Click()
'Copy input values to sheet.
    Dim eRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Data Sheet")
    eRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(eRow, 1).value = txtLiftnumber.value
        .Cells(eRow, 2).value = txtSystemnumber.value
        .Cells(eRow, 3).value = txtArrivaldate.value
        .Cells(eRow, 4).value = cmbRefernce.value
        .Cells(eRow, 5).value = txtPackagenumber.value
        .Cells(eRow, 7).Formula = "=IF(F1299="""",0,IF(LEFT(D1299,1)=""R"",(F1299-C1299),IF(((F1299-C1299)-7)<0,1,(F1299-C1299)-7)))"
        .Cells(eRow, 8).Formula = "=G1298*0.35"
        .Cells(eRow, 9).value = "=IF(A1298="""",0,IF(F1298="""",(INT($B$1-C1298)),0))"
        .Cells(eRow, 10).Formula = "=I1298*0.35"
        .Cells(eRow, 11).value = txtComments.value
    End With
    'Clear input controls.
    Me.txtLiftnumber.value = ""
    Me.txtSystemnumber.value = ""
    Me.txtArrivaldate.value = ""
    Me.cmbRefernce.value = ""
    Me.txtPackagenumber.value = ""
    Me.txtComments.value = ""
 
Last edited by a moderator:

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Dim eRowNext As Long
eRowNext = eRow + 1

Use this variable for your IF formula for column 7.

Example for 1 formula:
.Cells(eRow, 8).Formula = "=G" & eRow & "*0.35"
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,327
Members
452,635
Latest member
laura12345

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