VBA - ranges -

heathball

Board Regular
Joined
Apr 6, 2017
Messages
119
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>
 
well - the result is false?

which means its not blank?

which is a bit of a mystery to me?
 
Upvote 0

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
great - that 2nd code works fantastic!

Such a short code but such a time saver!

Its like magic!

i will attempt to use this within other macros

thanks for your time.

and thanks for the tip on the non-blank cells, i ran a 'delete leading and trailing spaces' and now they are blank. i might include that in my macro.

thanks for your great help!
 
Last edited:
Upvote 0
with a code like this

is it possible to have a reference to columns

like if my 'range-array- column is 'M'

and the two calculation columns are "Y" and "Z"

and i would like the result to run down column "AA"

Is there a way to do that? - where i can change the columns to suit the need?

thanks
 
Upvote 0
Not really, as the size of the array will vary each time, as will the columns in the array that you need to reference.
This will look at cols M, Y:Z & output in AA
Code:
Sub GetDifference()
   Dim ary As Variant
   Dim i As Long
   
   ary = Range("M1", Range("M" & Rows.count).End(xlUp).Offset(, 14))
   With CreateObject("scripting.dictionary")
      For i = 1 To UBound(ary)
         .Item(ary(i, 1)) = .Item(ary(i, 1)) + val(ary(i, 13)) - val(ary(i, 14))
      Next i
      For i = 1 To UBound(ary)
         ary(i, 15) = .Item(ary(i, 1))
      Next i
      Range("AA1").Resize(UBound(ary)).Value = Application.Index(ary, , 15)
   End With
End Sub
 
Upvote 0
that works perfectly for 40,000+ rows

thanks very much

I'm trying to work out the details....

I know it makes sense, and i will get to understand it eventually....

From what you say, i may have to ensure that these columns are always used for the calculation.


Cheers:)
 
Upvote 0
You can make it easier to change by using this
Code:
Sub GetDifference()
   Dim i As Long
   
   With CreateObject("scripting.dictionary")
      For i = 2 To Range("C" & Rows.count).End(xlUp).Row
         .Item(Range("C" & i).Value) = .Item(Range("C" & i).Value) + val(Range("D" & i).Value) - val(Range("F" & i).Value)
      Next i
      For i = 2 To Range("C" & Rows.count).End(xlUp).Row
         Range("H" & i).Value = .Item(Range("C" & i).Value)
      Next i
   End With
End Sub
BUT on 40,000+ rows it will take longer
 
Upvote 0

Forum statistics

Threads
1,216,131
Messages
6,129,066
Members
449,485
Latest member
greggy

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