I have a Excel File, I need to insert an scanned barcode into selected cell and after that I need to select the next cell no the below cell. For example:
Current selected cell = A1, Barcode scanned and inserted. Current selected cell now should be B1 instead A2.
I tried adding a textbox and works fine but I found a problem. If my barcode is for example 20010 my code split one number and inserts in one cell. So instead of having in A1 20010 I have 2, in B1 0, in C1 0 in D1 1 and in E1 0.
This is my code:
Private Sub TextBox1_Change()
If Len(TextBox1.Value) > 0 Then
ActiveCell.Value = TextBox1.Value
ActiveCell.Offset(0, 1).Activate
TextBox1.Value = ""
TextBox1.Activate
End If
End Sub
PD. I don’t know the barcode length. Never it’s gonna by the same.
How can I solve my problem?
Scanners are usually programmable to enter either a carriage return or a tab after it scans, but if you're dead set on using VBA to accomplish this, you could get rid of your textbox and use this in the Worksheet code of the sheet you're scanning into:
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
If Target.Row = 1 Then
ActiveCell.Offset(-1, 1).Select
End If
End Sub
If you want it to move from Column A to B to C regardless of row number then change the above to
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
ActiveCell.Offset(-1, 1).Select
End Sub
Related
I'm very new to using macros in Excel, so I hope this question isn't too silly.
I'm creating a worksheet to track sales. I've got a list of drinks in one column and I wanted to assign a macro to the cells so that when you click on them, the text in their cells is copied to another column.
I know you can record macros to copy-and-paste values, but I'm not sure how to make it copy the text in the next empty cell in the column, and not just in the first cell.
So in summary, these are my tables. I want to be able to click a cell in the Drinks column, and have the string appear at the bottom of the list column (so after 'Cuba Libre')
Thanks!
EDIT_1:
Ok, so here's my code so far:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address = "$C$2" Then
Range("C2").Select
Selection.Copy
Range("A2").Select
ActiveSheet.Paste
End If
End Sub
^I've repeated this code for each of the relevant cells in column C (C2:C5).
Like I said, I've only gotten to the point of being able to copy paste the values from the Drinks column into the List column, I am lost as to how to paste the value into the next empty cell.
You need to expand your check on Target to include the whole Drinks range. Then determine the next available cell in List
Something like (hard coded for List in Column A and Drinks in Column C)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rDrinks As Range
' single cell has been selected
If Target.Cells.Count = 1 Then
' Get ref to Drinks range
Set rDrinks = Me.Range(Me.Cells(2, 3), Me.Cells(Me.Rows.Count, 3).End(xlUp))
' Is selected cell in drinks range?
If Not Application.Intersect(Target, rDrinks) Is Nothing Then
' add to list
Me.Cells(Me.Rows.Count, 1).End(xlUp).Offset(1, 0).Value2 = Target.Value2
End If
End If
End Sub
UPDATE
So I've managed to figure out how to get the macro to paste the cell value at the bottom of the list!
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim LastRow As String
If Target.Address = "$C$2" Then
Range("C2").Select
Selection.Copy
LastRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row + 1
Range("A" & LastRow).Activate
ActiveSheet.Paste
Application.CutCopyMode = False
End If
End Sub
However, I've got a quality-of-life question. Is there any way to edit the code so that it applies to all the cells in the range C2:C10? Because as it stands, I would have to repeat the code for each cell replacing lines 3 & 4 for all the cells in range.
If Target.Address = "$C$2" Then
Range("C2").Select
If you actually turn List and Drinks into tables (with those names), you could use the following code
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Integer
Dim row As Integer
' Check user has clicked on a drink
If Not Intersect(Target, [Drinks]) Is Nothing And Target.Cells.Count = 1 Then
' Find first blank row in List
For i = [List].Rows.Count To 1 Step -1
If [List].Cells(i, 1) = "" Then
row = i
End If
Next
' If no blank row, add a new row
If row = 0 Then
ListObjects("List").ListRows.Add
row = [List].Rows.Count
End If
[List].Cells(row, 1) = Target
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
In the code below that i got working, when i select a cell in a range, an input box pops up and my input is sent to the same cell i clicked but in sheet 2. I want to take this one step further. i want to bypass the input box completely and just send the value F, and i only want to do this after i click cell b2. so cell b2 would have to work as some kind of toggle (maybe put an invisible shape over it to act as a button?)
Example 1: sheet 1, select cell B2 turns on macro, select cell in range example: D10 inputs the letter F into cell D10 on sheet 2, select cell B2 turns off macro so if i select cell D10 or any cell in that range nothing will happen anymore. it would also need to remove the value F from D10 if the cell is clicked again while the macro is on.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim xRtn As Variant
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D9:AS20")) Is Nothing Then
xRtn = Application.InputBox("Insert your value please")
Sheets("2020").Range(Target.Address).Value = xRtn
End If
End If
End Sub
Untested. I'm not sure if I understood all of your objectives.
I think if you add a checkbox to your worksheet (resize and store it wherever you want; maybe in cell B2) called "Check Box 1" then the below code should work.
One way of adding a check box might be: Excel > Developer > Insert > Check Box (Form Control) (depending on your Excel version). If the Developer tab is not visible, you may need to get it to show first.
Option Explicit
Private Function GetCheckBox() As Shape
Set GetCheckBox = Me.Shapes("Check Box 1")
End Function
Private Function IsCheckBoxTicked() As Boolean
IsCheckBoxTicked = (GetCheckBox.OLEFormat.Object.Value = 1)
End Function
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge <> 1 Then Exit Sub
If Intersect(Target, Me.Range("D9:AS20")) Is Nothing Then Exit Sub
If IsCheckBoxTicked() Then
With ThisWorkbook.Worksheets("2020").Range(Target.Address)
.Value = IIf(.Value = "F", Empty, "F")
End With
End If
End Sub
I have a single workbook with multiple sheets. Sheet 1 uses cell A1 as a search box to find a specific sheet in the book. The sheets after Sheet 1 are named like this, SO123456 with different numbers following the SO. I have the following code on sheet 1 to open the corresponding sheet from the value typed into A1 of Sheet 1:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
End Sub
When the corresponding sheet is opened, I want to open it to the next empty cell in column A. I have some code on the SOxxxxxx sheets that populates time in Column C when my data is entered in Column A. Here is that code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Columns("A:A")) Is Nothing Then
Target.Offset(0, 2).Value = Now
End If
End Sub
I'm not sure how to make sure the first empty cell in Column A is selected or where the code goes; sheet being opened or where the call is to open the sheet. I tried entering the following code below the Worksheet_Change block with no luck:
Range("A" & Rows.Count).End(xlup).Offset(1).Row
Cells(Rows.Count,1).End(xlup).Offset(1).Row
UsedRange.Rows.Count
I know next to nothing about VBA if you haven't already noticed.
Try this
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address <> "A$1” Then Exit Sub
With WorkSheets(Target.Value)
.Activate
.Cells(.Rows.Count,1).End(xlup).Offset(1).Select
End With
End Sub
This should get you to the first empty cell in Column A
rowEmpty = Range("A" & 10000).End(xlup).Row +1
You can replace the number 10000 with a sufficiently large number that ensures there will be no data there. There are alternatives to that, but I think this one is easier to begin with.
With this code, typing a sheet name in cell A1 from sheet1 will activate the sheet you type, and the active cell will be the first one in blank in column A.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Range("A1"), Target) Is Nothing Then Exit Sub
Sheets(Target.Value).Activate
ActiveSheet.Cells(Sheets(Target.Value).Range("A1").End(xlDown).Row + 1, 1).Select
End Sub
This seems rather an easy task, but somehow nothing has worked since I have tried several solutions to now...
I want something, supposing we have this sheet:
When a Cell is Clicked randomly, the value/contents of that text should be shown in the Orange Box.
That's the first and most important part.
The second thing to happen is:
if possible, all other Cells that contain the same value should be highlighted/shaded with an Green Background (See image)
I have tried:
https://www.ablebits.com/office-addins-blog/2015/02/10/excel-indirect-function/
http://www.contextures.com/xlFunctions05.html
https://support.office.com/en-us/article/Hide-or-display-cell-values-c94b3493-7762-4a53-8461-fb5cd9f05c33
https://support.office.com/en-us/article/CELL-function-51bd39a5-f338-4dbe-a33f-955d67c2b2cf
Thank you in advance.
EDIT 1: PS: Preferably a Non-VB solution. However, if a VB solution would be all there is, then your VB suggestions would be highly appreciated
Most of this is answered here so it just needs editing a bit
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
'Put in your actual range and the cell where you the text to be shown
If Not Intersect(Target, Range("B5:D17")) Is Nothing Then
Selection.Copy Destination:=Range("E2")
End If
End If
End Sub
and the colouring can just be done with a conditional formatting rule
=B5=$E$2
assuming that the values start in B5 and the text to be displayed is in E2.
If you only want to copy the value and not the formatting to E2, replace
Selection.Copy Destination:=Range("E2")
with
Range("E2").Value = Selection.Value
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Task1: Show your active cell contents in cell E2
actval = ActiveCell.Value
actaddr = ActiveCell.Address
Range("E2") = actval
'Task2: Turn all cells green, matching with active cell value, except host cell
'first set entire background white
Range("B5:F18").Interior.Color = vbWhite
'second set all matching cells green
For Each c In Range("B5:F18")
If c.Address <> actaddr Then
If c = actval Then
Range(c.Address).Interior.Color = vbGreen
End If
End If
Next c
End Sub