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
Related
I am trying to show/hide a number of rows based on "n".
The rows I'm trying to show or hide span from rows 21 to 70. My input cell for "n" is B14.
Say if B14 is 2 then I only want rows 21 and 22 to be visible (23 to 70 hidden). If B14 is 48 then rows 21 to 68 visible and so forth.
Can someone help me with the macro required to achieve this?
Try this code, please. It will automatically hide the rows according to the changed value in "B14". Copy the code in the sheet module. In order to do that, select the sheet to be processed, right click on its page and choose "View Code". Copy the code there, go back in the sheet, start playing with values in "B14" and send some feedback about its behavior:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("B14")) Is Nothing Then
Dim lastRow As Long, visRows As Long
If IsNumeric(Range("B14").Value) Then
visRows = Range("B14").Value
Else
MsgBox "In range ""B14"" must be a number!": Exit Sub
End If
If visRows = 50 Then
lastRow = 69
Else
lastRow = 70
End If
Range("A:A").EntireRow.Hidden = False 'make all rows visible
Range(Range("A21").Offset(visRows), Range("A" & lastRow)).EntireRow.Hidden = True
End If
End Sub
The following VBA code should work
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rngWatched As Range, n As Long
Set rngWatched = Me.Range("B14")
If Not Intersect(Target, rngWatched) Is Nothing Then
With Me.Range("A21:A70")
.EntireRow.Hidden = True
n = rngWatched.Value
If n > 0 Then
.Cells(1, 1).Resize(n, 1).EntireRow.Hidden = False
End If
End With
End If
End Sub
To use the code, please follow these steps:
Press Alt+F11. This will take you to the VBA editor window
On the left hand side you will see a project explorer
Expand VBAProject(YOUR_EXCEL_FILENAME)
Expand Microsoft Excel Objects
Double-click on the relevant sheet. This will open the sheet class page
Paste the code here
Close the VBA editor window and go back to excel
Make sure the file is then saved as Excel Macro-Enabled workbook (*.xlsm)
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
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 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