make automatic move between columns and cell? - excel

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

Related

How to display value based on highlighted cell?

Is there a way for a cell in Excel to show a value based on just selecting another cell? Example:
Column A has a list of car manufacturers. Column B has a list of vehicle models. When the user puts the cursor in cell A4 then cell D2 would display the content of cell B4. If the user clicks cell A3 then cell D2 now displays the content of cell B3. Is there a way to put a formula in cell D2 for this purpose without any macros? It would change value based on selecting a different cell in column A.
The code given below should work. It will be triggered everytime you select a value in Column "Make".
Paste the below code in the VB editor :
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim currentRow As Long
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A:A")) Is Nothing Then
currentRow = Target.Row
Range("D2").Value = Cells(currentRow, 2).Value
End If
End If
End Sub
Go to Excel Developer Mode (Alt + F11)
Doube Click The Sheet1(Sheet1) in Left Side
Paste The code And Save
Sub test()
Worksheets("Sheet1").Activate
Set selectedCell = Application.ActiveCell
Range("D2").Value = selectedCell.Row
End Sub
D2 is a your highlighted Cell
And Goto Excel Sheet and Open Macro
Select Cell & use ctrl+q

Make cursor jump to same row, different column when pressing Enter, with other specific requirements

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

select cell in range inputs value into cell in another sheet

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

Scroll to first highlighted cell

I have an excel sheet of 1250 line items. At the header I made one cell (I4) to enter the value that I’m looking for. Once clicked enter, the row of matched value in the sheet will be highlighted.
As I have to scroll down to reach it and to enter a value in first cell of the highlighted row, I need something, that selects the first cell of the highlighted row, once I entered the value in cell (I4) and pressed enter.
Please insert your row number here:
ActiveWindow.ScrollRow = YourRow
And if you want to scroll back to Range("A1"), even if your windows is tiled into panes, you may use this:
Private Sub ScrollHome()
Dim i As Integer
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
For i = 1 To ActiveWindow.Panes.Count
ActiveWindow.Panes(i).ScrollRow = 1
ActiveWindow.Panes(i).ScrollColumn = 1
Next i
End Sub
You need to setup an Event in your SHEET code, not MODULE code. It needs to be the Worksheet_Change event. This code should work. I included a couple defenses in case a user enters, clears, etc.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 And Not Intersect(Target, Me.Range("i4")) Is Nothing Then
If IsNumeric(Target.Value) Then
If Target.Value > 0 Then
Me.Rows(Target.Value).Select
End If
End If
End If
End Sub

Change selected cell after type text

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

Resources