• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 182
  • Last Modified:

Create an Excel Macro

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.
1 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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now