PDA

View Full Version : Request Help for this Simple Macro



Beginner1
09-21-2013, 05:42 PM
Hi Experts!

I need your help to write this simple macro. This macro performs two tasks: inserting new columns and perform arithmetic additions. I have 10x4 table. it contains names in the first column, and numbers in the next 10 columns.


0) my (10x4) table is started at column C of Sheet2 (two) of a Workbook. The first column contains people name. The next column (column D, which I call col1) and other following columns contain numbers. This table initially has 10 columns, I label them as col1, col2, col3, … col10)


1) Please tell me how to write an excel macro that performs several tasks below:
























2) first, inserts a blank column between col3 and col4, put a label "sum1" on the new column. (the first column of this table is started column C. it contains text data like a, b, c, d…


2) then add numbers in first row of col1, col2, and col3, place the sum in the corresponding cell of the newly created column "sum1"



3) then continue to perform the addition for the next rows of col1, col2, and col3…, the table has only 4 rows.





















4) now the table has 11 columns, similarly insert a blank column between col8 and col9, call it "sum2"






5) then sum of two numbers in the first row of col7 and col8, then go on to the next row….























6) similarly, insert a new column between col10 and col11, call it "sum3", then copy the content of col10 to this newly created column. The end!


















7) all these tasks are started by a click on "Start" button, which is placed alone in the Sheet1 of this Workbook

I attached screen capture of the table for your reference.

Thank you much for your help!

Aussiebear
09-21-2013, 06:36 PM
This is an easy task to learn. Since you already know the steps you need to undertake, turn on the macro recorder and undertake each task as you want, then stop the recorder and have a look at the code. The code in itself made look a little confusing, however you then to need to upload it to here by clicking on the Go Advanced button, scroll down to Manage Attachments and follow the prompts from there. We'll have a look at your code and amend where necessary. This is a great way to learn and understand code.

SamT
09-21-2013, 07:23 PM
Beginner1, Welcome to VBAExpress.

That is a homework question and we don't do homework. See our FAQ (http://www.vbaexpress.com/forum/faq.php).

We will help you figure out HOW TO on your own.

Copy these words into an other Sheet's code page and put the cursor in or next to each word and press F1.

Columns
Insert 'Range
Range
Cells
Offset
Sum
Long
String
For '...Next and For... each
Variant
Constant
Variable
Dim

When you have learned all you can from those, copy this code snippet into Sheet2's code page.

Option Explicit

Sub Test()
Dim Cel As Range 'An Object Variable
Dim i As Long 'i,j,&k are standard loop counters
Const ColSum1 As Long = n 'You must assign the column number for each of these Contants
Const ColSUm2 As Long = n
Const ColSum3 As Long = n

Columns(ColSum1).Insert
For i = 0 to 4
Cells(i, ColSum1) = Sum Range(Cells(i, n), Cells(i, n)
Next i
'Question: Why was I able to use the same number, ColSum1, for both the Insert and the " =Sum..."?
'What is the "i" standing in for?

End Sub

There are at least two easy errors in that code. Place the cursor inside the code and repeatedly press F8 to Step Thru the code.
When you get that bit to work properly, come on back and we'll teach you how to do all the Sum Columns in one more loop.