Construct a pair of mirrored cells in two different worksheets - excel

I'm trying to create a pair of cells on two different sheets of my workbook, such that anything you enter into one cell gets copied across to the other. If this was one way, it would obviously be trivial. I want to make it so that the link is two-way, though. That's going to involve VBA scripting, and I'm something of a novice at that. Does anyone have some pointers on how to learn what I need to know? I have Googled, but that just produced some code samples without explanation, and I don't want to put in code that I don't understand.

Let's assume you have Sheet1 and Sheet2 and their names are exactly that.
At Sheet1, go to Developer and click on View Code. You should be looking at a blank coding sheet for Sheet1
On the left drop down of the coding area, select Worksheet instead of the default (General)
On the right drop down. select Change. You would automatically be given the following:
Private Sub Worksheet_Change(ByVal Target As Range)
End Sub
Changes to any cell in Sheet1 will trigger the code above.
The cell that has been changed is referred to as the Target range.
You can easily identify the address and content of the Target range using Target.Address and Target.Value. You can then use this address and value to update Sheet2.
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Sheets("Sheet2").Range(Target.Address).Value = Target.Value
End Sub
To copy data from Sheet2 to Sheet1, just repeat the steps above but at Sheet2.
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value = Target.Value
End Sub
However, if change to Sheet1 will trigger a change to Sheet2, which in turn will trigger a change to Sheet1, the changes will happen for an indefinite time which does not make sense. Therefore, we need to add some code to apply the change only when the values on both Sheet1 and Sheet2 are different. If the values are the same, there is no need to apply change. Therefore, the code for Sheet2 should appear as below:
Private Sub Worksheet_Change(ByVal Target As Range)
If ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value <> Target.Value Then
ThisWorkbook.Sheets("Sheet1").Range(Target.Address).Value = Target.Value
End If
End Sub

You would use a pair of event macros to accomplish this. Here is an example that you can adapt:
mirror changes

Related

Hiding rows using a macro

I've got this project I'm working on where I have a "Data" sheet and am pulling the data in to "Sheet 2". I want a row on "Sheet 2" to automatically hide/unhide if there is no data within a specific cell in that row.
I am very new to macros and coding in general. I have tried looking this up but and following the tutorials on YouTube but none of them go too in-depth as to why you select certain things and I haven't had any luck getting them to work.
My question are.:
Would a macro that is effecting "Sheet 2" while I am on "Data", be placed within "Sheet 2" or within "ThisWorkbook"?
Before you write the macro, you need to select the drop downs "Workbook" and then I would assume you are detecting a change so would "SheetChange" be correct?
Will a macro detect a change within a cell that is hidden and then can that cell's row be unhidden?
Lastly, What the heck do I even write for the to detect a 0 and hide/unhide cells, I haven't gotten any of them to do anything so far?
Thank you for any help you guys can give.
To answer your questions:
Would a macro that is effecting "Sheet 2" while I am on "Data", be placed within "Sheet 2" or within "ThisWorkbook"?
It doesn't matter where you place the code unless the code is triggered by a built in event like Worksheet_Change(). In that case then it must reside within the worksheet that you are wanting to detect changes in. If it's not event driven then I would place the code in it's own module. To each their own though.
Before you write the macro, you need to select the drop downs "Workbook" and then I would assume you are detecting a change so would "SheetChange" be correct?
It would be appropriate if you wanted the code contained in that subroutine to be executed whenever there is a change to that worksheet. You could also trigger code through a user action like a button click or a double click or sheet activation, etc.
Will a macro detect a change within a cell that is hidden and then can that cell's row be unhidden?
Sure! Why not? As an example:
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if the change was in a cell we care about (it can be hidden, VBA don't care)
If Not Intersect(Target, Column(3)) Is Nothing Then
'Something changed in Column C! Unhide whatever row had the change
Target.EntireRow.Hidden = False
End If
End Sub
Lastly, What the heck do I even write for the to detect a 0 and hide/unhide cells, I haven't gotten any of them to do anything so far?
Using that last example to detect if a particular column experienced a change, detect if it was just a single cell, detect if that single cell is now a 0 and hide it's row:
Private Sub Worksheet_Change(ByVal Target As Range)
'Detect if the change was in a cell we care about (it can be hidden, VBA don't care)
If Not Intersect(Target, Column(3)) Is Nothing Then
'Something changed in Column C!
If Target.Cells.Count = 1 Then
If Target.Value = "0" Then
Target.EntireRow.Hidden = True
End If
End If
End If
End Sub
Stick that in whatever worksheet you wanting to detect the change. Change that first If so that you are only detecting changes in whatever range you anticipate that change to happen in (I chose Column C as an example).

