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:
Related
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.
I have a set of data:
I have data validation set up for entering Y or N within the "more issues Column".
I need a row to be inserted under that when Y is selected, without having to run a macro.
I am running Excel 2016.
Try this macro: place in the worksheet object.
*Updated
Private Sub Worksheet_Change(ByVal Target As Range)
'If the cell already has a capital "Y" and you
'double click the cell it will insert a row.
'And typing a "Y" into any other column will not error
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Columns(2)) Is Nothing Then
If Target.Value = "Y" Then
Target.Offset(1).EntireRow.Insert Shift:=xlDown
End If
End If
End Sub
Here's some working code to get you started. Just place the code in the sheet you are using by right clicking the sheet tab and selecting view code.
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim r As Range, sh As Worksheet
Set sh = ThisWorkbook.Sheets("Sheet1")
If Not Application.Intersect(Range("B2:B8"), Range(Target.Address)) Is Nothing Then
Set r = Target
If r = "Y" Then r.Offset(1, 0) = "populated cell"
End If
End Sub
The animated gif (click to see detail) show entering N or Y and having the cell below only Y's populated. Ask if you have questions about how this works.
I have rows from 1-100.
I know how to target specific cells and get data from them, but how would I do this when any row from 1 to 100 can be changed?
Say you put anything into Row A3. How would you write "Updated" into row B3 via VBA?
I want this to apply to rows A1-A100.
Thanks
Place the following event macro in the worksheet code area:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim A As Range, Intersection As Range, Cell As Range
Set A = Range("A1:A100")
Set Intersection = Intersect(Target, A)
If Intersection Is Nothing Then Exit Sub
Application.EnableEvents = False
For Each Cell In Intersection
Cell.Offset(0, 1).Value = "Updated"
Next Cell
Application.EnableEvents = True
End Sub
Open VBA Editor
Double click on the sheet you event take action (sheets appears in the left top box)
Select Worksheet on the left box above code box
Select change on the right box above code box
Paste the code
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
With ThisWorkbook.Worksheets("Sheet1")
If Not Intersect(Target, .Range("A1:A100")) Is Nothing Then
Application.EnableEvents = False
.Range("B" & Target.Row).Value = "Updated"
Application.EnableEvents = True
End If
End With
End Sub
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.
I try to write a macro that on double click of a cell, inserts a new row below that cell with some formulas. The important thing for me is that if I double click the cell again, then the formulas of the previously inserted line are updated with the right indexes.
For example, in the code below, double click A1 will insert the formula =B2+1 in line 2. Double clicking again should insert the same in line 2. But now line 2 shifter to line 3, so the formula in A3 should be =B3+1.
Here is the code I have so far:
Option Explicit
Const MYRANGE As String = "A:A"
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
'If Sh.Name <> "Sheet1" Then Exit Sub
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Sh.Range(MYRANGE)) Is Nothing Then Exit Sub
Cancel = True
Target.Rows(2).Insert
Dim newRow As Range
Set newRow = Target.Rows(2)
Dim rowIndex As Long
rowIndex = newRow.row
newRow.Cells(1, 1).Formula = "=B" & rowIndex & "+1"
End Sub
UPDATE: Changing Target.Rows(2).Insert to Target.Offset(1).EntireRow.Insert solves the issue. Leaving the question open for explanations on what is Offset and how it differs from Rows (The property EntireRow does not exist for Rows(2))
You can reduce this code by four lines for the same outcome, pls see below
Note though that your code is updating cells in your target row and below, ie it won't be updating any cell formulae outside column A that reside above your target. Which is probably not an issue but worth mentioning. If you wanted a full update then you would always insert at row2 rather than at target
Option Explicit
Const MYRANGE As String = "A:A"
Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
If Target.Cells.Count > 1 Then Exit Sub
If Intersect(Target, Sh.Range(MYRANGE)) Is Nothing Then Exit Sub
Cancel = True
Target.Offset(1).EntireRow.Insert
Target.Offset(1).Formula = "=B" & Target.Row + 1 & "+1"
End Sub