Find the details against on double clicking a particular key - vba

Can you please help me with this problem?
I want to write a VBA program where when I click on a particular key in sheet1 it goes to the database and find me the details associated with the particular key.
Database is Excel sheet named PSN.
I have written my code below what is wrong.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Columns("E")) Is Nothing Then
Cancel = True
Worksheets("PSN").Activate
Dim cell As Range
Set cell = Range("A1:A10000").Find(Target.Value, LookIn:=xlValues)
End If
End Sub

just mention the sheets to search in.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As
Boolean)
If Not Intersect(Target, Columns("E")) Is Nothing Then
Cancel = True
Dim cell As Range
Set cell = sheets("PSN").Range("A1:A10000").Find(Target.Value, LookIn:=xlValues)
End If
End Sub

Related

Using VBA cut cell text and format with double click then paste with next click

Trying to set up so a double click in a cell cuts text AND formatting and then a single click in another cell paste the cut text AND format. Current code only paste un-formatted text into new cell.
here is what I have now
Option Explicit
Dim CutValue
Dim CutCell As Range
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Target.Cut
CutValue = Target.Text
Set CutCell = Target
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not IsEmpty(CutValue) Then
Target.FormulaR1C1 = CutValue
CutValue = Empty
CutCell.Clear
End If
End Sub
Big thanks for any help.
You can delay the cut until the user selects another cell:
Option Explicit
Dim CutCell As Range
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Cancel = True
Set CutCell = Target
End Sub
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not CutCell Is Nothing Then
CutCell.Cut Target.Cells(1) '<<<in case of multi-cell selection...
Set CutCell = Nothing
End If
End Sub

Excel Link two cells in different sheets using when changed macro

I have an Excel workbook with multiple worksheets. I have a cell in WORKSHEET A with range name TRACK1 and a cell in WORKSHEET B with range name TRACK2.
Each TRACK1 and TRACK2 are validated from a list. The user can change either cell from the drop-down list shown when the cell is selected.
I want to be able to allow the user to change either and have the other be also changed to match. Change value of TRACK1 and TRACK2 is changed, and vice versa.
I know how to do this basic macro, but how to stop the event propagating?
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Range("TRACK2") = Range("TRACK1")
End If
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Range("TRACK1") = Range("TRACK2")
End If
End Sub
In worksheet A's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK1")) Is Nothing Then
Worksheets("WORKSHEET B").Range("TRACK2") = Range("TRACK1")
End If
Application.EnableEvents = True
End Sub
In worksheet B's code module, use:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.EnableEvents = False
If Not Application.Intersect(Target, Range("TRACK2")) Is Nothing Then
Worksheets("WORKSHEET A").Range("TRACK1") = Range("TRACK2")
End If
Application.EnableEvents = True
End Sub

Double click cell to return cell contents into another cell (VBA)

I'd like to be able to double-click a cell and have the cell value/text be returned into another cell.
My code so far is:
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("E:P")) Is Nothing Then
Else
ActiveCell.Copy Destination:=Sheets("Sheet1").Range("S13")
End If
End Sub
The problem with this code is that I want the actual cell contents, not necessarily the numeric value.
For instance, in cell E12, I have the text "Completed", but when I double click E12, a "0" gets returned in cell S13 and not the text "Completed".
I agree with A.S.H "your original cell contains a formula".
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("E:P")) Is Nothing Then
If Target.Cells.Count = 1 then
Cancel = True
Sheets("Sheet1").Range("S13") = Target.Value
End If
End If
End Sub
Does this work?
Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("E:P")) Is Nothing Then
Cancel = True
Target.Copy Destination:=Sheets("Sheet1").Range("S13")
End If
End Sub

Change userform textbox value with cell double-click event

I have been searching for a answer all morning and have not come up with a solution. I want to change the value of a textbox1 on userform1 after a double click event. I keep getting the method or data member not found error. How do I complete this from the double click event?
Public Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Worksheets("Offense").Range("A:A")) Is Nothing Then
Cancel = True
DataCollectionFormValid.Show
Me.RowNumber.Value = ActiveCell.Row ' error here
End If
End Sub
Me refers to the object running the code, which in this case is the worksheet, not the userform. You need:
Public Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Worksheets("Offense").Range("A:A")) Is Nothing Then
Cancel = True
With DataCollectionFormValid
.RowNumber.Value = ActiveCell.Row
.Show
End With
End If
End Sub

how to determine if a range was selected in Excel VBA - Multiple ranges

I have created a userform in Excel 2012 whereby when the user double clicks on a specific range of cells the userform pops up.
This can be done by running the following vba code in the specific pages module.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
'condition to run when not clicked in range
Cancel = True
'condition to run if cell in range was clicked
userform1.show
End If
End Sub
This works perfectly.
The only problem is, since you are checking if not condition, I am unable to run many IF conditions to enable the functionality that will display a range of forms when the user clicks on different sections of the worksheet
Do you know how to enable an if statement that will check if different ranges are clicked, and for each range, show a different userform?
thanks
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
'condition to run when not clicked in range
Cancel = True
'condition to run if cell in range was clicked
userform1.show
ElseIf Not Intersect(Target, Range("B1:B10")) Is Nothing Then
'condition to run when not clicked in range
Cancel = True
'condition to run if cell in range was clicked
userform2.show
End If
End Sub
You can multiple ranges in your Intersect arguments and then test for Column to see which range was clicked. An example:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:A10, C1:C10")) Is Nothing Then
If Target.Column = 1 Then
useform1.show //Show userform1 if A1:A10 clicked
ElseIf Target.Column = 3 Then
useform2.show //Show userform2 if C1:C10 clicked
End If
End If
End Sub
Like I mentioned in comments why not an ElseIf?
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
ElseIf Not Intersect(Target, Range("B1:B10")) Is Nothing Then
'
'`~> And So on
'
End If
End Sub
Not much needs to be modified in your code to achieve what you want to.
Try this!
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Not Intersect(Target, Range("A1:A10")) Is Nothing Then
MsgBox "Column A"
ElseIf Not Intersect(Target, Range("B1:B10")) Is Nothing Then
MsgBox "Column B"
ElseIf Not Intersect(Target, Range("C1:C10")) Is Nothing Then
MsgBox "Column C"
End If
End Sub
The Solution(s) are all correct as per the question, however, based on the fact that the userdialog is actually being called for a particular column header, I decided that a select case would work better for me (where the extension of the worksheet will not affect the forms which are shown)
hence the code below looks for the particular column name (where the column is the first record on the row) and shows the form based on that.
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'Check the columName to determine which Form to Show
Select Case Cells(1, Target.Column)
Case "Column1"
UserForm1.Show
Case "Column2"
UserForm2.Show
Case Else
Cancel = True
End Select
End Sub