Office 365 for Mac: Execute VBA macro on table change

I'm currently running Excel for Mac using the Office 365 subscription. I'm trying to execute a VBA macro whenever a certain cell value changes. I've already looked online and saw many code examples using the Worksheet_Change Sub, but it does not work for me however. This is how my code currently looks like:
Private Sub Worksheet_Change(ByVal Target as Range)
Target.Font.ColorIndex = 5
End Sub
I'm trying to run it on the second of four sheets. I clicked View Code on the second sheet so I am in the correct sheet though.
Does somebody know why it does/might not work? Does it not work in Excel for Mac?
If you need more information please tell me.
Thanks
I was having similar trouble earlier, and because your symptoms sound like mine (code not even executing) I suspect your code is actually not on the correct sheet. The VBA window in Mac is much less detailed than in Windows and I find it confusing.
Make sure that in the left-hand pane you click on the appropriate drop down
VBAProject(WorkbookName) -> Microsoft Excel Objects -> Sheet2
and insert the Worksheet_Change function there.
Also, if you want the color to change for only a certain range, you'll need to use Intersect:
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:Z99")
'This part executes only if the cell being changed is in KeyCells
If Not Application.Intersect(KeyCells, Range(Target.Address)) Is Nothing Then
Target.Font.ColorIndex = 5
End If
End Sub

Automatically Run Macro When Data Is Pasted VBA

I have a worksheet where in the first three columns (A,B,C) I store data. These values are used in a macro.
I would like to know how it is possible to make this macro run automatically after data is pasted onto these columns. I am almost sure that I will use the Worksheet-Change module, but as for the code I am clueless.
Thanks in advance.
A simple implementation:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:C" & ThisWorkbook.Worksheets(1).UsedRange.Rows.Count)) Is Nothing Then
'Call your Macro to do stuff
End If
End Sub
Intersect checks if Target is in the range you want to monitor. So if something changes in columns past C, Intersect will return Nothing and your macro won't be called. Just keep in mind that the Worksheet_Change event fires on any change, even double clicking into the cells. If all you are doing in this Worksheet is copy&pasting data and then running your Macro, this should be fine, but if you are manipulating your data further, you might have to look at more sophisticated solutions. Examples include mirroring your Worksheet and comparing it pre/post Worksheet Changed Event. You can read more on this here: Detect whether cell value was actually changed by editing

sound alert in Excel when cell changes

I use a DDE feed to display a value in a cell. I would like to create an alarm to alert when the value in the cell changes. I am not comparing the cell with any other cell and I don't know whether the value will be higher or lower. I just want to hear a sound when the cell receives a new piece of data from the DDE feed.
Is there anyway to do this.
I know to create an alarm...I just don't know how to trigger it on a single cell changing.
Help appreciated.
Thanks.
S
You can use something like this code (just add it in the sheet module):
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Cells.Count = 1 Then
'call your alarm here
End If
End Sub
this code will triggered each time something changed in the sheet. So, you can use IF statement to determine whether only one cell changed and call alarm in this case

Excel VBA - prevent cell from changing upon edit

I would like to prevent a cell from being edited as I need to preserve the formula in it. The problem is, I want to copy paste a large range of values from another workbook over into my sheet, and the target range for pasting includes some cells which must not change. Thus, a simple lock option wouldn't work, as this would prevent me from doing this operation in one go.
I tried using a code wherein the cell changes back to its intended formula upon change of the cell, however, this yields an infinite loop. See simplified example below:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
End Sub
Many thanks for your help, much appreciated.
Edit:
An automated solution that would transfer from this other workbook wouldn't work as I receive these files in various formats and workbook names.
If your problem is infinite loop than here goes quite common solution for the problem. You just need to switch off events for a while and than switch them on back. Your code could looks like:
Private Sub Worksheet_Change(ByVal Target As Range)
'turns off events
Application.EnableEvents = False
If Not Intersect(Target, Range("F28")) Is Nothing Then
Range("F28").Formula = "=if(E28=0,0,G28/E28)"
End If
'turns on events back
Application.EnableEvents = True
End Sub
You have to make a Sub that enters the "resident" formulas. Just make a call to this sub after pasting the new values.

Resources