Forum Discussion
BickeringTuba06
May 18, 2023Copper Contributor
Need some help with Excel.
When I scan my barcode in column n it matches the number in column b and highlights the information in that row in green. I would like a date and time to be added to column k of same row.
- NikolinoDEGold Contributor
To achieve the desired functionality in Excel, you can use a VBA macro. Here's an example code that can help you accomplish this task:
Private Sub Worksheet_Change(ByVal Target As Range) Dim rngBarcode As Range Dim rngData As Range Dim cell As Range ' Set the range for the barcode column (Column N) Set rngBarcode = Me.Range("N:N") ' Set the range for the data column (Column B) Set rngData = Me.Range("B:B") ' Check if the changed cell is in the barcode column If Not Intersect(Target, rngBarcode) Is Nothing Then ' Loop through each cell in the barcode column For Each cell In Intersect(Target, rngBarcode) ' Find the matching barcode in the data column Dim matchCell As Range Set matchCell = rngData.Find(cell.Value, LookIn:=xlValues, LookAt:=xlWhole) ' If a matching barcode is found, highlight the row in green and add date and time to column K If Not matchCell Is Nothing Then Dim rowNumber As Long rowNumber = matchCell.Row ' Highlight the row in green Me.Rows(rowNumber).Interior.Color = RGB(0, 255, 0) ' Add date and time to column K Me.Cells(rowNumber, "K").Value = Now() End If Next cell End If End Sub
Here's how you can add this code to your Excel workbook:
- Open your Excel workbook.
- Press Alt+F11 to open the VBA Editor.
- In the Project Explorer window, find your worksheet in which you want to implement this functionality.
- Double-click on the worksheet name to open the code window.
- Paste the above code into the code window.
- Close the VBA Editor.
Now, whenever you scan a barcode in column N, the macro will check if it matches any value in column B. If a match is found, the corresponding row will be highlighted in green, and the current date and time will be added to column K of that row.
Make sure to save your workbook in a macro-enabled format (e.g., .xlsm) to keep the VBA code enabled.
Note: This code assumes that your barcode column is N and your data column is B. Modify the ranges (rngBarcode and rngData) in the code if your columns are located differently.