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.
Related
I am trying to code my spreadsheet to react to changes to a specific cell in my spreadsheet. This cell contains a formula so the programing is not recognizing any change to the cell although the number is updating the formula is not. I am looking for a way to return the results of the cell containing the formula into another cell as a value so the change can be recognized by the code.
The change event isn't firing because the contents of the cell aren't changing, just what it displays (the formula result) is. You could use the Worksheet_Calculate event and check the value against another static value. If it's changed, then update it and trigger your other code.
It sounds like there's a better way to design your sheet though.
Private Sub Worksheet_Calculate()
Dim watchCell As Range ' set to something
Dim checkCell As Range ' set to something
If checkCell.Value = watchCell.Value Then Exit Sub
' Value has changed. Update the check and trigger action.
checkCell.Value = watchCell.Value
Call SomeOtherResponse
End Sub
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.
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.
I have a spreadsheet with a large amount of data in. About half the cells are merged horizontally with other cells and contain names e.g. John Doe.
Does anyone know how to write a macro to unmerge the cells while distributing the value of the cell to all the cells that were previously merged?
Cheers
Jack
EDIT: The reason I am doing this is to check to see if two adjacent cells are equal i.e. is A1 = A2. But I run into problems when either cell is merged. If anyone knows a way around this problem without separating the cells and copying the data that would be even better!
The idea I provide below is tested for Excel 2010 VBA Win7. However, being not sure I hope it should work as for Mac, too (as this is rather set of standard properties and methods of Range object). If this doesn't work please let me know to delete my answer.
This simple code will work for selected area however it's quite easy to change it to any other range. Some other comment inside the code below.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
Cell.UnMerge
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
And the picture presenting before and after result:
I was able to get the solution from KazJaw to work on a mac with one edit, changing Cell.UnMerge to
ActiveSheet.UsedRange.MergeCells = False, as provided by Ron Debruin here: http://www.rondebruin.nl/mac/mac027.htm.
Sub Unmerging_Selection()
Dim tmpAddress As String
Dim Cell As Range
'change Selection below for any other range to process
For Each Cell In Selection
'check if cell is merged
If Cell.MergeCells Then
'if so- check the range merged
tmpAddress = Cell.MergeArea.Address
'umnerge
ActiveSheet.UsedRange.MergeCells = False
'put the value of the cell to
Range(tmpAddress) = Cell
End If
Next
End sub
I have a macro that runs when any cell is changed, and I want to sum only the shown cells, so if someone filters the sheet the macro will run and sum just the visible cells.
I found the SpecialCells(xlCellTypeVisible) function but I can't manage to make it work.
My idea of the code is something like this:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim Rcells As Range
Dim sum as Double
sum = 0
For each Rcells In Range("A5:A65536").SpecialCells(xlCellTypeVisible)
sum = sum + Rcells.Value
Next Rcells
Sheets("aSheet").Range("B1").Value = sum
End Sub
I managed to sum just the visible cells but it does not run automatically.
Why is it not working?
The two key two steps from my article Trapping a change to a filtered list with VBA which includes detailed instructions and a sample fle:
A second dummy WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet (i.e. the range you want to capture the filter on).
A Worksheet_Calculate() Event is added to the dummy WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed on your main sheet.
Check out the subtotal function. enter =subtotal(
then hit the fx to understand how it works.