VBA for adding formulas to specific Columns when a new row is added.

Jstump

New Member
Joined
Oct 25, 2023
Messages
36
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have formulas set up in coulmn 9-15 (I-O). On this sheet "hours" a row is either added or deleted depending on if data is entered or deleted off of another selection on another sheet "Input sheet". Im trying to make it where a new row is added the formulas are already there so i do not have to manually imput them every time. The only cells where data is entered are in yellow. Here are the 2 sheets i am working off of for reference. Ive also attached the code I have so far just for the last column.

Any help would be amazing!!


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim newRow As Long

    Set ws = ThisWorkbook.Sheets("Hours")

  
    If Target.Column = 1 Then
        
        If Target.Rows.Count > 1 Then
            Application.EnableEvents = False

            
            newRow = 6

            
            ws.Cells(newRow, 15).FormulaArray = "=FILTER(Database!$C$2:$C$10, Database!$A$2:$A$10=A" & newRow & ", """")"

            
            Application.EnableEvents = True
        End If
    End If
End Sub
Kane Macro copy 1.xlsm
ABCDEFGHIJKLMNO
1
2
3
414-Jan15-Jan16-Jan17-Jan18-Jan19-Jan20-JanStraight TotalOT TotalStraight $OT $Weekly TotalTotal
5
6Stump, James101010101010140211461.541151$2,612.5061.036.538
7Total Straight Hrs#REF!
8Total OT HRs#REF!
9Total Weekly HRS#REF!
10Total weekly cost#REF!
Hours
Cell Formulas
RangeFormula
I6I6=IF(N6>40,40,N6)
J6J6=IF(N6>40,N6-40,0)
K6K6=I6*O6
L6L6=(O6*1.5)*J6
M6M6=SUM(K6:L6)
N6N6=SUM(B6:H6)
O6O6=FILTER(Database!$C$2:$C$10,Database!$A$2:$A$10=A6,"")
B7B7=N61
B8B8=O61
B9B9=S61
B10B10=R61*1.22


Kane Macro copy 1.xlsm
OP
3Employee Employee name
41Stump, James
52
63
74
85
96
107
118
129
1310
1411
1512
1613
1714
1815
1916
2017
2118
2219
2320
Input page
Cells with Data Validation
CellAllowCriteria
P4:P23List=Database!$A$2:$A$10
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
This is what i have now and it is not working. I'm Lost. the first one is for when a new row is added in just row 6 and the second one iswhen a row is added in gerenal.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Hours")

    If Target.Column = 1 And Target.Rows.Count > 1 Then
        Application.EnableEvents = False
        ws.Cells(6, 9).Formula = "=IF(N6>40,40,N6)"
        ws.Cells(6, 10).Formula = "=IF(N6>40,N6-40,0)"
        ws.Cells(6, 11).Formula = "=I6*O6"
        ws.Cells(6, 12).Formula = "=(O6*1.5)*J6"
        ws.Cells(6, 13).Formula = "=SUM(K6:L6)"
        ws.Cells(6, 14).Formula = "=SUM(B6:H6)"
        ws.Cells(6, 15).FormulaArray = "=FILTER(Database!$C$2:$C$10,Database!$A$2:$A$10=A6,"""")"
        Application.EnableEvents = True
    End If
End Sub
[/CODE]
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim newRow As Long
    Dim formulaRange As Range

    Set ws = ThisWorkbook.Sheets("Hours")

    If Target.Column = 1 Then
        If Target.Rows.Count > 1 Then
            Application.EnableEvents = False
            newRow = Target.Cells(1, 1).Row

            Set formulaRange = ws.Range(ws.Cells(newRow, 9), ws.Cells(newRow, 15))

            formulaRange.Formula = Array( _
                "=IF(N" & newRow & ">40,40,N" & newRow & ")", _
                "=IF(N" & newRow & ">40,N" & newRow & "-40,0)", _
                "=I" & newRow & "*O" & newRow, _
                "=(O" & newRow & "*1.5)*J" & newRow, _
                "=SUM(K" & newRow & ":L" & newRow & ")", _
                "=SUM(B" & newRow & ":H" & newRow & ")", _
                "=FILTER(Database!$C$2:$C$10,Database!$A$2:$A$10=A" & newRow & ","""")" _
                )

            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 0
CAn anyone help witht this?? it is still not working.
Upon testing your code, it does indeed do nothing as far as one can tell.

When exactly is the code supposed to run? And what row should be populated with the formulas once it has run?
 
Upvote 0
It is suppose to run automatically when a row is added to the "Hours" sheet and the formulas are populated into the new row in columns 9-15 (I-O).
 
Upvote 0
It is suppose to run automatically when a row is added to the "Hours" sheet and the formulas are populated into the new row in columns 9-15 (I-O).
Okay, it does seem to be working, but only if you have more than one cell selected when you make any changes. Inserting a row does not satisfy your condition of "Target.Rows.Count > 1" unless you select multiple cells in column A and right click the sheet to insert. If you right click the left side of the sheet to insert an entire row, the code does not do what you want. If you change the condition to count Columns instead of Rows, I think it might work as intended.

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim ws As Worksheet
    Dim newRow As Long
    Dim formulaRange As Range

    Set ws = ThisWorkbook.Sheets("Hours")

    If Target.Column = 1 Then
        If Target.Columns.Count > 1 Then
            Application.EnableEvents = False
            newRow = Target.Cells(1, 1).Row

            Set formulaRange = ws.Range(ws.Cells(newRow, 9), ws.Cells(newRow, 15))

            formulaRange.Formula = Array( _
                "=IF(N" & newRow & ">40,40,N" & newRow & ")", _
                "=IF(N" & newRow & ">40,N" & newRow & "-40,0)", _
                "=I" & newRow & "*O" & newRow, _
                "=(O" & newRow & "*1.5)*J" & newRow, _
                "=SUM(K" & newRow & ":L" & newRow & ")", _
                "=SUM(B" & newRow & ":H" & newRow & ")", _
                "=FILTER(Database!$C$2:$C$10,Database!$A$2:$A$10=A" & newRow & ","""")" _
                )

            Application.EnableEvents = True
        End If
    End If
End Sub
 
Upvote 1
Solution

Forum statistics

Threads
1,223,888
Messages
6,175,203
Members
452,617
Latest member
Narendra Babu D

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