Code Problem

zyhin

Board Regular
Joined
Oct 8, 2009
Messages
78
Hi All,

I have this code I am trying to get to work. I have a time sheet for all employees and want to automate the process a bit.

On Worksheet "Employee" I have 3 columns(Employee Number(A), Employee Name(B), Area(C). I am trying to fill in the time sheet with each employee and print it out then move to the next one...and so on.

Code:
Sub RunPrint()
    
    Dim Employee As Range
    Dim EmployeeName As String
    Dim WeekStart As Date
    Dim WeekEnd As Date
    Dim Area As String
    
    WeekStart = InputBox("Please enter week beginning date. **/**/**")
    WeekEnd = WeekStart + 6
    
    For Each Employee In Worksheets("Employee").Range("A2:A65536")
    
        With Employee
        
            If Employee = True Then
                
                If Range(Employee, 3) = "P/C" Then
                    
                    Worksheets("TimeSheet").Cells("N1") = ("EMPLOYEE NAME: " & EmployeeName & "   " & EmployeeNumber)
                    Worksheets("TimeSheet").Cells("A3") = ("WEEK BEGINNING: " & WeekStart)
                    Worksheets("TimeSheet").Cells("N3") = ("WEEK ENDING: " & WeekEnd & "  " & Area)
                    Worksheets("TimeSheet").PrintOut Copies:=2
                Else
                    Worksheets("TimeSheet").Cells("N1") = ("EMPLOYEE NAME: " & EmployeeName & "   " & EmployeeNumber)
                    Worksheets("TimeSheet").Cells("A3") = ("WEEK BEGINNING: " & WeekStart)
                    Worksheets("TimeSheet").Cells("N3") = ("WEEK ENDING: " & WeekEnd & "  " & Area)
                    Worksheets("TimeSheet").PrintOut Copies:=1
                End If
                
            End If
        
        End With
        
    Next Employee
        
   
End Sub

Any help would be great. Z
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
The first problem is getting the WeekStart from an InputBox.

An InputBox returns a value of data type string. So if you want to add six to get the WeekEnd you need to convert the InputBox value into a date.

I haven't done any validation on the user input other than wrap an error trap around it.

Code:
    [color=green]'validate date input[/color]
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
        tempDate = InputBox("Please enter week beginning date. **/**/**")
        WeekStart = Format([COLOR="Red"]CDate[/COLOR](tempDate), "dd/mm/yy")
        WeekEnd = Format(WeekStart + 6, "dd/mm/yy")
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0 [color=green]'report errors as normal[/color]

The code then loops through the Employee's until it reaches an empty cell.

Code:
    [color=green]'loop through employees[/color]
    [color=darkblue]Set[/color] rngEmp = Sheets("Employee").Range("A2")
    [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngEmp = ""

It then populate the TimeSheet and prints out according to the value of the AREA. Then move to the next record.


The full code is shown below. I have commented out the Print statements for testing.

Code:
[color=darkblue]Sub[/color] RunPrint()
    [color=darkblue]Dim[/color] rngEmp [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] WeekStart [color=darkblue]As[/color] [color=darkblue]Date[/color]
    [color=darkblue]Dim[/color] WeekEnd [color=darkblue]As[/color] [color=darkblue]Date[/color]
    [color=darkblue]Dim[/color] tempDate [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] empName [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] empNum [color=darkblue]As[/color] [color=darkblue]String[/color]
    [color=darkblue]Dim[/color] empArea [color=darkblue]As[/color] [color=darkblue]String[/color]
    
    [color=green]'validate date input[/color]
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
        tempDate = InputBox("Please enter week beginning date. **/**/**")
        WeekStart = Format(CDate(tempDate), "dd/mm/yy")
        WeekEnd = Format(WeekStart + 6, "dd/mm/yy")
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0 [color=green]'report errors as normal[/color]
        
    
    [color=green]'loop through employees[/color]
    [color=darkblue]Set[/color] rngEmp = Sheets("Employee").Range("A2")
    [color=darkblue]Do[/color] [color=darkblue]Until[/color] rngEmp = ""
        
        [color=green]'==================[/color]
        [color=green]'Populate TimeSheet[/color]
        [color=green]'==================[/color]
        empNum = rngEmp.Value
        empName = rngEmp.Offset(, 1).Value
        empArea = rngEmp.Offset(, 2).Value
        [color=darkblue]With[/color] Sheets("TimeSheet")
            .Range("N2").Value = "EMPLOYEE NAME: " & empName & "    " & empNum
            .Range("A3").Value = "WEEK BEGINNING: " & WeekStart
            .Range("N3").Value = "WEEK ENDING: " & WeekEnd & "   " & empArea
        [color=darkblue]End[/color] [color=darkblue]With[/color]
        
        [color=green]'============[/color]
        'Print out
        [color=green]'============[/color]
        [color=darkblue]If[/color] UCase(rngEmp.Offset(, 2).Value) = "P/C" [color=darkblue]Then[/color]
            [color=green]'.PrintOut Copies:=2[/color]
        [color=darkblue]Else[/color]
            [color=green]'.PrintOut Copies:=1[/color]
        [color=darkblue]End[/color] [color=darkblue]If[/color]
        
        [color=green]'next employee[/color]
        [color=darkblue]Set[/color] rngEmp = rngEmp.Offset(1, 0)
    [color=darkblue]Loop[/color]
    
End [color=darkblue]Sub[/color]

PS Move the END WITH to after the PRINT OUT
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,560
Messages
6,179,520
Members
452,921
Latest member
BBQKING

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