Create an Excel Macro

Posted on 2011-10-12
Last Modified: 2012-05-12
I get several excel files which have the same format and the same number of columns ( 6 columns). The number of rows differ in each file.

I need to reformat these files in a specific way as follows -
Insert a text heading at the top of the list and make it bold
Delete the last column ( column 6). List now has 5 columns
Goto the last row of the data and delete the contents in Columns 3 and Column 4
Put text "Subtotal" in Column 4 of this row
Goto next row --> Put text "GST" in Column 4
Put formula in Column 5 ( 10% of the cell above)
Goto next Row --> Put text "Total" in Column 4
Put formula in Column 5 ( sum of the 2 cells above it)
Format the last row now as bold.

As I am new to Excel would somebody help me to do a macro to cover these tasks which are repetitive for all the files.
Question by:dlg
    1 Comment
    LVL 33

    Accepted Solution

    A good place to stary would be to turn on the macro recorder and do all these things manually.

    That won't give you the exact code you need but it'll give you a start.

    What you basically need is something like this.
    Dim ws As Worksheet
    Dim LastRow As Long
         ' Sheet1 is the worksheet the code will run on
        Set ws = Worksheets("Sheet1")
         ' delete column 6
        ws.Rows(1).EntireRow.Insert xlShiftDown    ' insert row for headings.
        ' put headers in first row.
        ws.Range("A1").Resize(, 5).Value = Split("Header1, Header2, Header3, Header4, Header5", ",")
        ' find last row of data.
        LastRow = ws.Range("A" & Rows.Count).End(xlUp).Row
        ' clear columns 3 and 4 in last row
        ws.Range("C" & LastRow).Resize(, 2).Clear
        ' add totals and formulas
        ws.Range("C" & LastRow) = "SubTotal"
        ws.Range("C" & LastRow + 1) = "GST"
        ws.Range("D" & LastRow + 1).FormulaR1C1 = "=0.1*R[-1]C"
        ws.Range("C" & LastRow + 2) = "Total"
        ws.Range("D" & LastRow + 2).FormulaR1C1 = "=R[-2]C+R[-1]C"

    Open in new window

    You would need code to open the workbooks, reference the correct worksheet etc.

    PS Don't you need

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Introduction This Article is a follow-up to my Mappit! Addin Article (, it was inspired by an email posting I made to EUSPRIG (, I will briefly cover: 1) An overvie…
    Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
    The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
    This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now