• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 186
  • 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
NorieVBA ExpertCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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