Fluid VBA macro?

samilynn

Board Regular
Joined
Jun 24, 2003
Messages
166
Office Version
  1. 2016
Platform
  1. Windows
I'm working on a macro with a lot of steps for a file that needs to be recreated on a regular basis.

Is it possible to have the macro modify the range in the formula each time, based on how many rows are populated in the file? (the spreadsheet doesn't always have 738 rows of data).

The formula gives the total quantity per product, without having to use Subtotal.

Thank you!
Samantha


Excel 2010 64 bit
ABCDEF
1PRODWHSEQTYCOSTTOTAL QTY
2AB5314Whse: 15,000$1.9515,000IF(COUNTIF($A$2:A2,A2)=1,SUMIF($A$2:$A$738,A2,$C$2:$C$738),"")
3AB5314Whse: 210,000$1.97 
4SQ5577Whse: 129$317.4341
5SQ5577Whse: 212$317.43 
QTY
Cell Formulas
RangeFormula
E2=IF(COUNTIF($A$2:A2,A2)=1,SUMIF($A$2:$A$738,A2,$C$2:$C$738),"")
E3=IF(COUNTIF($A$2:A3,A3)=1,SUMIF($A$2:$A$738,A3,$C$2:$C$738),"")
E4=IF(COUNTIF($A$2:A4,A4)=1,SUMIF($A$2:$A$738,A4,$C$2:$C$738),"")
E5=IF(COUNTIF($A$2:A5,A5)=1,SUMIF($A$2:$A$738,A5,$C$2:$C$738),"")
 
I have been completely overlooking the .Value=.Value code line... all this time I thought the goal was to load up Column E with formulas. Since the goal is to place values in the cells, I think this code might be (noticeably?) faster...
Code:
[table="width: 500"]
[tr]
	[td]Sub samilynn2()
  Dim R As Long, I As Variant, Data As Variant, QtyRow As Variant
  Data = Range("A2", Cells(Rows.Count, "A").End(xlUp).Offset(, 2))
  With CreateObject("Scripting.Dictionary")
    For R = 1 To UBound(Data)
      If IsEmpty(.Item(Data(R, 1))) Then .Item(Data(R, 1)) = "/" & R + 1
      .Item(Data(R, 1)) = Val(.Item(Data(R, 1))) + Data(R, 3) & Mid(.Item(Data(R, 1)), InStrRev(.Item(Data(R, 1)), "/"))
    Next
    For Each I In .Items
      QtyRow = Split(I, "/")
      Cells(QtyRow(1), "E").Value = QtyRow(0)
    Next
  End With
End Sub[/td]
[/tr]
[/table]
@Fluff,

If you get the chance to test it, I would be curious how the above code fared speedwise in comparison to the other codes.
 
Last edited:
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
That is a lot faster ~ quarter of a second.
 
Upvote 0
That is a lot faster ~ quarter of a second.
Yes! I knew it would be faster!!!! :LOL:

Seriously, though, I thought the combination of an internal VB array coupled with the Dictionary object (that is, processing everything in memory) would have produced a better result than that.
 
Upvote 0
I think you may have misunderstood, reading it again it's slightly ambiguous.
It took ~.25 seconds, rather than being .25 seconds quicker.
 
Upvote 0
I think you may have misunderstood, reading it again it's slightly ambiguous.
It took ~.25 seconds, rather than being .25 seconds quicker.
And here I thought you were being facetious. Okay, so then I was right, executing almost entirely in memory was a lot faster. Hopefully the OP comes back to this thread to see this.
 
Upvote 0

Forum statistics

Threads
1,215,047
Messages
6,122,858
Members
449,096
Latest member
Erald

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