Formulas that can find headers and be updated

justvba

New Member
Joined
Jan 6, 2017
Messages
41
Hi,

I need some help!! I am trying to put formulas in a few columns. Here's where the problem comes in, I am using variables to look for the column with the headers names.


Code:
Cells(5,dis).select
        Price= Application.Match("List Price", .Rows(1), 0)
        Cost= Application.Match("Cost Price", .Rows(1), 0)
        sale= Application.Match("SalePrice", .Rows(1), 0)
        dis= Application.Match("Discount", .Rows(1), 0)
        Item= Application.Match("Item", .Rows(1), 0)

 With ActiveSheet
        LastRow = .Cells(.Rows.Count, "Item").End(xlUp).Row
    End With


Activecell.formulaR1C1= "=(RC[-4]-RC[-3])/rc[-4]"
SELECTION.COPY

    Do until Activecell.row > lastrow
        activecell.offset(1,0).select
        activecell.paste
    loop
application.cutcopymode.false
       
    End Sub


So when the user adds columns everything gets messed up with the formula so then I tried to change this line

Activecell.formulaR1C1= "=(RC[-4]-RC[-3])/rc[-4]"
changed to
Activecell.formulaR1C1= "=((" & Price & " )-(" & Sale& " ))/(" & Price & " )"
but only gives me the number that the column is not the value in the cell

Can anyone help!!!
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Try this:

Code:
With ActiveSheet
    Price = Application.Match("List Price", .Rows(1), 0)
    Cost = Application.Match("Cost Price", .Rows(1), 0)
    Sale = Application.Match("Sale Price", .Rows(1), 0)
    Dis = Application.Match("Discount", .Rows(1), 0)
    Item = Application.Match("Item", .Rows(1), 0)
    lr = .Cells(.Rows.Count, Price).End(xlUp).Row
    If IsNumeric(Dis) And IsNumeric(Price) And IsNumeric(Sale) and lr > 4 Then
        With Range(.Cells(5, Dis), .Cells(lr, Dis))
            .FormulaR1C1 = "=IF(RC" & Price & "<>"""",(RC" & Price & "-RC" & Sale & ")/RC" & Price & ","""")"
        End With
    End If
End With
 
Last edited:
Upvote 0
I am getting an error on the formula Application defined or object defined error Found it sorry how do remove the $ on the column?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,432
Messages
6,124,858
Members
449,194
Latest member
HellScout

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