VBA - ranges -

heathball

Board Regular
Joined
Apr 6, 2017
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Im learning a little VBA, but I'm stuck with a range issue.

Im starting with columns A,B,C
and i want to achieve D

So my range is effectively in column A (each name change represents a new 'range' - and they are in alphabetical order down the page)
I'm currently using an 'average if' helper column in macros to help me with this, but the problem is i have big files (over 150,000 rows), and 'average if' takes a very long time (I do many of these during a macro)

within my code, im trying to use variations of 'dim' but I'm not getting anywhere.

Is there something i use with 'dim' to select column A, and use each name (each range begins with a name change moving down the column) as individual ranges, so that when i apply a function or sum in column D, and run it down the page in my macro, i can get the result as per the table below? then i can add this code ( i need this for many things, on different sheets and different workbooks, and if the range column "A" changes, i can change that reference in the code to suit the file )

I would like to use it with the various functions i use now ('if' 'and' 'or' etc) and I"m looking for something flexible that excludes specific object references to worksheet/column names etc.

Is something possible with this, or am i stuck with my slow average-if helper columns?

A NAMEB High
C LowD high minus low
barry10006
barry1000156
barry100096
barry200044
barry200084
barry30006
barry3000126
barry30006
barry30006
barry300066
barry40002617
barry4000917

<tbody>
</tbody>
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Whilst I don't understand what you are trying to do, this will give you the difference between col B & col C
Code:
Sub GetDifference()
   Dim ary As Variant
   Dim i As Long
   
   ary = Range("A2", Range("A" & Rows.count).End(xlUp).Offset(, 3))
   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(ary)
         .Item(ary(i, 1)) = .Item(ary(i, 1)) + ary(i, 2) - ary(i, 3)
      Next i
      For i = 1 To UBound(ary)
         ary(i, 4) = .Item(ary(i, 1))
      Next i
      Range("D2").Resize(UBound(ary)).Value = Application.Index(ary, , 4)
   End With
End Sub
 
Upvote 0
thanks for your reply. Perhaps my explanation was not very clear.

Your code works when i transfer the above table in my example to an excel sheet.

but when i use it for one job i have which looks like the table below ( there are 40,000 rows) ...i am trying to arrive at the (b-c) sum total in column D for each range in column A, ( i have added the result in column D) ...where column A is what i call the 'range' column, so each entry eg "alb_1050" is a range, and within each range, there will always only be one value in col B, and 1 value in column C, and the result of B-C will be on every row in col D, referring to the 'range' in col A.

Perhaps i have the wrong term when i use 'range' - I'm not sure how else to say that column A contains entries in alphabetical order, and each different name - i would like to be a separate 'range.'

the line in the code that is in debug yellow is .Item(ary(i, 1)) = .Item(ary(i, 1)) + ary(i, 2) - ary(i, 3)

I hope this helps and I"m looking online for the definition of 'ary' and 'ubound' ...very interesting stuff

ABCD
alb_1050-0.5620833331.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10501.3
alb_10500.7379166671.3
alb_1100-0.9288235291.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11001.62
alb_11000.6911764711.62

<colgroup><col><col><col><col></colgroup><tbody>
</tbody>
 
Upvote 0
What error message did you get?
 
Upvote 0
Do cols B:C contain formulae that return "" ?
 
Upvote 0
cols B:C contain no formulae, and are pasted values

in the macros I'm currently using and developing, i paste values for everything before each step.

i would like to sample and try whatever portion of code works within the vba code i use, and change the column references etc.
 
Upvote 0
so that means the cells that are empty are actually 'empty cells' and the values are non-formulae pasted values
 
Upvote 0
I suspect that either the cells are not blank, or the numbers are not numbers.
in col D put =isblank(B2) where B2 is a blank cell, what is the result?
 
Upvote 0
Maybe
Code:
Sub GetDifference()
   Dim ary As Variant
   Dim i As Long
   
   ary = Range("A1", Range("A" & Rows.count).End(xlUp).Offset(, 3))
   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(ary)
         .Item(ary(i, 1)) = .Item(ary(i, 1)) + val(ary(i, 2)) - val(ary(i, 3))
      Next i
      For i = 1 To UBound(ary)
         ary(i, 4) = .Item(ary(i, 1))
      Next i
      Range("D1").Resize(UBound(ary)).Value = Application.Index(ary, , 4)
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,688
Messages
6,126,209
Members
449,299
Latest member
KatieTrev

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