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

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

Related

Select specific cell after Inputting values In previous cells

so I have an input sheet and want to move the activated cell to another cell after data has been typed in/chosen.
So far I've got this:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Range("F11").Value <> "" Then
Range("F13").Select
End If
End Sub
But the problem is this does not seem to work with more cells and ranges for some reason. So for example if then F13 is selected I want to move to F16. If F16 is selected I want to move to F17. So no rule here like always 2 rows down. How can I solve this?
Best
This is a simply type of a boilerplate, which can be increased further, following a specific business logic.
If you want to Add Range("D5") with 3 rows, then add it to the Union in this line: If Intersect(Target, Union(Range("F16"), Range("F13"))) and then make a Case Range("D5").
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Row > Rows.Count - 2 Then Exit Sub
If Target = vbNullString Then Exit Sub
If Intersect(Target, Union(Range("F16"), Range("F13"))) Is Nothing Then Exit Sub
Select Case Target
Case Range("F16")
Target.Offset(1).Select
Case Range("F13")
Target.Offset(2).Select
End Select
End Sub

Display cell content in a text box in excel using VBA

I have a range of cells with data. I want a text box to show the cell content when I click on any cell in the text box. Is this possible? Thanks
You could just use something like this:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim i As Long
Dim lRow As Long
lRow = Cells(Rows.Count, 1).End(xlUp).Rows
For i = 1 To lRow
If Cells(i, 1).Count = 1 Then
If Cells(i, 1) = "" Then
Else
If Not Intersect(Target, Cells(i, 1)) Is Nothing Then
MsgBox (i)
End If
End If
End If
Next i
End Sub
This will show the value in a message box, not a text box. Not sure why you need a text box.
i refers to the row and change the 1 in lRow = Cells(Rows.Count, 1).End(xlUp).Rows to the correct column number you are working in
Add this to the worksheet (see the black arrow):
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
MsgBox Target.Value
End Sub
In general, if you want to check for a specific range, you can define the range in the event:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rngPreselected As Range
Set rngPreselected = Range("A1:B10")
If Not Intersect(Target, rngPreselected) Is Nothing Then
MsgBox Target.Value
End If
End Sub
In this case, A1:B10 is the defined range.
That's called Event. See more about events here: http://www.cpearson.com/excel/events.aspx

Macro doesnt appear on the List after copy-pasting code

I am completely new to VBA and hence have no idea what I'm doing...
Below is the description of my problem and also the code in question.
What I originally wanted to do was this:
Copy a clicked cell, Select new sheet, Select any cell, Paste as values
Now i found a code that apparently does the trick which is this:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing Then
Cancel = True
If Target.Row > 1 And Len(Target.Value) Then Worksheets("S11").Range("C2").Value = Target.Value
End If
End Sub
I say "apparently" as I am not able to test it. Here is the problem:
I saved the workbook as excel macro-enabled workbook
Clicked ALT Q - to go back to my workbook
I then ALT F8 to run it - but there is nothing there... blank....
what am I missing?
In a module, have the macro you wish to use.
public sub mymacro(r as excel.range)
If Not Intersect(r, Columns("A")) Is Nothing Then
If r.Row > 1 And Len(r.Value) Then Worksheets("S11").Range("C2").Value= r.Value
End If
end sub
public sub wbtest()
mymacro activecell
end sub
and use like so
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Columns("A")) Is Nothing then
mymacro target
end if
end sub

VBA - Open a msgbox when cell value = 1

I need help with a very basic vba macro. When the value in A6 is equal 1 a msgbox needs to appear in the workstation. I developed the below code but the problem is that when I add any other information in the spreadsheet (for example, if I write "i love pizza" in B6) the msgbox will prompt again and I need it to prompt just one time, just when I set the value of A6 = 1. Could you please help me?
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("A6").Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End Sub
#edit
I forgot one very important thing... the value "1" is getted with a VLOOKUP so its not insert manually by the user, i'm sorry about this. I tried the codes you people answered and it worked just when I put the vlue manually and as I said in the edit start, the value is set with a VLOOKUP. Any other tips, please?
You need to check if the change is due to the cell A6 being changed, rather than a different cell.
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 1 And Target.Row = 6 Then
If Target.Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End If
End Sub
You can use this code instead of the previous one
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Me.Range("A6").Address And Me.Range("A6").Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End Sub
Target is the changed cell in Excel. so for every change event, I check if the target is Range("A6").
Pertaining to the statment : and I need it to prompt just one time, you need to save the oldvalue. So the prompt is displayed only once, when you set the value to 1. If A6 is already 1 and then you type 1 again, no prompt.
Option Explicit
Dim oldVal
Private Sub Worksheet_Change(ByVal Target As Range)
If (Target.Address = "$A$6") Then
If Target.Value = 1 And Target.Value <> oldVal Then
oldVal = Target.Value
MsgBox "Test"
End If
End If
End Sub
You need to check inside the Worksheet_Change event, that only if Cell "A6" is pressed, then continue. And afterwards, check if the value of the cell equals 1.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A6")) Is Nothing Then
If Target.Value = 1 Then MsgBox "THIS IS A MSGBOX."
End If
End Sub
Try this:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$6" Then
If Target.Value = 1 Then
MsgBox "THIS IS A MSGBOX."
End If
End If
End Sub
I forgot one very important thing... the value "1" is getted with a VLOOKUP so its not insert manually by the user, i'm sorry about this. I tried the codes you people answered and it worked just when I put the vlue manually and as I said in the edit start, the value is set with a VLOOKUP. Any other tips, please?

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