Worksheet_SelectionChange not firing from Selection.Offset - excel

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.

Related

How to get a radio button or sheet macro to use relative references

I want to use a radio button to add or subtract from the on hands in the same row. I couldn't figure out how to tie it down to a cell.
I decided to just fill E2-E450 and F2-F450 with a "+" or "-" sign respectively.
I want when you click E2 it adds + 1 to D2 or when you click F3 it subtracts one from D3 and so on and so forth.
Sub MyMacro()
Range("D2").Select
ActiveCell.FormulaR1C1 = Range("D2") + 1
End Sub
Sub MyMacro2()
Range("D2").Select
ActiveCell.FormulaR1C1 = Range("D2") - 1
End Sub
For the sheet this is what I have
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("E2")) Is Nothing Then
Call MyMacro
End If
End If
If Selection.Count = 1 Then
If Not Intersect(Target, Range("F2")) Is Nothing Then
Call MyMacro2
End If
End If
End Sub
Each button is assigned to the OptionButtonClick procedure below, which is stored in a normal module. Clicking the button will return the address of the TopLeftCell that the button is sitting in.
Application.Caller will return the name of the shape that was clicked.
Public Sub OptionButtonClick()
MsgBox ActiveSheet.Shapes(Application.Caller).TopLeftCell.Address
End Sub
I would give a more detailed answer, but I've no idea what the "on hands" are.

make automatic move between columns and cell?

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

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

How to create a macro: let a button use a cell value

I have a list with asset tags in excel. I have 2 columns (F and G). In G are the actual asset tags. In F I would like to create a button on every row that executes a macro using the data in the cell next to it in column G
Example of Columns:
I have a little bit of VBA code that starts vncviewer.exe with the asset tag in column G. It's a nice bit of code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("G:G")) Is Nothing Then
'Call Shell("c:\progra~1\realvnc\vncvie~1\vncviewer.exe " & Target.Value)
Call Shell("c:\progra~1\realvnc\vncvie~1\vncviewer.exe " & Target.Value)
End If
End Sub
But you have to double click on the cell in column G to enter the data in the cell and then hit Enter or Tab to get out of it. This activates the vncviewer successfully.
I would like the macro to take over this double-click and exiting of the cell next to the button I click on.
The (recorded) Macro below seems to do what I want (except for only entering G2, copying pasting in place and exiting to H2), but I have no idea how to make it so that I can make an individual button on each row in Column F corresponding the asset tag next to it in G
Sub Macro2()
'
' Macro2 Macro
'
'
Range("G2").Select
Selection.Copy
ActiveSheet.Paste
Range("H2").Select
End Sub
This is a screenshot of the button creation:
From what I can understand, your column G is just a list. You don't necessarily want to invoke VNCViewer when the list is changed but, instead, you would like the user to be able to select one of those values and then click a button to get VNCViewer to use its value.
If so, try this (assuming your button is linked to the Macro2 subroutine):
Sub Macro2()
If Selection.Column = 7 Then
Shell "c:\progra~1\realvnc\vncvie~1\vncviewer.exe " & Selection.Value
End If
End Sub
and delete your Worksheet_Change code.
The user will then be able to simply select a cell in column G by clicking it, then click the single button.
It's not really clear what you're after, but this might start you off
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("G:G")) Is Nothing Then
If Target.Count > 1 Then Exit Sub
Dim b As Button
Call Shell("c:\progra~1\realvnc\vncvie~1\vncviewer.exe " & Target.Value)
Application.ScreenUpdating = False
Set b = ActiveSheet.Buttons.Add(Target.Offset(, -1).Left, Target.Top, Target.Width, Target.Height)
With b
'.OnAction = "btnS"
.Caption = "Btn " & target.row
.Name = "Btn" & target.row
End With
Application.ScreenUpdating = True
End If
End Sub

Excel tab to new line after certain amount of columns

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 ?

Resources