Change userform textbox value with cell double-click event - vba

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

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

Find the details against on double clicking a particular key

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

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

VBA - Open a UserForm by clicking anywhere in a specific column

I would like to build a makro in VBA which opens a UserForm when I click in a cell in a specific column, for more details look here.
With this code (from Mr.Burns):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("A1")) Is Nothing Then
'name of userform .Show
End If
End If
End Sub
I was able to open the UserForm by clicking in the cell A1, but not by clicking in any cell inside the column A.
I tried to solve this problem with this code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
Dim check As Boolean
check = True
If check Then
Dim i As Long
For i = 1 To 100000
If Not Intersect(Target, Range("A" & i)) Is Nothing Then
UserForm1.Show
check = False
End If
Next
End If
End If
End Sub
It actually works fine, but it is very slow, is there any better possibility to solve this?
To display the form when a cell is selected in column A:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
' if target is one cell and in column A
If Target.Columns.count = 1 And Target.Rows.count = 1 And Target.Column = 1 Then
UserForm1.Show
End If
End Sub
You can use .count and .column property together with AND and it will become so much simple and fast. Following code triggers pop-up if u click in column A on active-sheet
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error GoTo errorhandler
If Target.Count = 1 And Target.Column = 1 Then '.count to check if only one cell is selected and .column to check if it is a first column
'UserForm1.Show
'Do whatever you want to do here like opening User form
MsgBox "You clicked in column A"
End If
errorhandler:
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