Hey Id like to know if it is feasible : currently I have a drop down list on sheet1 that contains a list of names being pulled from sheet2 using data validation.
The cell next to that drop down list is populated with a phone number using VLOOKUP from sheet2.
My question : Can I use VBA so that everytime there is a change in the drop down list the cell next to it will become populated from data in SHEET2? keep in mind the value that is selected is needed to pull the proper phone number .
Why am i asking if my file works? Because anyone can accidently delete the VLOOKUP formula and I can not Protect it because it is a shared document that employs several different macros.
Something like this? (Source: https://support.microsoft.com/en-us/help/213612/how-to-run-a-macro-when-certain-cells-change-in-excel)
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A1")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
Range("B1").Formula = "=VLookup(A1,LookupTable,2,FALSE)"
End If
End Sub
Related
I've got an Excel file, where I have to enter names in one sheet, and then the other sheets are adding or deleting rows.
For example I have the excel sheets User, NumberSheet and GradeSheet.
I am entering "John Doe", "Jane Doe" and "Phil Doe" into the User-Sheet, and in NumberSheet and GradeSheet, 3 Rows are added.
I have this code for now:
Private Sub Worksheet_Change(ByVal Target As range)
Dim KeyCells As range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
' Start Cell is A2 because Names start there
Set KeyCells = range("A2:A100")
If Not Application.Intersect(KeyCells, range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
'The row in the user-Sheet, where I added or removed a user
targetRow = Target.row - 1
'In the Number Sheet, the row from where the names should get listed
targetRowInNumberSheet = targetRow
'The value from the cell where I entered the name
vlue = Target.Value
If IsEmpty(vlue) Then
'Delete row in Worksheets
Worksheets("NumberSheet").Rows(targetRowInNumberSheet).Delete
Worksheets("GradeSheet").Rows(targetRowInNumberSheet ).Delete
Else
'Add row in Worksheets
Worksheets("NumberSheet").Rows(targetRowInNumberSheet).Insert
Worksheets("GradeSheet").Rows(targetRowInNumberSheet ).Insert
End If
End If
End Sub
Now this code works if I add a name, delete one or insert a row somewhere in between.
But I can't delete multiple names, or delete a row. In both cases IsEmpty(vlue) will be false, and I don't know why
IsEmpty does not check if a cell value is empty...
It checks if a variable has been initilized.
Instead of If IsEmpty(vlue) Then, simple please use:
If vlue = "" Then
or:
if vlue = Empty Then 'this is different!
Please, check here how Microsoft describes this function...
Then, it is recommended to declare all used variables (targetRow, targetRowInNumberSheet, vlue).
If your Target have to contain only one cell, you should add a code line:
If Target.count>1 then Exit Sub
If you want allowing the multiple cells range, you should state it and deal in a different way with what you try doing. I asked about that, but you did not answer anything. If so, i can show you how to proceed.
Thats because your Input parameter of the Worksheet_Change Function is a Range called Target.
A Range is an area instead a single Value.
If you delete now multiple Rows, the Target.Row property only returns the first Row in your Target Range and so only the First entry will be deleted.
So you should further seek information about Ranges.
I would try this:
Count all Rows in your Target Range:
Target.Rows.Count
With this number you can go on from the first Row Index of the Range like in your example with a loop or something:
targetRow = Target.row - 1
I am having a list of names in a Range A2:A77, in the worksheet name called Manual. whenever i choose a name, that is when a cell gets selected from the range, that active cell value should get reflected in the cell C1. Also, the macro should not work incase if i selected else where, other than the given worksheet or range.
I have googled alot but nothing seem to be matching my criteria, so i'm here, hoping for a better solution. You may ask me to achieve this by using data validation, but for that i will have to do multiple clicks and scrolling work to be done everytime. so to avoid that i'm looking for vba code to minimize the work and time.
Thank You.
I am only just learning VBA at the moment so this could be some very horible code but here goes.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim cells As Range
Set cells = ActiveSheet.Range("A1:A27")
If Not (Intersect(Target, cells) Is Nothing) Then
ActiveSheet.Range("C1").Value = Target.Value
End If
End Sub
Worksheet_SelectionChange is called if the selected cell in the sheet changes then using the test from InRange that I found here: VBA test if cell is in a range test if the cell is within the defined range then set the values.
Edited as sugested by #Vitaliy Prushak in comments.
I have a macro which should be triggered when any cell in the range (s1:s100) changes. Range s1:s100 is populated by another macro and the values keep changing
I have the below piece of code, which works fine when only one cell in the range is changed. If more than one cell changes at the same time, it doesn't work. When more than one cell changes at a time, I get the message "No Change".
Sub Worksheet_Change(ByVal Target As Range)
Dim keyscells as Range
Set KeyCells = Range("S1:S100")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
msgbox Target.Address
'execute my macro
else
msgbox 'no change'
end if
end sub
Any help appreciated!
Added more info
When I edit more than one cell at the same time, say S1 S4 and S8, It will say S1 changed (Just pick the first one). But I need to identify all the three cells that was changed.
Thanks,
Valli
I've copied your modified sub to an Excel 2013 spreadsheet, and I can't see the problem.
If I enter some numbers at the start of col S, then select 3 non-contiguous cells and press delete, then the Target address is the addresses of the 3 cells. Here's a screenshot.
Please provide steps on how someone can replicate your problem.
I have two Excel sheets with the same columns, and I want a way by which if I update rows in one table, the corresponding rows in other tables are automatically also updated, and vice versa for both the tables?
Does a way like this exist? I searched a lot, but could not find the vice versa option.
You'll need to use some VBA to do this. Something like the following will work, which needs to be embedded in the script window for each sheet that contains your data. This assumes your data is in the same location on each sheet i.e. A1:A10.
Private Sub Worksheet_Change(ByVal Target As Range)
'First Column
Set KeyCells = Worksheets("sheet 1").Range("A1:A10")
'Only undertake action if key cells are edited
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
'Edit second column
Worksheets("sheet 2").Range(Target.Address) = Target.Value
End If
End Sub
You'll obviously need to duplicate this to work for changes to sheet 2 and also and embedded the code in sheet 2.
I tried looking at other similiar questions and solutions but as an Excel beginner I couldn't quite figure it out.
So I have the following macro:
Sub Worksheet_Change(ByVal Target As Range)
Dim wsNew As Worksheet
If Target.Cells.Count > 1 Or IsEmpty(Target) Then Exit Sub
On Error Resume Next
If Not Intersect(Target, Range("B46:B99")) Is Nothing Then
ThisWorkbook.Sheets("LT").Copy _
After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
End Sub
It opens a new sheet in the same workbook and I'd need to auto populate certain cells with data from the main sheet. Main sheet: http://i.imgur.com/RJe44hQ.jpg new sheet: http://i.imgur.com/eatbg6j.jpg . The cells I need copied are in red.
Thanks in advance for any help! Really new to all this..
Since you don't specify which value(s) you need to pull from the main sheet I can't get too specific, but in general there are three approaches to take.
1) If the data is in contiguous range(s) of cells on both sheets, you can just copy the data from the main sheet after creating the new sheet, and then paste the values to the correct target range
2) If the data isn't contiguous on both sheets, then your next best option would be to have the value for each target cell set based on the value of the corresponding cell on the main page. Ex: To set A2 on Sheet2 to the value of B4 on Sheet1 you would use Worksheets("Sheet2").Range("A2").value = Worksheets("Sheet1").Range("B4").value
3) This one also works if the data isn't contiguous, but gets to be troublesome if there are more than ~5 values to copy. You can create an appropriate variable (string for text, long/int for numbers, etc.), set that before creating the new sheet, and then use them to set the appropriate cells once the new sheet is created.