Excel VBA select range - excel

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.

Related

How to copy entire row from worksheet to another worksheet

I'm relatively new to VBA. I have this sub procedure CutePaste, that I call in worksheet_change(ByVal Target As Range), that executes whenever the value in column "F" is changed. My goal is to copy the entire row of the cell changed and paste it into another sheet ("Cast Worked"). My code right now only copies the updated cell and paste that to the new sheet. Please advise how I can copy the entire row of the updated cell.
Sub CutPaste()
If Not Intersect(myTarget, Range("F:F")) Is Nothing Then
ActiveCell.Activate
a = Sheets("Cast Worked").Cells(Rows.Count, "A").End(xlUp).Row + 1
Sheets("Cast Worked").Range("A" & a).Value = ActiveCell.Value
ActiveCell.Offset(1, 0).Select
End If
End Sub
You are close. You can use the Range.Copy method to do this. Furthermore you need to pass the target from the worksheet_change event to your subroutine.
Sub worksheet_change(ByVal Target As Range)
'Pass target range to subroutine
CutPaste(Target)
End Sub
Sub CutPaste(myTarget As Range)
If Not Intersect(myTarget, Range("F:F")) Is Nothing Then
a = Sheets("Cast Worked").Cells(Rows.Count, "A").End(xlUp).Row + 1
Target.EntireRow.Copy Destination:=Sheets("Cast Worked").Range("A" & a)
Target.Offset(1, 0).Select
End If
End Sub
I removed ActiveCell.Activate since setting the active cell to active is superfluous.

If A1 changes, put something into B1 | If A2 changes, put something into B2

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

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:

Check on ActiveCell before Target.Address

I want an if clause to be true if the ActiveCell was "A1" before I clicked on "A2".
Its not connected with a button or anything. It has to work by just clicking on "A2".
That's my try i made:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Address(0, 0) = "A2" And ActiveCell.Cells(0, 0) = "A1" Then
MsgBox ("test")
End If
End Sub
Does someone have a solution?
I think you are using the wrong approach here.
If you use the SelectionChange event then it means the selected cell already changed. You have to stock the address of the last cell every time the event is called. To do the trick, put your condition before stocking the ActiveCell as the lastCellAddress and it works perfectly.
Here is the code with comments to help you understand:
'Declare the variable out of the event
Dim lastCellAddress As String
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
'Check if the last selected cell was A1 when selecting A2
If (ActiveCell.Address = "$A$2" And lastCellAddress = "$A$1") Then
'If the condition is true display your MsgBox
MsgBox ("test")
End If
'Set the current address as the last selected for next selection change
lastCellAddress = ActiveCell.Address
End Sub

Checking ranges for information- Double click

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.

Resources