Solved

would like to creat a relatively easy macro in excell

Posted on 2013-12-31
1
356 Views
Last Modified: 2014-01-29
i have 5 columns of numbers listed in excell

would like to create a macro that gets the count of numbers in the first column

the remaining columns, i would like to calculate the average of each column

tried to record a macro to do this many times and it didnt work. One of the problems it wouldnt work was because the number of column entries will vary from spreadsheet to spreadsheet. The macro I created kept the column count the same.

If this doesnt explain the problem well enough, i can send a file
0
Comment
Question by:radrick60
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 9

Accepted Solution

by:
dustock earned 500 total points
ID: 39748594
With the description you gave I tried to come up with a solution.  It may not be the best way to do it, but it seemed to work for my testing.

You didn't say what you wanted to do with the count and the averages so I just set this to put that data on the last row of each line with bold text and a pink cell color with your values

Go to the Developer Tab and click Visual Basic, double click "ThisWorkbook" and past the following code.  Then try to run the "Calculate" macro and see if you get the desired results.

Also in the future, please add Microsoft Excel to the Topics portion so that the Excel Experts can assist.

Dim cAverage As Double

Sub Calculate()
  'LastRow for column A
  ALastRow = Sheets(1).Cells(Sheets(1).Rows.Count, "A").End(xlUp).Row
  
  Dim rRng As Range
  'If you have a header row change to A1 to A2
  Set rRng = Range("A1:A" & ALastRow)
  
  Sheets(1).Cells(ALastRow + 1, "A").Value = ALastRow
  'Set font to bold
  Sheets(1).Cells(ALastRow + 1, "A").Font.Bold = True
  'Set cell color
  Sheets(1).Cells(ALastRow + 1, "A").Interior.ColorIndex = 22
  
  'For Columns B to E
  For i = 2 To 5
  'Last Row for column i
    LastRow = Sheets(1).Cells(Sheets(1).Rows.Count, i).End(xlUp).Row
        CellAverage (Sheets(1).Range(Cells(1, i), Cells(LastRow, i)))
        Sheets(1).Cells(LastRow + 1, i).Value = cAverage
        'Set font to bold
        Sheets(1).Cells(LastRow + 1, i).Font.Bold = True
        'Set cell color
        Sheets(1).Cells(LastRow + 1, i).Interior.ColorIndex = 22
    Next i
End Sub

Sub CellAverage(rng As Range)
    cAverage = 0
    cAverage = Application.WorksheetFunction.Average(rng)
End Sub

Open in new window

0

Featured Post

Plug and play, no additional software required!

The ATEN UE3310 USB3.1 Gen1 Extender Cable allows users to extend the distance between the computer and USB devices up to 10 m (33 ft). The UE3310 is a high-quality, cost-effective solution for professional environments such as hospitals, factories and business facilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I was prompted to write this article after the recent World-Wide Ransomware outbreak. For years now, System Administrators around the world have used the excuse of "Waiting a Bit" before applying Security Patch Updates. This type of reasoning to me …
An introduction to the wonderful sport of Scam Baiting.  Learn how to help fight scammers by beating them at their own game. This great pass time helps the world, while providing an endless source of entertainment. Enjoy!
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…
Suggested Courses

630 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