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?
Related
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
I'm making calculation form where you can enter element quantity and select materials from drop down list on sheet1 (all info will go to the sheet2) and in sheet2 you can change same materials that were set in sheet1 to other from drop down list and it automatically update info in sheet1. I have the code that works perfectly(see code below) and it works both ways.
Question is that the code for checking and changing the values is ok with me but it has to to be repeated for about 10 different positions(ranges) that are defined next to 51 different element calculations and it need to be entered 1000+ times. If there is possibility to somehow group that 10 position with one code similar to "loop" then it would be repeated just for 100+ times :)
the idea is to be able to adjust "materials"in both sheets see picture
enter image description here
Main code is in sheet and yes it is _change
calculation sheet (sheet1)
Private Sub Worksheet_Change(ByVal Target As Range)
Call FloorElementExchange1
End Sub
main sheet(sheet2)
Private Sub Worksheet_Change(ByVal Target As Range)
Call FloorElementExchange2
End Sub
and all another code that actually do the check and replacement of value is in module (sub FloorElementExchange1()) that goes like this:
sub FloorElementExchange1()
If Worksheets("CALCULATION").Range("N67") <> Worksheets("main sheet").Range("F44") Then
Worksheets("main sheet").Range("F44").Value = Worksheets("CALCULATION").Range("N67").Value
End If
sub FloorElementExchange2()
If Worksheets("main sheet").Range("F44") <> Worksheets("CALCULATION").Range("N67") Then
Worksheets("CALCULATION").Range("N67").Value = Worksheets("main sheet").Range("F44").Value
End If
give this a try , your If condition isn't needed here
Sheet1 :
Worksheets("main sheet").Range("F15").Value = Worksheets("CALCULATION").Range("N17").Value
Sheet2 :
Worksheets("CALCULATION").Range("N17").Value = Worksheets("main sheet").Range("F15").Value
Note : Your check is useless and waste time .. Try without this will be quite faster
Users will enter new information into a sheet using an userform. This information is tied to a unique ID given at time of entry (i.e. 2019-7 for the 7th item in this year). Currently, each piece of information is placed into separate cells in a row. I want to hide some of these cells but be able to allow users to click on the unique ID at the start of that row, which will run a macro that will create a new sheet to display all information in a user friendly way.
I have tried creating a hyperlink to a run a macro but the code for identifying the hyperlink can't be cell specific. I need a more dynamic way to have excel recognize which ID was clicked and then use that to gather the rest of the information in that row (i.e. the hidden columns).
My best option had been to put a private sub in the worksheet to recognize when a cell was changed/clicked but couldn't get past it just identifying the cell. I need it to then identify the contents of that cell, which would be the unique ID.
I have no issues with creating a hyperlink to nothing, I just need excel to run a macro when a hyperlink is clicked and then within that macro, identify the unique ID and/or row that was clicked. Once I have the ID or Row identified, I then can go from there to grab the rest of the info in the other columns.
You can use the BeforeDoubleClick event of the Worksheet:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target.Address <> "$A$7" Then Exit Sub
' below, call the functions/subs that produce your output worksheet
MsgBox "You clicked on " & Target.Address
End Sub
Above only works on cell A7. If you need this to apply to entire column A, modify slightly:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Intersect(Target, Range("A:A")) Is Nothing Then Exit Sub
' below, call the functions/subs that produce your output worksheet
MsgBox "You clicked on " & Target.Address
End Sub
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 ;)
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