I'm trying to take info from a list of employees on sheet 2 and fill in various cells on a timecard template on sheet 1. I want to use a SpinButton to scroll through an employee list. I'm trying to make my variables equal the value of a cell in a column but the row number changes with each press of the SpinButton. Making the variables work is my problem. See sample code.
Private Sub SpinButton2_Change()
' Create variables to hold employee info.
Dim Dept As String
Dim ENbr As Integer
Dim EName As String
Dim Acct1 As Integer
Dim Acct2 As Integer
Dim Acct3 As Integer
Dim Acct4 As Integer
Dim Acct5 As Integer
Dim Stdby As Integer
'Change active sheet to Employee List.
Sheets("Employee List").Select
' Select column D (the list of employee names.
ActiveSheet.Range("D1").Select
' Go to the last cell with data in the selected column.
Selection.End(xlDown).Select
' Create variable, make it equal to the selected cell's row number.
LastRow = (ActiveCell.Row)
If SpinButton2 > LastRow Then SpinButton2 = 1
' Create variable for row number of employee info.
' Identifies desired row number.
RowNum = SpinButton2 + 1
'Fill variables with info. I can't figure this part out.
Dept = Cells(RowNum, 2)
ENbr = Cells(RowNum, 3)
EName = Cells(RowNum, 4)
Acct1 = Cells(RowNum, 5)
Acct2 = Cells(RowNum, 6)
Acct3 = Cells(RowNum, 7)
Acct4 = Cells(RowNum, 8)
Acct5 = Cells(RowNum, 9)
Stdby = Cells(RowNum, 10)
'Change active sheet to TimeCard.
Sheets("TimeCard").Select
If SpinButton2 = 1 Then
Range("M1").Select
ActiveCell.FormulaR1C1 = Dept
Range("D3").Select
ActiveCell.FormulaR1C1 = ENbr
Range("M3").Select
ActiveCell.FormulaR1C1 = EName
Range("C5").Select
ActiveCell.FormulaR1C1 = Acct1
Range("I5").Select
ActiveCell.FormulaR1C1 = Acct2
Range("O5").Select
ActiveCell.FormulaR1C1 = Acct3
Range("T5").Select
ActiveCell.FormulaR1C1 = Acct4
Range("Y5").Select
ActiveCell.FormulaR1C1 = Acct5
Range("AD5").Select
ActiveCell.FormulaR1C1 = Stdby
End If
End Sub
This is how I want it to work but not knowing enough maybe I have the wrong approach. Any help will be greatly appreciated.
Private Sub SpinButton2_Change()
' Create variables to hold employee info.
Dim Dept As String
Dim ENbr As Integer
Dim EName As String
Dim Acct1 As Integer
Dim Acct2 As Integer
Dim Acct3 As Integer
Dim Acct4 As Integer
Dim Acct5 As Integer
Dim Stdby As Integer
'Change active sheet to Employee List.
Sheets("Employee List").Select
' Select column D (the list of employee names.
ActiveSheet.Range("D1").Select
' Go to the last cell with data in the selected column.
Selection.End(xlDown).Select
' Create variable, make it equal to the selected cell's row number.
LastRow = (ActiveCell.Row)
If SpinButton2 > LastRow Then SpinButton2 = 1
' Create variable for row number of employee info.
' Identifies desired row number.
RowNum = SpinButton2 + 1
'Fill variables with info. I can't figure this part out.
Dept = Cells(RowNum, 2)
ENbr = Cells(RowNum, 3)
EName = Cells(RowNum, 4)
Acct1 = Cells(RowNum, 5)
Acct2 = Cells(RowNum, 6)
Acct3 = Cells(RowNum, 7)
Acct4 = Cells(RowNum, 8)
Acct5 = Cells(RowNum, 9)
Stdby = Cells(RowNum, 10)
'Change active sheet to TimeCard.
Sheets("TimeCard").Select
If SpinButton2 = 1 Then
Range("M1").Select
ActiveCell.FormulaR1C1 = Dept
Range("D3").Select
ActiveCell.FormulaR1C1 = ENbr
Range("M3").Select
ActiveCell.FormulaR1C1 = EName
Range("C5").Select
ActiveCell.FormulaR1C1 = Acct1
Range("I5").Select
ActiveCell.FormulaR1C1 = Acct2
Range("O5").Select
ActiveCell.FormulaR1C1 = Acct3
Range("T5").Select
ActiveCell.FormulaR1C1 = Acct4
Range("Y5").Select
ActiveCell.FormulaR1C1 = Acct5
Range("AD5").Select
ActiveCell.FormulaR1C1 = Stdby
End If
End Sub
This is how I want it to work but not knowing enough maybe I have the wrong approach. Any help will be greatly appreciated.