Checking ranges for information- Double click - excel

I am trying to write a macro where it checks if column A,B,C ( inclusive) have information in them. If all these columns have information in them, it allows the user to double click in column D , which then populates with Environ("Username"). If any if these columns are blank are messgebox pops up msgbox(“Missing Information”). I have not be able to get my head around this.

Try
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("D:D")) Is Nothing Then
If Application.CountIf(Range("A" & Target.Row & ":C" & Target.Row), "") > 0 Then
MsgBox "Missing Information "
Else
Target = Environ("Username")
End If
End If
End Sub
On how to add Worksheet_BeforeDoubleClick see this.

Related

Excel VBA select range

I want to be able by double clicking on a cell to move to a certain location in the same workbook, in the example below, I want to select the cells in Column B:BR of the current row.
For this I am using the following code:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Application.Intersect(Target, Range("o7:o7")) Is Nothing Then
Cancel = True
Worksheets("INCOMING").Activate
Range("r" & ActiveCell.Row & ":br" & ActiveCell.Row).Select
End If
End Sub
but it keeps on giving me the "run-time error '1004' select method of range class failed.
There is always an entire row selected.
anyone has any idea what I am doing wrong?
Please, try the next adapted code event. It assumes that ActiveCell from your code should be the one initially double clicked ("O7"):
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.address = "$O$7" Then
Cancel = True
With Worksheets("INCOMING")
.Activate
.Range("B" & Target.Row & ":BR" & Target.Row).Select
End With
End If
End Sub
Double clicking on cell "O7", the seventh row of "INCOMING" sheet is selected between "B" and "BR" columns...
If you want selecting the row of the ActiveCell of "INCOMING" sheet, you need to change Target.Row with ActiveCell.Row.

Running a macro on double clicking any cell in a worksheet

I want to run a common macro when I double click on any of the cells of my excel sheet.
Say when I double click the cell A20, my macro will capture the column number & row number.
And for this intersection of column & row proceed with my next step.
The macro is as below for e.g:
Sub Trial()
Dim x, y As Integer
x = ActiveCell.Column
y = ActiveCell.Row
Dim input1, input2 As String
input1 = Range(x & "2").Value
input2 = Range("A" & y).Value
End Sub
The result will be used in another macro for calling a SQL query -
Select Sum(value)
From Table
Where
column1 = 'input1' and
column2 = 'input2'
I just need the step to how to call my macro when I double click on any of the cell.
Right-click the sheet tab and choose View Code.
Select Worksheet from the first (Object) drop-down list at the top of the window, and select BeforeDoubleClick from the second (Procedure) drop-down. This generates a procedure stub:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
End Sub
It is in here that you can write your code. You can check the Target to only respond to a certain range of cells, and use Cancel to cancel any default double-clicking behaviour.
For example, the following will confirm which range was double-clicked:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox Target.Address
End Sub
Try using:
Option Explicit
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
MsgBox "Row:" & Target.Row & vbNewLine & "Column:" & Target.Column
End Sub
Image:

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)

macro vb script for pop up box in Excell

I have 3 columns A,B,C where C is the ID and the rest 2 have values. If I change some values in A or B columns, I need to get a pop up message box which shows the ID of the row which was changed (EX: If I change row 10 column A value, then in the pop up it should show the ID of row 10 in column C).
Please help me on this.
Below is the script that I have tried, but I am not able to populate the column C value,I can only able to populate the next column value for which the value has changed (Ex: If I change the value of column A pop is showing the value of Column B, but I need the value of column C)
Please find the script I am using
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Range("A1:C16"), Target) Is Nothing) Then
MsgBox Target.Next & "" & Target.Address & " has changed.", vbInformation
End If
End Sub
Please try code below. It will give you address of changed cell and ID value of row in which change occurred.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not (Application.Intersect(Range("A1:C16"), Target) Is Nothing) Then
MsgBox "ID:" & Cells(Target.Row, 3) & "-" & Replace(Target.Address, "$", "") & " has changed.", vbInformation
End If
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

Resources