I am wanting to set up an excel spreadsheet for data entry with a barcode scanner.
The barcode scanner sends the barcode then a tab OR an enter key depending how its programmed.
Basically I want to set up an excel sheet that we can scan 6 barcodes for each item, with the scanner tabbing to the next column each time, then when it reaches the 6th column the next tab will make it move to a new line for the next product.
I hope this makes sense. It can be done in MS word... e.g if you create a table with 6 columns and push tab 7 times it will move to the next row.
I am wanting to do this in Excel.
Thank you
The TAB or ENTER keys already trigger the SelectionChange event.
So, this might be a little tidier way of doing the same thing if you don't for some other reaason need to use the Change event instead of the SelectionChange event.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngLastColumn As Range
'Note: The {Tab} or {Enter} key triggers selectionChange event.'
' Modify the address of rngLastColumn as needed. It should be one column beyond
' the last column of your inputs, so if input use columns A:F, then it should
' be Range("G:G").
Set rngLastColumn = Range("G:G")
If Not Intersect(Target, rngValidColumns.Columns(7)) Is Nothing Then
'Insert a new row:'
Target.EntireRow.Offset(1, 0).Insert
'Select the first cell in the new row'
cells(Target.Row + 1, 1).Select
End If
End Sub
Well... after a lot of experimenting gathering pieces of code from a lot of places and then debugging I ended up with the following VBA macro. Hope it helps! :)
When TAB or ENTER key is pressed the Sub Worksheet_Change will run.
It will check if it's column F being left...
If true => insert new row and select first cell [A]n where n = row number.
VBA macro code
Private Sub Worksheet_Change(ByVal Target As Range)
'Do nothing if more than one cell is changed or content deleted
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
'Column F
If Target.Column = "6" Then
'Insert new row bellow
ActiveCell.EntireRow.Offset(1, 0).Insert
'Select first cell of next row just inserted
ActiveSheet.Cells(ActiveCell.Row + 1, 1).Select
End If
End Sub
Private Sub Workbook_Activate()
Application.OnKey "{TAB}", "Worksheet_Change" 'TAB key press
Application.OnKey "~", "Worksheet_Change" 'Keyboard ENTER press
End Sub
Private Sub Workbook_Deactivate()
Application.OnKey "{TAB}"
Application.OnKey "~"
End Sub
Maybe I'm missing something on the issue, but if you select your six columns and transform the selection with the "create list" command, then whenever you tab to the last cell of a line you'll automatically going to the next line. Furthermore, if you're at the last line, a new one will be created. IM not sure why you need a macro for that ?
Related
How do you make a macro, which deletes a row if a row is selected, and otherwise prompts up an message box? I made a code like this but it always skips the first if statement and goes on to the next one.
Sub DeleteRow()
If ActiveCell.Row = True Then
Selection.EntireRow.Delete
Else: MsgBox "Choose a row first", vbOKOnly, "Delete a row"
End If
End Sub
You may try below sub
Sub DeleteRow()
If ActiveCell.Row = Selection.Row Then
Selection.EntireRow.Delete
Else: MsgBox "Choose a row first", vbOKOnly, "Delete a row"
End If
End Sub
Basically you can just run below single line to delete selected row.
Selection.EntireRow.Delete
This should also work
ActiveCell.EntireRow.Delete
Any time you want code to run when you do something to a worksheet, consider using the Worksheet subroutines.
The one you are interested in is the Worksheet_SelectionChange. The Worksheet subroutines (such as the Worksheet_SelectionChange are always stored in the code for that Sheet.) Here are a list of events that the Worksheet has.
Let's break your problem down:
When the user selects something, identify what they have selected
If the user has selected an entire row, do something OR
If the user has selected a single cell, do something else OR
If the user has selected anything else, ignore
The Worksheet_SelectionChange takes one parameter: Target, which is the selection that the user made.
Below is the code for deleting a row if it is selected. This should be enough for you to move in the right direction.
Again, the big take away: when responding to user input, always consider the built-in Worksheet subroutines.
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' Purpose:
' Delete a row if it is selected
' Check if a row was selected
Dim Address As String
Address = Target.Address
Address = Replace(Address, "$", "")
' Rows and columns have the form #:# when selected
' Where # is either the column letter OR the row number
If InStr(Address, ":") Then
' Since a : was in the address, an entire row or column was selected
' Was a column or row selected?
' Check if the members of AddressArray are numbers
Dim AddressArray As Variant
AddressArray = Split(Address, ":")
If IsNumeric(AddressArray(0)) And IsNumeric(AddressArray(1)) Then
' Both members of the array were numbers, so a row was selected
' Delete the row
Target.Delete
End If
End If
End Sub
I would go with this approach:
If Selection.Address = Selection.Cells(1).EntireRow.Address Then
I need VBA code for Excel that will allow me to capture the enter key in a specific column and instead of moving down one row (default for rest of workbook) on enter, remain in the same cell or return to same cell.
Public Sub MyCode()
ActiveCell.Offset(-1, 0).Select
End Sub
Private Sub Worksheet_Activate()
With ThisWorkbook.Sheets("REQUEST FORM")
Application.OnKey "~", "MyCode"
End Sub
In addition, it cannot affect any other open workbooks.
I've had a look about and whilst I've found some similar threads (with varying degrees of relevancy/usefulness), none of them covered my exact situation.
I want to use a sheet in my workbook as a form where I input one type of data in one column and press enter, and then jump to another column on the same row to input a second type of data.
My sheet has an "Item Number" column and a "Count" column with several columns in between them.
What I'm trying to achieve is:
Type data into a cell in the "Item Number" column and press Enter.
The cursor appears on the same row, several columns to the right in the "Count" column.
I want this only to occur when pressing Enter after entering data into blank cells only
in the "Item Number" column. Pressing Enter in any other column should just allow the cursor to behave as it would otherwise.
At the moment I have the code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Range("A100") = ActiveCell.Address
Cells(Application.ActiveCell.Row, 11).Select
End Sub
The first line (beginning with "Range") allows me to see the active cell address in cell A100. This works along with some other code that lets me colour fill the active cell in that worksheet so I can see things a bit better when the spreadsheet gets busy.
The second line (beginning with "Cells") is the code that is supposed to make the cursor jump over to the "Count" column after pressing Enter in the "Item Number" column.
Now, this all works fine except for one major drawback.....
When clicking on a cell in the "Item Number" column, the cursor is immediately moved to the "Count" column before I'm able to enter any data, let alone hit the Enter key.
What am I doing wrong?
and
How do I go about ensuring this only happens when pressing Enter after entering data into a blank cell in the "Item Number" column?
I hope this makes sense, reading so much about code has got me cross eyed.
Cheers
you should combine Worksheet_Change() and Worksheet_SelectionChange() events and use a sheet scoped variable to check for empty cells being changed
Option Explicit
Dim emptyCell As Boolean
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.CountLarge > 1 Then Exit Sub ' don't bother nulticell selections
If Cells(1, Target.Column).Value <> "Item Number" Then Exit Sub ' don't bother selections outside "Item Number" column
If emptyCell And Not IsEmpty(Target.Value) Then Cells(Target.Row, Range("A1", Cells(1, Columns.Count).End(xlToLeft)).Find(what:="Count", LookIn:=xlValues, lookat:=xlWhole).Column).Select ' if current cell was empty and now it is not then skip to "Count" column same row
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
emptyCell = IsEmpty(Target.Value) ' store the info about current selection being empty
End Sub
Please try the Change event. The code below should do what you want. It could be expanded to respond (differently) to changes in other columns.
Private Sub Worksheet_Change(ByVal Target As Range)
Const TriggerColumn As Long = 3 ' modify as required
Const NextColumn As Long = 11 ' modify as required
Dim Rng As Range
' don't take action if more than 1 cell was changed
If Target.Cells.CountLarge > 1 Then Exit Sub
' don't include the caption row in ther ange
' find the last used row in a column filled BEFORE Triggercolumn
Set Rng = Range(Cells(2, TriggerColumn), Cells(Rows.Count, "A").End(xlUp))
' take action only if the changed cell is within Rng
If Not Application.Intersect(Target, Rng) Is Nothing Then
' skip if the change resulted in a blank cell
With Target
If Len(.Value) Then
Cells(.Row, NextColumn).Select
Else
' prevent the next action from triggering this function
Application.EnableEvents = False
' delete contents of the next cell
Cells(.Row, NextColumn).ClearContents
Application.EnableEvents = True
End If
End With
End If
End Sub
It's possible make automatic move between columns and cell?
For Example:
I want only column A,B,C(Column A-item number Column B-LP(license plate) Column-C Location
I want scan barcode and cursor from column A2 go to right to Column B2 form column B to Column C2 but from column C2 I want back to Column A3 next Cell like automatic is possible ?
now I have like this but still I must use Ctrl+a then I back to Column A3.
If the scanner is set to press Enter, use this piece of code in Worksheet_Change event:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.count = 1 Then
If Target.Column = 1 Or Target.Column = 2 Then
Target.Offset(0, 1).Select
ElseIf Target.Column = 3 Then
Target.Offset(1, -2).Select
End If
End If
End Sub
It will not move the selection if you modify something outside the A:C columns.
Easy alternative is to format the columns A to C as table (list object). And change the option Cell Movement After Enter in Excel options to right instead of down. Entering data will then go from column C to A in the next line:
Image 1: The values are just typed in and confirmed with Enter. Because column C is the last one in the table now it starts over in the next line column A.
The cell movement direction can also be changed by code
Application.MoveAfterReturnDirection = xlToRight
so you can eg. change it if the workbook opens and change it back if it closes, so it doesn't affect all your workbooks.
Option Explicit
Dim MoveAfterReturnDirection_ORIGINAL As Long
Private Sub Workbook_Open()
MoveAfterReturnDirection_ORIGINAL = Application.MoveAfterReturnDirection
Application.MoveAfterReturnDirection = xlToRight
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.MoveAfterReturnDirection = MoveAfterReturnDirection_ORIGINAL
End Sub
I'm trying to use the selection of a cell in a given column as an event. This works if I physically mouse click or arrow into a cell in the column.
I would like to though, get to this column by using Selection.Offset or ActiveCell.Offset or something similar. But when a cell in the given column is selected via this offset function, it doesn't seem to count as a SelectionChange event and therefore doesn't activate my next macro.
When Data is entered into "Height" column, Call Height_To_Comments()
Height_To_Comments() moves over to "Comments" from "Height" in same row
With or without data entry, in "Comments" Column, ENTER calls Comments_To_Weight()
Comments_To_Weight() moves back to "Weight" from "Comments" and down a row
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("H3:H52")) Is Nothing Then
Call Height_To_Comments
End If
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
x = 0
For Each cell In Target
If cell.Column <> 13 Then x = 1
Application.OnKey "~"
Application.OnKey "{ENTER}"
Next cell
If x = 0 Then
Application.OnKey "~", "Comments_To_Weight"
Application.OnKey "{ENTER}", "Comments_To_Weight"
End If
End Sub
Sub Height_To_Comments()
Selection.Offset(-1, 5).Select
End Sub
Sub Comments_To_Weight()
Selection.Offset(1, -7).Select
End Sub
I solved it.
I had changed the function of "~" and "{ENTER}" only after there had been a selection change in the comments column itself (rather than on the prior height column before the curser arrives in the comments column). I changed it so that when a selection change was made in the height column, THEN the function of "~" and "{ENTER}" changes. So once the data is entered into height, the sheet tabs over to comments, then when the user hits ENTER or ~ it correctly goes back to weight on the next line, rather than enter just going to the next line and then the next time enter is hit it goes to the next row and left 7 columns.
Changed If cell.Column <> 13 Then x = 1 to If cell.Column <> 8 Then x = 1.