简体   繁体   中英

What is the vba code to register employees' daily meals in a "list"?

Good night,

I have a screen made in excel for the canteen employees to register the employees who go to lunch daily.

Sub test()

Range("N5:Q5").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.149998474074526
    .PatternTintAndShade = 0
End With
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With
Selection.Merge
With Selection
    .HorizontalAlignment = xlLeft
    .VerticalAlignment = xlBottom
    .WrapText = False
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = True
End With
Range("N5:Q5").Select
ActiveCell.FormulaR1C1 = "Request"
Range("N6:Q21").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .ThemeColor = xlThemeColorDark1
    .TintAndShade = -0.149998474074526
    .PatternTintAndShade = 0
End With
Range("N21:Q21").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlMedium
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("N21").Select
ActiveCell.FormulaR1C1 = "TOTAL:"
Range("Q21").Select
ActiveCell.FormulaR1C1 = "=SUM(R[-14]C:R[-1]C)"
Range("Q7:Q21").Select
Range("Q21").Activate
Selection.Style = "Currency"
Range("N7").Select
ActiveCell.FormulaR1C1 = "Daily menu"
Range("Q7").Select
ActiveCell.FormulaR1C1 = "$4 "
Range("N8").Select
ActiveCell.FormulaR1C1 = "Extra drink"
Range("Q8").Select
ActiveCell.FormulaR1C1 = "$1 "
Range("N9").Select
ActiveCell.FormulaR1C1 = "Extra dessert"
Range("Q9").Select
ActiveCell.FormulaR1C1 = "$1.20 "
Range("Q10").Select
 ActiveCell.FormulaR1C1 = "Code employee:"
Range("F6").Select
ActiveCell.FormulaR1C1 = "Name employee:"
Range("G5:J5").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
Selection.Borders(xlEdgeTop).LineStyle = xlNone
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("G6:J6").Select
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
    .LineStyle = xlContinuous
    .ColorIndex = 0
    .TintAndShade = 0
    .Weight = xlThin
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Columns("F:F").EntireColumn.AutoFit
Range("P4").Select
ActiveCell.FormulaR1C1 = "=NOW()"
Range("P4").Select
Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss"
End Sub

This screen registers the menu and on the right side appears the summary with the price and at the end of the column the total. However, I would like to record this information in an additional sheet "List" where the employee's code, his name, the time he had lunch, what he had for lunch and the price of the meal, but I don't know which code to use, because as the employee registers, in the list it should appear on the following line. As you register on the home page, you will save in the "list".

Try this: You can save each column by changing CELLNAME to the cell that you want to save.

Sub SaveLunch()
Dim LunchRow As Long

'Determine New or Existing Invoice
If Sheet1.Range("XX999").Value = Empty Then 'Check if first record
    LunchRow = Sheet2.Range("A99999").End(xlUp).Row + 1  'First Available Row
Else: 'Add row
    LunchRow = Sheet1.Range("XX999").Value 'Existing Lunch Row
End If
Sheet2.Range("A" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 1
Sheet2.Range("C" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 2
Sheet2.Range("D" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 3
Sheet2.Range("E" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 4
Sheet2.Range("F" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 5
Sheet2.Range("G" & LunchRow).Value = Sheet1.Range("CELLNAME").Value 'Save column 6


End If

Sheet1.Range("XX999").Value = Sheet1.Range("XX999").Value + 1

End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM