Dynamic forumulas using VBA

brs_prs

New Member
Joined
Mar 18, 2009
Messages
4
Hi,


I need to calculate sum and other math calculations in a sheet. The rows are not always the same.I recorded a macro to see how the code looks like but it only takes the current cell. How to make it dynamic.

The code I tried is below:

Code:
Sub Sum_per_format()
    Columns("Y:Y").Select
    Selection.Insert Shift:=xlToRight
    Range("Y1").Select
    Selection.NumberFormat = "General"
    ActiveCell.FormulaR1C1 = "Sum"
    Range("Y2").Select
    ActiveCell.FormulaR1C1 = "=SUM(RC[-1]:R[38166]C[-1])"
    Range("X3").Select
    Range("Z1").Select
    Selection.NumberFormat = "General"
    ActiveCell.FormulaR1C1 = "Percent"
    Range("Z2").Select
    ActiveCell.FormulaR1C1 = _
        "=1-(SUM(RC[-3]:R[38166]C[-3])/SUM(RC[-4]:R[38166]C[-4]))"
    Range("Z3").Select
    Range("Z2").Select
    Selection.NumberFormat = "0%"
    Rows("1:1").Select
    Range("M1").Activate
    Selection.Font.Bold = True
    Range("Y2:Z2").Select
    Selection.Font.Bold = True
    With Selection.Font
        .Name = "Arial"
        .Size = 10
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    With Selection.Font
        .Name = "Arial"
        .Size = 8
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Cells.Select
    Range("M1").Activate
    With Selection.Font
        .Name = "Arial"
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    With Selection.Font
        .Name = "Arial"
        .Size = 8
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With
    Range("A1").Select
End Sub

Am calculating the sum of a column and posting the result in another cell.
Rows are dynamic. How to achieve it?
Any small help is greatly apapreciated.
 
Last edited by a moderator:

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.

Forum statistics

Threads
1,191,374
Messages
5,986,274
Members
440,016
Latest member
vasanrajeswaran

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
Top