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))
 
Looks like it works. I realized we are using the 5th column to find last row. I will change it to 1st because it will always have a date inputted from userform.

I will continue and see where I can go from here.

Thank you
 
Upvote 0

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Looks like it works. I realized we are using the 5th column to find last row

Except it has been so long since I last used it, I forgot that SpecialCells(xlLastCell) can't be restricted to a column (I normally avoid it) so stick to either the Find method or Range("A" & Rows.count).End(xlUp).
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,599
Messages
6,120,453
Members
448,967
Latest member
grijken

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