Insert a subtotal when value in column changes

ijswalker

Registered User.
Local time
Today, 13:54
Joined
Jun 16, 2008
Messages
67
Hi

I have a set of data in a spreadsheet that I want to insert a 2 blank lines and then subtotal the data each time there is a change in value in column F. I then want to grand total at the bottom. I would like to be able to do this using VBA as the subtotal function is not appropriate for my needs.

Can someone help?

Thanks

Ian
 
Well you want to use sheetchange event of the workbook with an if statement so it only fires when column f is changed.

Code:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

if target.column = 6 then
   
   code goes here

end if


End Sub
 
Hi

Here is the code I have. This is from a previous post and is what I really want it to do in order to insert lines but it does not seem to work.

Is there something missing?

Code:
Sub InsertRows()
Sheets("MainViewTest").Select ' adjust sheet   name as necessary
For lRow = lLastRow To 2 Step -1  'Work from last row up to row 2
    If Cells(lRow, 9) <> Cells(lRow - 1, 9) Then
    Rows(lRow).Insert
    End If
Next lRow
End Sub

Thanks

Ian
 
Where is the value for 'lLastRow' coming from?
 
Hi,

I think that is the issue. I'll see if I can go from here.

Thanks

Ian
 
WEll to get a value for lLastRow I would use:

Code:
lLastRow = thisworkbook.worksheets("MainViewTest").Range("F65536").end(xlup).row
 
Thanks Chergh,

That works. Do you have any insight into how I would then add subtotals using VBA?

Thanks

Ian
 
Yeah just sum between the empty cells pretty much. I can think of plenty of ways to do this but nothing particularly elegant might need to leave this till the morning when my brain hasn't ground to a halt.
 

Users who are viewing this thread

Back
Top Bottom