Computing.Net > Forums > Office Software > Excel Macro Nightmare!!!

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Excel Macro Nightmare!!!

Reply to Message Icon

Name: ELGordo
Date: July 29, 2003 at 09:35:59 Pacific
OS: Win 2000
CPU/Ram: Good Question
Comment:

I have a problem with a Macro i am writing... I will explain what it does to give you an understanding of my problem. First of all it opens two text files and reads the values into column B and then column C. Then it draws a graph based on the data in column A, B & C, then I had to write some code that looks for the start and the end of a certain event within each data set (or column) when it has found the start and end points, it calculates the Average of all the values in between... my problem is that i NEVER get the same result as when Excel does it with the Average function, I thought it could be because my data contans negative values and because of adding them it was subtracting them, i fixed that but its still doing it..... i am at a loss, i am a pretty experienced VB type guy and i wont let this macro beat me!!!



Sponsored Link
Ads by Google

Response Number 1
Name: Taxi
Date: July 29, 2003 at 11:29:51 Pacific
Reply:

Average functiun takes all values except blank and not numbers values maybe some datas are not numbers recognized... its an arithmetic average


0

Response Number 2
Name: An Uncertainty H
Date: July 29, 2003 at 13:57:46 Pacific
Reply:

...er, why wouldn't you want the negative values to be added (ie. absolute values subtracted) to calculate your average...?

average of 4, 6, -1 = 3 in my textbook


0

Response Number 3
Name: ElGordo
Date: July 30, 2003 at 00:43:03 Pacific
Reply:

Because the values i am averaging is Electrical Current, so if it is a negative value i change it to positive with the ABS(value) function. i need to add ALL the values in a positive fashion!!!


0

Response Number 4
Name: A Certain TH
Date: July 30, 2003 at 03:41:59 Pacific
Reply:

Ahhhh - electrical stuff. Not really my thing.

However, I'll still give your problem a go:

Why not use an array formula? These can often be the unsung hero of the spreadsheet. Working their magic, day in, day out, they provide lovely answers where mere formulae stagger and falter.

Try =AVERAGE(ABS(myRange)) and instead of just hitting Enter, hit CTRL+SHIFT+Enter.

The problem here is that blanks in the range are treated as zero - but, if you want to get around that, then "Hey Presto!" - an ARRAY FORMULA comes to your aid!!!!!!! (Can you tell I'm bored here at work, yet?)

Try: =SUM(ABS(MyRange))/COUNTA(MyRange) and again, get busy with the CTRL+SHIFT+Enter.


Hope that helps. The VB coding for entering array formulae is straightforward, too - I'll let you look it up yourself!

Cheers
Tom


0

Response Number 5
Name: ElGordo
Date: July 30, 2003 at 05:24:11 Pacific
Reply:

Yes, i have another problem... the data i use is read from a text file and since its always different i have coded the Macro to search for the start and end of a certain pattern of data... ie a range and so i have to build a cell reference for column B like this ("B" & Start : "B" & End) where Start and End are the "number" of the cell row i dont think Excel allows me to enter a range built like that...also what data type is MyRange as i believe i need to use the "Set" keyword to assign a range to MyRange....when i try that i get an error.....i dont think i have explained myself very well so please ask if/when you get confused!!!


0

Related Posts

See More



Response Number 6
Name: A Certain TH
Date: July 30, 2003 at 06:36:29 Pacific
Reply:

OK - we're getting a bit confused here. "MyRange" is just a variable placeholder - put in whatever you feel like, or even define a range as MyRange if you want. BUT REMEMBER: this is an example of the SPREADSHEET FUNCTION, and not the relevant VBA.

If you want to refer to ranges using VBA, then I find the following the best way to do it:

Rather than identify cell "A1" as Range("A1"), try Cells(1, 1). When you are happy with that, move onto a range, as defined like this:

Range(Cells(1, 1), Cells(3, 5))

So, you can see that to identify a block of any dimension is as easy as it could possibly be! Just remember that the "co-ords" work in terms of (y, x) [ie. Row, Column] - but also remember that these are not co-ordinates, but indexes.]

