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

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

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.

How to fill a worksheet based on cell values from another worksheet

I have two worksheets ("Worksheet A" and "Worksheet B").
Worksheet A has a list of tests. Each row represent a test performed.
Column F has the value "Passed" or "Not Passed".
If "Not Passed", the entire row needs to be added automatically to "Worksheet B" which represent an action list.
Any suggestion on how to do this dynamically, i.e. without having to run a macro manually?
Use worksheet events. Alt + F11 to open VBA interface:
Double click "Sheet1" on the left hand side of the interface and paste this in:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 0 Then
update_sheet
End If
End Sub
Below where it says "Sheet1", right click on "Modules". Hover on insert and click "Add Module". Paste the following code into the module:
Sub update_sheet()
s1_rows = ThisWorkbook.Worksheets(1).Cells(ThisWorkbook.Worksheets(1).Rows.Count, "F").End(xlUp).Row
Set s1_rng = ThisWorkbook.Worksheets(1).Range("F1:F" & s1_rows)
s2_rows = 1
For Each cell In s1_rng
If cell.Value = "Not Passed" Then
cell.EntireRow.Copy
ThisWorkbook.Worksheets(2).Range("A" & s2_rows).PasteSpecial xlPasteValues
s2_rows = s2_rows + 1
End If
Next cell
Application.EnableEvents = True
End Sub
This can be improved, but should start you off.

If statement that only performs action when selection is within a specific column

I am trying to finish this macro that will open another workbook, copy, and paste the selected cells into the other workbook. I want to do this only when the selection is in column A and only when the selection contains data.
The if the selection contains data part is easy, however how can I make a statement that says if selection is not within column A, then MsgBox("Please select data")?
Here is the if statement so far, it still needs the part mentioned directly above.
'Warns if no QN#s are selected
If Selection = "" Then
MsgBox ("Please Select Your QN#s Before Running This Macro")
Exit Sub
End If
This seems to work for recognizing if the cells are within a certain column.
If Not Intersect(Selection, Range("A:A")) Is Nothing Then
'Proceed
Else
MsgBox ("Please Select Your QN#s Before Running This Macro")
Exit Sub
End If
Why not use a Worksheet_SelectionChange?
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Column <> 1 Then
MsgBox "Please Select Your QN#s Before Running This Macro", vbOKOnly, "Data Selection"
Else
RunMacro
End If
End Sub
Use this pattern of codes. This is just dealing with the selection and not detecting changes.
Dim a As Integer
Dim b As Integer
Dim c As String
a = Selection.Row
b = Selection.Rows.Count - 1
c = "A" & a & ":" & "A" & a + b
If ((Selection.Column = 1) And (WorksheetFunction.CountA(Range(c)) > 0)) Then
MsgBox "ready to go"
End If
In the above code the hidden rows with data will be considered.
Merged cells are considered as one cell with value and others are blanks( example: When you have merged A2:A5, then you have 3 blank cells)

Worksheet_SelectionChange not firing from Selection.Offset

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.

Go To A Particular Cell After Inputting Data In Another Cell And Hitting Enter Options

I have a Excel sheet and I'm inputting data in A2, B2 and C2; after I hit enter I want the cursor select the next line so I can input information on A3, B3 and C3 and so on
I found this information
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Range("C2").Value <> "" Then
Range("A3").Select
End If
End Sub
But it only works one time, how can I repeat this process over and over
Thanks for your help.....
Another way.
If you type anywhere in Col A, the selection will move to Col B.
If you type anywhere in Col B, the selection will move to Col C.
If you type anywhere in Col C, the selection will move back to Col A (Next Line).
Code: This goes in the relevant sheet code area.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Whoa
Application.EnableEvents = False
If Not Target.Cells.CountLarge > 1 Then
If Not Intersect(Target, Columns(1)) Is Nothing Then
Target.Offset(, 1).Select
ElseIf Not Intersect(Target, Columns(2)) Is Nothing Then
Target.Offset(, 1).Select
ElseIf Not Intersect(Target, Columns(3)) Is Nothing Then
Target.Offset(1, -2).Select
End If
End If
Letscontinue:
Application.EnableEvents = True
Exit Sub
Whoa:
MsgBox Err.Description
Resume Letscontinue
End Sub
ScreenShot
If you need to involve VBA to that there are to properties which set direction and status respectively:
Application.MoveAfterReturnDirection = xlToRight
Application.MoveAfterReturn = True
in your Tools>options dialog what is the settings for Move Selection after Enter ?:
there is more information on Tech Republic: Three ways to control Excel's cursor movement during data entry which will show you how to do exactly what you want...
Basically, select the range cells you want to enter data in, Excel selects the first cell, then after each entry, hit the {Tab} key. Excel will move the carat to the next empty cell in your selection
Voila !

Resources