Using class in place of array

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Last edited:

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Change the minus to a plus in my code. If that doesn't work for you, then you'll need to give us some more to go on
 
Upvote 0
Change the minus to a plus in my code. If that doesn't work for you, then you'll need to give us some more to go on


Assume I have two columns of data:

Code:
Name Oranges
a        1
a        2
a        1
b        2
b        3
b        4
c        3
c        4
c        3
d        2
e        6
f        5

Suppose I want to add up the Oranges, according to the Name. This will do it.

Code:
Option Explicit
Sub Start()
    Dim DataArray() As Variant
    
    DataArray = Cells(1, 1).CurrentRegion.Value
    
    Dim DataArrayRows As Integer
    
    DataArrayRows = UBound(DataArray(), 1)
    
    Dim Total As Double
    
    Dim Counter As Integer
    
    Dim j As Integer
    
    For Counter = 2 To DataArrayRows - 1
        
        j = Counter
        
        Do Until DataArray(j, 1) <> DataArray(j + 1, 1)
        
            Total = Total + DataArray(j, 2)
            
            j = j + 1
        
        Loop
            
            Total = Total + DataArray(j, 2)
            
            Cells(j, 3).Value = Total
            
            Total = 0
            
            Counter = j
            
    Next Counter
End Sub


But I want to incorporate classes as in your previous example.

Code:
Sub UsingClass()
    Dim DataArray() As Variant
    
    DataArray = Cells(1, 1).CurrentRegion.Value
    
    Dim DataArrayRows As Integer
    
    DataArrayRows = UBound(DataArray(), 1)
    
    Dim Total As Double
    
    Dim Counter As Integer
    
    Dim j As Integer
    
    Dim MyClass(1 To 13) As Class1
    
    For Counter = 2 To DataArrayRows - 1
        
        j = Counter
            
        Set MyClass(Counter) = New Class1
        
        Do Until MyClass(Counter).Name <> MyClass(Counter + 1).Name
        
            Total = Total + DataArray(j, 2)
            
            j = j + 1
        
        Loop
            
            Total = Total + DataArray(j, 2)
            
            Cells(j, 3).Value = Total
            
            Total = 0
            
            Counter = j
            
    Next Counter
End Sub

The above does NOT work. Seems you need to add:

Code:
Set MyClass(Counter+1)=New Class

Even then, the difficulty lies on this line:

Code:
Do Until DataArray(j, 1) <> DataArray(j + 1, 1)

Thanks
 
Last edited:
Upvote 0
It looks like you've cross-posted this, in which case you should know the rules by now. Links, please.
 
Upvote 0

Forum statistics

Threads
1,215,375
Messages
6,124,592
Members
449,174
Latest member
chandan4057

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top