Consulting

Results 1 to 3 of 3

Thread: Request Help for this Simple Macro

  1. #1

    Cool Request Help for this Simple Macro

    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!
    Attached Images Attached Images

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Beginner1, Welcome to VBAExpress.

    That is a homework question and we don't do homework. See our FAQ.

    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.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •