I have been looking for a straightforward and reliable method to do sendkey "F2" on range of cells.
I want users to be sent directly to edit mode when selecting certain cells, which will allow them to copy or enter data in a more user friendly manner.
Kind regards,
Luke
Thanks for your help everyone. Found the answer I was looking for on another forum.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.CountLarge = 1 Then
If Not Excel.Application.Intersect(Target, Range("CertainCells")) Is Nothing Then
VBA.SendKeys "{F2}"
End If
End If
End Sub
I am not sure if it is a good idea to change into "edit mode" for some specific cells. The only difference is that the cursor gets visible and jumps to the end of the cell content if the cell is not empty. While it may be convenient to immediately append data, the price is that it is not possible to simply press Ctrl+C to copy the cell content. However, I will not argue with you.
As already written in the comment, use the Selection_Change-Event and check the parameter target if one of your "certain cells" was selected. If target.count is greater that 1, the user selected multiple cells - you probably don't want to enter Edit mode in that case.
The following example sends F2 if a cell in column B was selected - adapt it to your needs. You need to put the code into the sheet module of the sheet where you want the magic to happen and change the If-condition to your needs.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Target.Column = 2 Then ' Change this to check for the cells you are interested in
Application.SendKeys "{F2}"
End If
End Sub
Related
I have the following task: I want to write a macro that can be activated when you click on a specific cell and hides a certain number of rows under it.
I suppose that I need to solve the following problems:
I need to call macros when I select the cell.
We need to detect the exact coordinates of selected cell.
The final point: we need to hide N rows under the selected cell.
The first point works good because I found the code:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Selection.Count = 1 Then
If Not Intersect(Target, Range("D4")) Is Nothing Then
MsgBox 'you clicked d4!'
End If
End If
'End Sub
But when I try to replace the MsgBox 'you clicked d4!'for:
callingCellRow = Application.Caller.Row
It does not work Error:
the variable is undefined
Can you give some examples or provide the code which combines these thirds points?
I am working on a spreadsheet to copy a users data from the previous day, then delete any numerical values but keep cells with "NA". What I want is for users to not be able to change/delete the cells that still have "NA" in them. I found some code that used OFFSET to move down one cell if a certain cell was selected (based on the row and column) but I haven't been able to figure out how to use the OFFSET to move down one cell if the current cell contains "NA". (https://www.extendoffice.com/documents/excel/3820-excel-lock-cell-without-protecting-sheet.html) This worksheet is already locked with a Quality-set password, so I can't do anything to unlock the spreadsheet, then select the "NA" cells to be locked, then relock the spreadsheet, thus looking for a creative way to keep the cells from being selected or changed. Also, the code would need to run all the time, not just when a macro was selected to run. Any ideas?
If it's possible for the user to open the book without macros enabled, then I'm not sure what you're asking is possible.
If you can assume macros are enabled though, you could use events to either prevent the user selecting the cell (similar to the OFFSET you mention) or you could track changes manually onto a hidden tab in order to note changes and deal with them as you see fit. There are many ways you can achieve the latter, just search "VBA tracking changes to a sheet" etc.
This is how you'd use the OFFSET method:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Cells(1, 1).Text = "NA" Then
Beep
Cells(Target.Row, Target.Column).Offset(0, 1).Select
End If
End Sub
Keep in mind though, this is a very simplistic method. It won't prevent users selecting multiple cells (a range) and deleting the contents. Nor will it prevent values being pasted to range that includes the 'NA'.
UPDATE:
The following is an improved version that will at least prevent users from selecting multiple cells (for pasting into or deleting) if one of the cells contains "NA".
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim testarea As Range
Set testarea = Intersect(UsedRange, Target.Cells)
If Not (testarea Is Nothing) Then
Application.EnableEvents = False
For Each cell_to_check In Intersect(UsedRange, Target.Cells)
If cell_to_check.Text = "NA" Then
Beep
Cells(cell_to_check.Row, cell_to_check.Column).Offset(0, 1).Select
Do Until Selection.Text <> "NA"
Selection.Offset(0, 1).Select
Loop
Exit For
End If
Next
Application.EnableEvents = True
End If
End Sub
This is still slightly flawed however, as it is still possible to drag-fill cells from other areas over a cell containing "NA".
I have a large spreadsheet and have validation on several columns that have drop down lists.
I have the below VBA code in place that restricts user from hitting the delete button and blanking out cell in column with drop down. This works nicely but it does not prevent user from copying a cell from another column and pasting over the dropdown. Below is the code for one column.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
If Len(Target.Text) = 0 Then
MsgBox "You must select an item from the list!"
Target.Select
Application.Undo
End If
End If
Please advise if there is a way to limit copy and paste to the same column.
The spreadsheet that I am working with is for users to compile a large list of data, and I want to maintain data integrity with the drop down lists and validation of lengths etc. Once they are done I will take and using SSIS load the application with data in various tables as a speed load.
This is the only missing ingredient that I am needing. I am not a master at VBA which is why I am asking you.
From the code present in Excel VBA How to detect if something was pasted in a Worksheet you can adapt it to have something similar to this:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim lastAction As String
' Get the last action performed by user
lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
' Check if the cell was cleared or the last action was a paste
If Len(Target.Text) = 0 Or Left(lastAction, 5) = "Paste" Then
MsgBox "You must select an item from the list!"
Target.Select
Application.Undo
End If
End If
End Sub
Tip: You can also detect other actions performed in the sheet. To do so just print lastAction to a MsgBox or Debug.Print and catch the ones you need.
HTH ;)
I'm currently trying to write a macro based on sheet change, where the letters in a table column are automatically converted to upper case. So, for example, if I entered "abcde-12345-678" into a cell, it would automatically correct to "ABCDE-12345-678". After doing some digging, I found some code that has worked for some people, but I'm having trouble tweaking it to suit my needs.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("E:E")) Is Nothing Then Exit Sub
Application.EnableEvents = False
Target = UCase(Target)
Application.EnableEvents = True
End Sub
There are two things that I would like to address. The first being, that this code isn't currently working for me. I have it in the correct location according to the author (located in the Sheet1 object). Are there any ideas as to why this isn't working?
The second is that I would like to modify the code to refer to a table column rather than a range. For example, I've tried changing the second line of the above code to the following (the name of my table is ReviewTracker, and the column I'm interested in is Product Number):
If Intersect(Target, Range(ReviewTracker[[#Headers],[Product Number]])) Is Nothing Then Exit Sub
This returned a compile error "Expected: list separator or )". So there is obviously something wrong with it, but hopefully it might help illustrate what it is I'm trying to accomplish.
Thanks in advance for any help on the issue.
-Sean
First. You can have events disabled due to lots of reason. Let's make it sure that events are on which you can do as follows:
go to VBA Editor >> open Immediate Window >> write there: Application.EnableEvents = true >> press Enter
Second. To check if intersection match appropriate column within you ListObject table you need something like this:
If Intersect(Target, Range("ReviewTracker[Product Number]")) is Nothing Then
assuming that ReviewTracker is table name and Product Number is table column. You don't need #Headersas it will refer only to header row.
What UCase does is converting all the characters in a given string into upper case and thus you can apply it to any Range.Value. Worksheet_Change is called every time the value of a cell has changed and thus is a good place to put your code. But the way you are using to refer the table is wrong. The code your posted adapted to your requirements:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Sheet1.ListObjects("Table1").ListColumns(1).Range) Is Nothing Then Exit Sub
Target.Value = UCase(Target.Value)
End Sub
It converts into upper caps any string input in the first column of Table1 in Sheet1. It has to be placed in the Sheet1 object file (in the VBA Project explorer: Sheet1 (Sheet1) inside the Microsoft Excel Object folder). Adapting it to your actual conditions is straightforward.
After all, I already have the dropdownlists, the dependencies etc. and it works well, ALSO I have a vba code that, when the user change one value from the dropdownlist parent, the dependence clear their contents. BUT
That only works with that cell...
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Not Intersect(Target, Range("C2")) Is Nothing Then
Range("D2").ClearContents
End If
End Sub
Obviusly because I'm tellin vba that only C2 & D2, but What I want it's that somebody would help to figure it out how to make it the entire column, not just that specific cell, like (column - 1 ) or something... because if I copy paste those dropdownlists only works in the first one cause that is specified...
Anybody? Any ideas? please.
Here there are some pics
In the pictures above, the dropdownlists work only in that specific cell, I tried what Hol told me with the function Cells(row index,columnIndex) but I need a for or something like that isn't it ? This is the first thing I'm doing in vba so I dont have a clue and I'm looking for examples and then trying, it takes too long hahaha,
I've already tried instead of "C2" , Column(3) and in D2 Column(4) but I have an error in the conditional If Not Intersect(Target, Range(Column(3))) Is Nothing Then
As far as I understand you want your macro to works in pairs: any change in C column (as of 2nd row bottom direction) will clear cell in D column in the same row. If so the following code does the trick.
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count > 1 Then Exit Sub
If Target.Column = 3 Then
Target.Offset(0, 1).ClearContents
End If
End Sub