473,396 Members | 2,147 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,396 software developers and data experts.

In Excel can VBA be used to make an Array to sum device quanities

In Excel using VBA, how can I sum the following up to device types, quantities and then put the data on a seperate tab in a specific cell range

Expand|Select|Wrap|Line Numbers
  1. Device     Qty     Amps
  2. Device A    1    0.060
  3. Device B    1    0.092
  4. Device A    1    0.060
  5.  
  6. Device B    1    0.092
  7. Device B    1    0.092
  8. Device C    1    0.025
  9. Device C    1    0.025
  10.  
  11. Device D    1    0.089
  12. Device A    1    0.060
  13. Device B    1    0.092
  14.  
  15.  
  16. For Example
  17. Device     Qty     Amps
  18. Device A    3    0.060
  19. Device B    4    0.092
  20. Device C    2    0.025
  21. Device D    1    0.089
  22.  
Any help or direction is greatly appreciated.
Apr 13 '11 #1
3 1848
Oralloy
988 Expert 512MB
Have you tried a pivot table?

Perhaps you can use a macro to structure your data and copy it to another sheet. Then you can run a pivot table on your sanitized data.
Apr 13 '11 #2
MikeTheBike
639 Expert 512MB
Hi

I don't know anything about where on the sheet or on which sheet the data is or on which sheet you want to write the result to.

I also assume that you do not have a fix number/type of devices which may permit the use of SUMIF() or SUMPRODUCT() worksheet function.

That being the case then I would probable tackle it this way
Expand|Select|Wrap|Line Numbers
  1. Option Explicit
  2. Type Device_Type
  3.     Name As String
  4.     Quantity As Integer
  5.     Amps As Single
  6.     TotalQuantity As Integer
  7. End Type
  8.  
  9. Dim Devices() As Device_Type
  10.  
  11. Sub TestCode()
  12.     Dim ThisDevice As Device_Type
  13.     Dim i As Long
  14.     Dim TopRow As Long
  15.     Dim BotRow As Long
  16.  
  17.     'SET ROW RANGE TO SEARCH (ASSUME ACTIVE SHEET)
  18.     TopRow = 3
  19.     BotRow = 14
  20.  
  21.     'INITIALIZE ARRAY
  22.     ReDim Devices(0)
  23.     Devices(0).TotalQuantity = 0
  24.  
  25.     For i = TopRow To BotRow
  26.         If Cells(i, 1) <> "" Then
  27.             With ThisDevice
  28.                 .Name = Trim(Cells(i, 1))
  29.                 .Quantity = CInt(Cells(i, 2))
  30.                 .Amps = CSng(Cells(i, 3))
  31.             End With
  32.             AddDevice ThisDevice
  33.         End If
  34.     Next i
  35.  
  36.     'OUTPUT RESULTS (OR WRITE TO REQUIRED SHEET)
  37.     For i = 0 To UBound(Devices)
  38.         With Devices(i)
  39.             Debug.Print .Name & " " & .TotalQuantity & " " & .Amps
  40.         End With
  41.     Next i
  42.  
  43. End Sub
  44.  
  45. Sub AddDevice(ByRef ADevice As Device_Type)
  46.     Dim i As Integer
  47.     'CHECK FIRST ARRAY ELEMENT HAS BEEN USED, IF NOT THEN USE IT
  48.     If Devices(0).Name = "" Then
  49.         Devices(0) = ADevice
  50.         Devices(0).TotalQuantity = ADevice.Quantity
  51.         Exit Sub
  52.     Else
  53.         'FIND REQUIRED DEVICE
  54.         For i = 0 To UBound(Devices)
  55.             With Devices(i)
  56.                 If .Name = ADevice.Name Then
  57.                     .TotalQuantity = .TotalQuantity + .Quantity
  58.                     Exit Sub
  59.                 End If
  60.             End With
  61.         Next i
  62.  
  63.         'DEVICE NOT FOUND SO ADD TO ARRAY
  64.         ReDim Preserve Devices(UBound(Devices) + 1)
  65.         Devices(UBound(Devices)) = ADevice
  66.         Devices(UBound(Devices)).TotalQuantity = ADevice.Quantity
  67.     End If
  68. End Sub
  69.  
As I say you will need to resolve the rows columns and sheet references.

The other thing not clear is whether the quantity could be greater that one, and if so whether this should be included in the total count (I assumed so in the code).

HTH

MTB
Apr 14 '11 #3
Thanks for your comments and help. I will try them both and see which works best for the need
Apr 16 '11 #4

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

Similar topics

5
by: LRW | last post by:
I did a Web search, and a deja.com search on this...and I'm finding how to make checkboxes act like radiobuttons, and other interesting behaviors, but nothing that quite answers my question. If...
5
by: Denis Perelyubskiy | last post by:
Hello, I need to make an array of elements accross forms. My javascript skills, as evident from this question, are rather rudimentary. I tried to make an associative array and index it with...
8
by: Gustaf Liljegren | last post by:
I'd like a simple way of extracting everything that's not whitespace on each line in a file. For example, if the program encounters this line 1 2\t\t3 \t\t4 I want a string array like this ...
0
by: CR | last post by:
Hi, how do I get some data in an Excel Workbook imported into an array within my C# program? Are there any easy ways to do this? Is there a better approach than saving the cells to an array?...
2
by: George | last post by:
Is there a fast way to transfer an Excel range to an array? Example: Excel range is E2:E300 Dim person() as string Thanks, George
3
by: George | last post by:
Sub ExcelToListBox() Dim xRange As Object Dim ary Dim xValue As String xRange = oXL.Range("A1:A9") 'has letters A-H ary = xRange.value xValue = ary(3, 1) 'xValue = C...
9
by: Steve | last post by:
Hello, I created a structure ABC and an array of type ABC Public Structure ABC Dim str1 As String Dim int1 As Integer End Structure Public ABC1 As New ABC, ABC2 As New ABC
22
by: bela | last post by:
Hello, I am very new to JAVA. I would like to know, how to read *.csv file from java and how to save that data into an array. regards, bela
3
by: raylopez99 | last post by:
I suspect the answer to this question is that it's impossible, but how do I make the below code work, at the point where it breaks (marked below). See error CS0411 This is the complete code. ...
8
by: Sunny | last post by:
Hi, Can someone tell me, How to redefine array or make array empty or null. Here what I am trying to do. var temp = new Array(); for(i=0; i <=outstring.length-1; i++) { temp =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.