excel clear dependent dropdownlist vba - excel

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

Related

excel VBA Sendkey F2 for range of cells

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

Macros which hides the rows through the cell activation

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?

Using Target to Recognize a Cell Location

I run into this issue a fair amount and am curious if someone can tell me why or how I can write this a little cleaner.
Below is my code and it does work.
If Target.Row = rTime.Offset(0, 1).Row Then
If Target.Column = rTime.Offset(0, 1).Column Then
cboStatus.Activate
End If
End If
How come I can’t just write it like this?
If Target = rTime.Offset(0, 1) Then
cboStatus.Activate
End If
If target is already a range then why do I need to specify the individual row and individual column? That second code will not work and I have tried many variations of it. I even tried something like If Target.Range = range(“C4”) Then or If Target.Range = cells(4, 3) Then, but neither of those worked either. I tried many variations of similar stuff. Although, I don’t want to use a specific range like A4, since I wanted to use the rTime like what is in the example, but I was just trying to figure this out.
Nothing seems to work, other than specifying the individual row and column each time. Can someone please explain this to me? Also, is there a better way to write this than what I did in the first example, which does work?
Thanks for anything that relieves my confusion.
The default property of a range object is .Value so when you say If Target = rTime.Offset(0, 1), it will always compare the values in that range rather than the address of those ranges.
One way is already shown by L42. Here is another way using Intersect
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then cboStatus.Activate
EDIT
When you say Target.Column and Target.Row, you will always get the first column and the first row of the cell in that range, even if Target has multiple cells. To avoid this use the below to ensure that you have the desired Target. Your code will give you unexpected results even if there is a single cell in Target. For example, say the value of cell B1 is equal to any other cell which at the moment is target. So if Cell B1 = "Sid" and Cell F1 = "Sid" and you select cell F1 then you will get the "Hello World" message box.
For xl2003, you can use an additional check
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim rtime As Range
Set rtime = Range("A1")
If Target.Cells.Count > 1 Then
MsgBox "you have chosen more than one cell"
Exit Sub
End If
If Not Intersect(Target, rtime.Offset(0, 1)) Is Nothing Then
MsgBox "Hello World"
End If
End Sub
For xl2007+, replace Target.Cells.Count with Target.Cells.CountLarge
For L42
Your method is correct but then you also will have to put the above check to get the correct results.
try this:
Edit1: To cover Chis' concern
If Target.Address = rtime.Offset(0,1).Address(,,,True) then cboStatus.Activate
you cannot compare objects, just the properties? I'm not certain though.

Automatic Text Capitalization Excel VBA

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.

Workbook_SheetChange not triggered by change from formula in other worksheet

I'm trying to imlpement a code that displays a message when a certain condition is met. In this case, it should happen when Sheet2's A1's value is equal or bigger than 1000. This value, on the other hand, is defined by a formula located in Sheet1. I tried implementing a solution based on this thread: How can I run a VBA code each time a cell get is value changed by a formula?
So I got this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim updatedCell As Range
Set updatedCell = Range("A1")
If Not Application.Intersect(updatedCell, Range("A:A")) Is Nothing Then
If updatedCell.Value >= 1000 Then
MsgBox "Something requires attention"
End If
End If
End Sub
When I change the value of A1 through something from Sheet2, it works, but if for example I define it as =Sheet1!A7, and change Sheet1's A7, nothing happens.
How could I make it work?
Well, the linked thread deals with the problem that you want to find out the cell that is recalculated by the current change. (Anyway, the Dependents method only works for formula on the active sheet so this would not work across sheets).
In your case, you already know that you only want to monitor one specific (formula) cell.
So, I'd simply go with this:
Put this code in sheet 1 if you know that Sheet2!A1 only depends on values on sheet1.
Just catch all changes and look at your cell each time:
Private Sub Worksheet_Change(ByVal Target As Range)
If Worksheets("Table2").Range("A1").Value >= 1000 Then
MsgBox "Something requires attention"
End If
End Sub
Make sure that you use Worksheets(...).Range - a blank Range can be the source of sleepless nights of error-hunting, it refers to the worksheet where the code is located if the code is in a worksheet module and to the active sheet if it is in another code module.

Resources