VBA: Add formula to the last used Column, Create Subtotal

shane_aldrich

Board Regular
Joined
Oct 23, 2009
Messages
148
This is gonna be hard to verbalize, but I'll do my best.

Starting in Cell A4 I have a list of unique sales codes, there are duplicates, but it's already sorted.
There can be varying columns. I'm more or less trying to create subtotals w/o using the actual subtotal functionality.

The sub below finds the last used column, increments up by 1, and places it row 4. That formulas is more a less a T/F flag when the dealer code changes.

First, it's supposed to be referecning $A4, but it acutally reads RC1 in excel, not just VBA (not sure why)

Right now FH is the last used column (164)
so it should place the formula in FI4

FI4 should read =IF(NOT(UPPER(TRIM($A4))=UPPER(TRIM(OFFSET($A4,1,0)))),1,0)

Now the tricky part. I need to increment up one more column and place....

FJ4=IF(FI4=0,FJ3+B4,B4)

So...B4 is the second column, and after I do my T/F formula after the last used column, the next column should add the values starting in the second column...that formulas would carry to the right until there is a running total for every column used...
so since I am going to 164 now, the subtotals would carry to the 327

I think the thing I'm stuggeling with is not so much the VBA, but writing a flexible formula that can account for fluxuations in the number of columns.

I'm pretty sure how to get it to fill in correctly something like, I just can't wrap my mind around the formula


Dim lColFill As Long
lColFill = lLastCol+lLastCol
Range = Cells(4,lLastCol+2),(lLastRow,lColFill))




Sub Combine_SCPD()

'*************************
Dim lCol As Long
Dim lLastRow As Long
Dim lLastCol As Long
Dim wsSRC As Worksheet
Dim wsIMP As Worksheet
Dim wsDST As Worksheet
'*********************
Set wsIMP = Sheets("Import")

wsIMP.Activate
lLastRow = Last(1, Columns(1))
lLastCol = Last(2, Rows(1))

Cells(4, lLastCol + 1).Formula = "=IF(NOT(UPPER(TRIM(RC1))=UPPER(TRIM(OFFSET(RC1,1,0)))),1,0)"

End Sub
 

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I figured it out, posting the solution I came up with in case it's useful. Right now, the code is using a sheet named import. I have 4 headers rows, but that could be DIM and set as well to add flexibility.

Your table has to be sorted by Column 1...for example

Sample Info:

Name</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>
Name</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>
Name</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
John Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Jane Doe</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Barney Bo</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Barney Bo</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>
Barney Bo</SPAN>1</SPAN>2</SPAN>3</SPAN>4</SPAN>5</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL span=5></COLGROUP>

Sub Test()
'*************************
'*** Declare Variables ***
'*************************
Dim lCol As Long
Dim lLastRow As Long
Dim lLastCol As Long
Dim wsIMP As Worksheet
'*********************
'*** Set Variables ***
'*********************
Set wsIMP = Sheets("Import")
'===================
'= Subtotal Import =
'===================
'Set Variables
wsIMP.Activate
lLastRow = Last(1, Columns(1))
lLastCol = Last(2, Rows(1))
'Dealer Code Change
Range(Cells(4, lLastCol + 1), Cells(lLastRow, lLastCol + 1)).FormulaR1C1 = "=IF(NOT(UPPER(TRIM(RC1))=UPPER(TRIM(OFFSET(RC1,1,0)))),1,0)"
'Subtotals
Range(Cells(4, lLastCol + 2), Cells(lLastRow, lLastCol + lLastCol)).FormulaR1C1 = "=IF(RC1=R[-1]C1,R[-1]C+OFFSET(RC,0,-COUNTA(R1)),OFFSET(RC,0,-COUNTA(R1)))"
'Copy Paste Values
Range(Cells(4, lLastCol + 1), Cells(lLastRow, lLastCol + lLastCol)).Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
'Move Values
Range(Cells(4, lLastCol + 2), Cells(lLastRow, lLastCol + lLastCol)).Select
Selection.Cut
Cells(4, 2).Select
ActiveSheet.Paste
'Sort
Set rngSORT = Range(Cells(4, lLastCol + 1), Cells(lLastRow, lLastCol + 1))
wsIMP.Sort.SortFields.Clear
wsIMP.Sort.SortFields.Add Key:=rngSORT, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With wsIMP.Sort
.SetRange Range(Cells(4, 1), Cells(lLastRow, lLastCol + 1))
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
'Apply AutoFilter
Rows("3:3").Select
wsIMP.UsedRange.AutoFilter Field:=lLastCol + 1, Criteria1:="0"
'Delete AutoFilter Results
wsIMP.UsedRange.Offset(3, 0).Resize(wsIMP.UsedRange.Rows.Count - 1).Rows.Delete
Selection.AutoFilter
'Delete Last Column
Columns(lLastCol + 1).Delete Shift:=xlLeft

End Sub

Results =

Name</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>Header Row 1</SPAN>
Name</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>Header Row 2</SPAN>
Name</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>Header Row 3</SPAN>
John Doe</SPAN>15</SPAN>30</SPAN>45</SPAN>60</SPAN>75</SPAN>
Jane Doe</SPAN>8</SPAN>16</SPAN>24</SPAN>32</SPAN>40</SPAN>
Barney Bo</SPAN>3</SPAN>6</SPAN>9</SPAN>12</SPAN>15</SPAN>

<TBODY>
</TBODY><COLGROUP><COL><COL span=5></COLGROUP>
 
Upvote 0

Forum statistics

Threads
1,203,491
Messages
6,055,727
Members
444,814
Latest member
AutomateDifficulty

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