That said - you can still refer to the range as you specified above. Just make sure the colon goes INSIDE the quotes! [ie Range("B" & Start & ":B" & End) You are building a STRING reference to a range this way, so treat it like one!]


I hope this isn't getting too confusing.

If it is, here's a cheat snippet of code:


Sub ExampleMacro()
Dim MyRange As String

MyRange = Cells(5, 2).Address & ":" & Cells(9, 2).Address

Cells(1, 1).FormulaArray = "=SUM(ABS(" & MyRange & "))/COUNTA(" & MyRange & ")"
End Sub

This will put the average of absolutes in range B5:B9 in cell A1. It does it by defining the range as a string, then using that string to complete the text for the function.

Tom


0

Response Number 7
Name: ElGordo
Date: July 30, 2003 at 09:36:58 Pacific
Reply:

Ok, you are helping a LOT here.....but i have this problem now!!!
Here is the code i am having problems with

Dim myrange19 As String

myrange19 = Cells(burststart19, 2).Address & ":" & Cells(burstend19, 2).Address

Cells(9, 6).FormulaArray = "=SUM(ABS(" & myrange19 & "))/COUNTA(" & myrange19 & ")"

where BurstStart19 = 52
BurstEnd19 = 200


so that the range i have built is "B52:B200"

when i run the Macro i get an "Application Defined or Object Defined Error" I have probably made a mistake with the line somewhere... i just dont know anymore!!! :'(


0

Response Number 8
Name: A Certain TH
Date: July 30, 2003 at 13:51:13 Pacific
Reply:

Hmm... interesting.

I get no error when I use your exact code from above. I, of course, have to move the definition of BurstStart19 and BurstEnd19 to come before the definition of MyRange19 - but I presume you already have these defined.

1) How are you defining BurstStart/End19 - as integers? Wherever they get their value in your code, are you sure that you have spelled them exactly correctly? (Sorry if this sounds a bit "low-brow"!)
2) By using the number 19, are you expecting to be able to refer to data arrays? (ie burstStart1, BurstStart2, etc) [If so, you haven't done it correctly]

This is EXACTLY what I have running under Excel 2000:

Sub tester()
Dim myrange19 As String
BurstStart19 = 52
BurstEnd19 = 200
myrange19 = Cells(BurstStart19, 2).Address & ":" & Cells(BurstEnd19, 2).Address

Cells(9, 6).FormulaArray = "=SUM(ABS(" & myrange19 & "))/COUNTA(" & myrange19 & ")"

End Sub

And I put a couple of numbers into the range - and sure enough, it works...


Post back - I'm intrigued!!


Tom


0

Response Number 9
Name: ElGordo
Date: July 31, 2003 at 01:41:22 Pacific
Reply:

WOW!! yeah it works...but not in my main program, i think the problem may be that i have defined Burststart19 & Burstend19 as integers and i am trying to concatenate them by using & when i am building the cell reference etc, I still have a lot of work to do to try and get this working, you have gave me a lot of ideas and i thank you, i am busy with other stuff so i will get round to this later and i will let you know how it goes!! its easier if you mail me GHepburn@Sendo.com.....cheers!!


0

Response Number 10
Name: A Certain TH
Date: July 31, 2003 at 04:06:40 Pacific
Reply:

Glad to have helped.

On the concatenation - you shouldn't have a problem using "&" with integers!

Cheers
Tom


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Office Software Forum Home


Sponsored links

Ads by Google


Results for: Excel Macro Nightmare!!!

Run Excel Macro as Automated Task on Server www.computing.net/answers/office/run-excel-macro-as-automated-task-on-server/9661.html

Excel Macro read and process CSV www.computing.net/answers/office/excel-macro-read-and-process-csv/3820.html

Excel macro www.computing.net/answers/office/excel-macro/4051.html