473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Macro to build formula in the first empty cell

Hi,

I have a spreadsheet looks like this:

Expand|Select|Wrap|Line Numbers
  1. QID     C1   C2   C3   C4   %a    %b     %c   %d
  2. 13123   b    b    b    b    0%    100%   0%   0%
  3. 13124   d    d    d    d    0%    0%     0%   100% 
  4. 13125   d    d    d    d    0%    0%     0%   100%

QID is the question ID and C1 to C4 are the answers by students. Column "%a" is the percentage of "a" answered by students. I use countif function to calculate that.

Everytime I copy and paste a group of answers the number of questions ( number of rows) and number of answers (number of columns) will be different.
Is there any macro that will calculate (%a, %b, %c and %d) and insert the result in the first empty column?

Hope this is clear enough. Any answer will be appreciated!!

Thanks a lot!!!
Nov 6 '07 #1
3 1397
kadghar
1,295 Expert 1GB
...
Everytime I copy and paste a group of answers the number of questions ( number of rows) and number of answers (number of columns) will be different.
Is there any macro that will calculate (%a, %b, %c and %d) and insert the result in the first empty column?
...
Thanks a lot!!!
well there's for sure a macro that does that, the problem is to make it. Why dont you give it a try by yourself and answer a little bit more specific questions.

Use a couple of DO or FOR, use CELLS, may be writing the range into a variant should be good...

Check this little example, I hope it'll help you to figure out how to do what you want.

Expand|Select|Wrap|Line Numbers
  1. dim i as long
  2. dim dou1 as double
  3. dim a
  4. a = range(cells(2,2), cells(2,2).end(-4121))
  5.  
  6. for i = lbound(a) to ubound(a)
  7.     dou1=dou1+a(i,1)
  8. next
  9. i=1
  10. do
  11.     i=i+1
  12. loop until cells(2,i)=""
  13.  
  14. cells(1,1)=dou1 / (ubound(a)-lbound(a)+1)
  15.  
Nov 6 '07 #2
well there's for sure a macro that does that, the problem is to make it. Why dont you give it a try by yourself and answer a little bit more specific questions.

Use a couple of DO or FOR, use CELLS, may be writing the range into a variant should be good...

Check this little example, I hope it'll help you to figure out how to do what you want.

Expand|Select|Wrap|Line Numbers
  1. dim i as long
  2. dim dou1 as double
  3. dim a
  4. a = range(cells(2,2), cells(2,2).end(-4121))
  5.  
  6. for i = lbound(a) to ubound(a)
  7.     dou1=dou1+a(i,1)
  8. next
  9. i=1
  10. do
  11.     i=i+1
  12. loop until cells(2,i)=""
  13.  
  14. cells(1,1)=dou1 / (ubound(a)-lbound(a)+1)
  15.  

Thanks for reply.

I use the following macro trying to insert the formula but it wont allow me.

Is there anything wrong?

Expand|Select|Wrap|Line Numbers
  1. Sub InsertFomula()
  2. Set r = ActiveSheet.UsedRange
  3. 'Dim COUNTA As Integer
  4. nLastRow = r.Rows.Count + r.Row - 1
  5. nLastColumn = r.Columns.Count + r.Column - 1
  6. For i = 1 To nLastColumn
  7.   For j = 1 To nLastRow
  8.     If Cells(1, i).Value <> "*" Then
  9.       Cells(1, nLastColumn + 1).Formula = "=countif(Cells(1, nLastColumn + 1),"a"/nLastColumn"
  10.       Selection.AutoFill Destination:=Cells(1, nLastColumn + 1), Type:=xlFillDefault'
  11.       Cells(1, nLastColumn + 1).Select
  12.     End If
  13.   Next
  14. Next
  15. End Sub
Nov 7 '07 #3
kadghar
1,295 Expert 1GB
Thanks for reply.

I use the following macro trying to insert the formula but it wont allow me.

Is there anything wrong?
first of all, the cells you're changing have nothing to do with the range you defined. you're just changing cells on the active sheet. (i dont know if that's what you wanted)

And the formula you're writing has a few mistakes. try using FormulaRC instead.
Nov 7 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: nikhil kumar | last post by:
Hi all i am trying to develop a macro. the scenario is as follows,, we have 2 sheets 1 and 2. i want to apply a vlookup in sheet 1 and the lookup table is in sheet 2. but in sheet 1...
0
by: =?Utf-8?B?TGV0emRvXzF0?= | last post by:
I'd like to create a Macro that will sort some raw data, apprx 20k lines, remove some lines based upon a condition in a certain column. Then copy this data into a new spreadsheet and sort the ...
4
by: tonyP | last post by:
I am very Experiences in Excel but very new to VBA & I am struggling to convert an old XLM macro into Visual Basic The Macro helps you to compile the INDEX/MATCH formula (very similar to Vlookup...
4
by: bcr123 | last post by:
If someone could please help with the following: I have formula =COUNTIF(D3:D957, ">0") in cell N957 - If I copy/paste it N957 cell to empty N958 cell I get formula =COUNTIF(D4:D958, ">0"). ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.