Select rows of data and total some columns

windwardmi

Board Regular
Joined
Oct 18, 2009
Messages
138
I have the following VBA code that works most of the way:
Code:
Sub TimeSheet()
'
' TimeSheet Macro
'
' Keyboard Shortcut: Ctrl+t
'

Dim WS As Worksheet
Dim rightsheet As String
Dim StartDate As Date, EndDate As Date

StartDate = Application.InputBox("Start Date?")
EndDate = Application.InputBox("End Date?")
'ActiveSheet.PageSetup.CenterHeader = "&12" & rightsheet & Chr(10) & ActiveSheet.Name & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)
ActiveSheet.PageSetup.RightHeader = "&12" & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)

rightsheet = Application.InputBox("Employee Last Name?")
 
Sheets(rightsheet).Select

ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(StartDate), _
    Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)

   
Sub TimeSheet()
'
' TimeSheet Macro
'
' Keyboard Shortcut: Ctrl+t
'

Dim WS As Worksheet
Dim rightsheet As String
Dim StartDate As Date, EndDate As Date

StartDate = Application.InputBox("Start Date?")
EndDate = Application.InputBox("End Date?")
'ActiveSheet.PageSetup.CenterHeader = "&12" & rightsheet & Chr(10) & ActiveSheet.Name & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)
ActiveSheet.PageSetup.RightHeader = "&12" & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)

rightsheet = Application.InputBox("Employee Last Name?")
 
Sheets(rightsheet).Select

ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(StartDate), _
    Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)

    Range("E65536").End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("I2:I" & Cells.SpecialCells(xlLastCell).Row))
    Range("j65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("j2:j" & Cells.SpecialCells(xlLastCell).Row))
 
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
'IgnorePrintAreas:=False
End Sub

This part of the code does not work. Why is that? It works by itself in a separate procedure. What am I missing.
Code:
    Range("E65536").End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("I2:I" & Cells.SpecialCells(xlLastCell).Row))
    Range("j65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("j2:j" & Cells.SpecialCells(xlLastCell).Row))
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Ok, if I activate the .PrintOut in the code it allows the following lines of code to work but the total is the total worksheet columns not just the rows in the range of the requested start and end dates. How do I get the total of only the rows requested?
Code:
Range("E65536").End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("I2:I" & Cells.SpecialCells(xlLastCell).Row))
    Range("j65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("j2:j" & Cells.SpecialCells(xlLastCell).Row))
 
Upvote 0
Deleted as the OP has posted more info.
 
Last edited:
Upvote 0
How do I get the total of only the rows requested?

If you mean only the visible cells then use SUBTOTAL rather than SUM with 109 as the subtotal criteria.
 
Last edited:
Upvote 0
Yes I had a pasting error. Past should have been following:
Code:
Sub TimeSheet()
'
' TimeSheet Macro
'
' Keyboard Shortcut: Ctrl+t
'

Dim WS As Worksheet
Dim rightsheet As String
Dim StartDate As Date, EndDate As Date

StartDate = Application.InputBox("Start Date?")
EndDate = Application.InputBox("End Date?")
'ActiveSheet.PageSetup.CenterHeader = "&12" & rightsheet & Chr(10) & ActiveSheet.Name & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)
ActiveSheet.PageSetup.RightHeader = "&12" & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)

rightsheet = Application.InputBox("Employee Last Name?")
 
Sheets(rightsheet).Select

ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(StartDate), _
    Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)
    Range("E65536").End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("I2:I" & Cells.SpecialCells(xlLastCell).Row))
    Range("j65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("j2:j" & Cells.SpecialCells(xlLastCell).Row))
 
'ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
'IgnorePrintAreas:=False
End Sub

In this form the following does not create a sum line of the columns requested. If I activate ActiveWindow.SelectedSheets.PrintOut we do get a sum line of the columns requested but it is the total or sum of the original sheet not that of the requested dates that are printing.
Code:
Range("I65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("I2:I" & Cells.SpecialCells(xlLastCell).Row))    Range("j65536").End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Sum(Range("j2:j" & Cells.SpecialCells(xlLastCell).Row))
 
Upvote 0
Code:
    Range("E" & Rows.Count).End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I" & Rows.Count).End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Subtotal(109, Range("I2:I" & Range("I" & Rows.Count).End(xlUp).Row))
    Range("J" & Rows.Count).End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Subtotal(109, Range("J2:J" & Range("J" & Rows.Count).End(xlUp).Row))
 
Upvote 0
When pasted in the code it does not give me a subtotal line(2nd row below requested dates) when filtered. I unfilter and the subtotal line is their corrupting original data.
 
Upvote 0
Post the code as you have it as there is nothing in the code I posted that puts the result in any different position to the code you posted (unless your data goes past line 65536).
 
Upvote 0
Code as follows: By the way enjoy the coming of the New Year
Code:
Sub TimeSheet()
'
' TimeSheet Macro
'
' Keyboard Shortcut: Ctrl+t
'

Dim WS As Worksheet
Dim rightsheet As String
Dim StartDate As Date, EndDate As Date

StartDate = Application.InputBox("Start Date?")
EndDate = Application.InputBox("End Date?")
'ActiveSheet.PageSetup.CenterHeader = "&12" & rightsheet & Chr(10) & ActiveSheet.Name & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)
ActiveSheet.PageSetup.RightHeader = "&12" & Chr(13) & rightsheet & Chr(10) & (StartDate) & " To " & (EndDate)

rightsheet = Application.InputBox("Employee Last Name?")
 
Sheets(rightsheet).Select

ActiveSheet.Range("$A$1").CurrentRegion.AutoFilter Field:=1, Criteria1:=">=" & CLng(StartDate), _
    Operator:=xlAnd, Criteria2:="<=" & CLng(EndDate)
    Range("E" & Rows.Count).End(xlUp).Offset(2, 0).Value = "TOTAL"
    
    Range("I" & Rows.Count).End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Subtotal(109, Range("I2:I" & Range("I" & Rows.Count).End(xlUp).Row))
    Range("J" & Rows.Count).End(xlUp).Offset(2, 0).Value = _
        WorksheetFunction.Subtotal(109, Range("J2:J" & Range("J" & Rows.Count).End(xlUp).Row))

ActiveWindow.SelectedSheets.PrintOut Copies:=1, Collate:=True, _
IgnorePrintAreas:=False
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,186
Members
449,071
Latest member
cdnMech

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