ozymandias11
ozymandias11

Reputation: 45

Excel Macro for invoices

So there is this vendor that sends me spreadsheets every week for their invoices. In the name colum if it equals "BCR Plaza" i would want excel to automatically add another row below that, copy some of the data from the previous row and divide the total in the original row by 2. I already have a macro for all of this. What i cant figure out is how to tell excel that after all of the above actions are executed to go back to the original total and divide that by too as well and replace it with the result.

this is the macro that i currently have now :

Sub BlankLine()

Dim Rng As Range
Dim WorkRng As Range
Dim Name As String
Dim Memo As String
Dim dn As Variant
Dim dt As Variant
Dim Total As Variant

On Error Resume Next
xTitleId = "Add New Row"
Set WorkRng = Application.Selection
Set WorkRng = Cells.Select
Set WorkRng = WorkRng.Columns(1)
xLastRow = WorkRng.Rows.Count
Application.ScreenUpdating = False
For xRowIndex = xLastRow To 1 Step -1
    Set Rng = WorkRng.Range("A" & xRowIndex)
    If Rng.Value = "BCR Plaza" Then
            dt = Range("B" & xRowIndex).Value
            dn = Range("D" & xRowIndex).Value + 0.5
            Memo = Range("C" & xRowIndex).Value
            Total = (Range("I" & xRowIndex).Value) / 2
        Rng.Offset(1, 0).EntireRow.Insert Shift:=xlDown
            Range("A" & xRowIndex + 1) = "Billing"
            Range("D" & xRowIndex + 1) = dn
            Range("B" & xRowIndex + 1) = dt
            Range("C" & xRowIndex + 1) = Memo
            Range("I" & xRowIndex + 1) = Total

    End If
Next
Application.ScreenUpdating = True
End Sub

Original File enter image description here

After Macro enter image description here

Upvotes: 3

Views: 408

Answers (1)

Subodh Tiwari sktneer
Subodh Tiwari sktneer

Reputation: 9976

Give this a try...

Sub BlankLine()
Dim Memo As String
Dim dn As Variant
Dim dt As Variant
Dim Total As Variant
Dim xRowIndex As Long, xLastRow As Long

Application.ScreenUpdating = False
xLastRow = Cells(Rows.Count, 1).End(xlUp).Row
For xRowIndex = xLastRow To 2 Step -1
    If Cells(xRowIndex, 1) = "BCR Plaza" Then
        dt = Range("B" & xRowIndex).Value
        dn = Range("D" & xRowIndex).Value + 0.5
        Memo = Range("C" & xRowIndex).Value
        Total = (Range("I" & xRowIndex).Value) / 2
        Rows(xRowIndex + 1).Insert
        Range("A" & xRowIndex + 1) = "Billing"
        Range("D" & xRowIndex + 1) = dn
        Range("B" & xRowIndex + 1) = dt
        Range("C" & xRowIndex + 1) = Memo
        Range("I" & xRowIndex + 1) = Total
        Range("I" & xRowIndex) = Total
    End If
Next
Application.ScreenUpdating = True
End Sub

Upvotes: 1

Related Questions