I am creating a spreadsheet for a point of sale system. On the ‘sales’ sheet I have a command button for when a sale is complete. When clicked all of the data from that sheet moves into another sheet ‘today’s sales’ and I need help with vba code to assign a receipt number to each completed sale. The first sale of the day should be receipt no. 1 and each thereafter goes up by 1. Some of the code on that command button is as below:
Dim ReceiptNumber As Long ReceiptNumber = Sheets("Today's sales").Range("G" & Rows.Count).End(xlUp).Value + 1 For Each c In Sheets("Today's sales").Range("A:A") If c <> 0 And c.Offset(, 6) = 0 Then c.Offset(, 6) = ReceiptNumber End If Next
But this doesn’t work for the first sale of the day because column G has a title ‘receipt number’ – the code can’t add 1 to this value.
Does anyone know a way to get around this?
Just change your first line to this:
ReceiptNumber = Val(Sheets("Today's sales").Range("G" & Rows.Count).End(xlUp).Value) + 1
Loop the rows assigning each receipt a receipt number.
Dim lRow as Long Dim lReceipt as Long Dim ws As Excel.Worksheet Set ws = ActiveWorkbook.Sheets("Today's sales") 'You can set this to start at whatever row you want. lRow = 2 lReceipt = 1 'Loop through and rows and add a receipt number to ColumnA Do While lRow <= ws.UsedRange.Rows.count 'Put the receipt number in cell A of the current row. ws.Range("A" & lRow).Value = lReceipt lReceipt = lReceipt + 1 lRow = lRow + 1 Loop