Automatically Copying Value & Format - Excel - excel

Automatically Copying one/many cell's value & colour format to another cell/range of cells specified by myself. The other cell also needs to update every time the first cell changes its value or format.
I've been able to see just one or two threads with answers to this question but it still doesn't serve my purpose. Can any genius help me out in here? I don't quite understand why a basic thing like this has taken me 1 full day to figure out (the answer shouldn't be that I'm not smart :D)
Attaching an Excel example of what I want to achieve.
I want to be able to;
Update the Sheet 2 with the data from Sheet 1. (C4 in sheet 2 has to be green and have the value 5). Also, if C4 in Sheet 1 changes its value and colour, I want C4 in Sheet 2 to change automatically)
Now, the above is not only my concern. I have many cells in Sheet 1 which I want to be able to select and have them copied into Sheet 2 in respective places. (eg - c4, d4,e4 from sheet 1 ,.etc to be copied into c4, d4, e4 in sheet 2). Not necessarily I would want to choose the cells in a sequential fashion, but if there's a way for me to specify which cell needs to be copied into which cell of the other sheet, I'll be even more convinced.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim ping As Boolean
If Intersect(Target, Range("A3")) Is Nothing Then
If ping = False Then
Range("A3").Copy
Range("C10").PasteSpecial Paste:=xlPasteFormats
End If
ping = True
Exit Sub
Else
ping = False
End If
End Sub

One solution can be with a user defined function. I could not try the code, but just to show the idea:
Public Function CopyFromTo(rngFrom As Range, rngTo As Range)
Application.Volatile True ' "A volatile function must be recalculated whenever calculation occurs in any cells on the worksheet"
rngFrom.Copy rngTo
CopyFromTo = rngFrom ' I am not sure what the function should return
End Function
For example, formula in cell B2 would be =CopyFromTo(A2, B2)

Related

Runs once, then never again

I posted a question to Stack Overflow about creating a Excel Macro to add/subtract a value in a column from the previous column, then clear the current column.
Excel cell value update macro
The code worked find for years in office 2007, but no longer works in office 365 (2016/2019).
If F1 has a value of £100.00, and I type -50 into G1, F1 changes to £50.00 and G1 is empty.
Each cell in column F is changed by an amount entered into the row in column G.
In Office 365 versions of Excel this macro executes once then won't run again until I close an re-open the document. Has something changed about the way macros work that would cause this?
Edit:
Here is the code (attached to sheet 1), slightly modified from my previous question to work on column G.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FirstNum As Currency 'Long is for number currency should help keep format
Dim SecNum As Currency
If Target.Column = 7 Then 'Only Runs if Cel l being changed is in column C, Might need to be Columns
FirstNum = Target.Offset(0, -1).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(0, -1).Value = FirstNum + SecNum ' Makes Cell B equal to difference of previous value and value typed in C
'MsgBox ("Difference Found") ' Just to display code worked Remove when confirm code works
Target.Clear ' Clears Value you typed
End If
End Sub
I don't have enough rep to do a comment...
Without seeing your full code it's hard to know. But if your code has
application.enableEvents = false
You will need to to set it back to True eventually. Or worksheet change events won't fire off until the workbook is closed and opened again
I think it is nothing wrong with Office 365...
In order to work as you explain, the code must look like:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim FirstNum As Currency
Dim SecNum As Currency
If Target.Column = 3 Then 'Only Runs if Cell being changed is in column C, Might need to be Columns
FirstNum = Target.Offset(0, -1).Value ' Value in Column B
SecNum = Target.Value ' Value being typed in C
Target.Offset(0, -1).Value = FirstNum - SecNum ' Makes Cell B equal to difference of previous value and value typed in C
Target.Clear
End If
End Sub
But, you must understand that this code works only for changing cells in column C:C.
The variant you received six years before worked only in the same column, but on vertical. I mean, changing of C2, it used to modify C1. But it had a bug. If you try modifying of C1 it will try to set an Offset of -1, row zero does not exist in Excel and returns an error...
I cannot understand how the event functioned at least once for ranges G1, F1. It could work only if some links exists in the sheet, or you adapted the code and in such a case the real used code will give us the possibility to analize and tell you, not supposing, where the problem is. I do not think it is a matter of Office version, but who knows...
It sounds like a formula would suffice. However a cell cannot refer to itself, you could try,
E1: =F1+G1
Where E1 is a new cell with the sum of F1 and G1, because you are entering its sign operator i.e. - or + the you can leave it as addition in cell E1. To use this for a column just drag from the lower left square on the cell E1 all the way down to the row number you want. This will refresh every time a value changes.
excel image

Auto-populate cell, based on another cell's value, without formulae

I've seen a couple of spreadsheets over the years that had a blank, unpopulated, non-formula cell, that would populate when another cell was populated correctly. I am wondering if there is a way to do this without using add-ons, or VBA.
Scenario:
User is asked to enter a value in cell A1.
If the value is X, cell B1 populates with a value.
If the value is Y, cell B1 remains blank.
I know that this can be done with a formula such as =IF(A1="","",IF(A1=1234,"Hello 1234","")).
However, I am wondering if it is possible to do this without a formula in cell B1, but still have cell B1 populated?
From your description, it sounds like this might be what you witnessed. Macros can be set to trigger automatically given a certain event & criteria met. In this instance, the macro will fire when you make a Worksheet_Change in cell A1.
Note that the change to A1 must be manual to fire macro - a change due to formula will not suffice to trigger macro
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target = "X" Then
Range("B1") = "X Result"
ElseIf Target = "Y" Then
Range("B1") = "Y Result"
End If
End If
End Sub

need formula with a temp variable

we have all added information in our heads and then written the results down,
for example a simple tally, yesterday I sold 10 apples, the day before I sold 5 for a total of 15, today I sold 5 for a total of 20, a simple equation we all do every day in our head without even thinking about it, the formula I believe would basically read: A+B=Bnew
where A would be the daily sale.
B would be the total sale
and Bnew is the new total.
how do I do this in excel without taking pages of running total lines, or fancy visual basic script.
I want to use at most 3 cell's
Cell-1 = changing variable, (the daily sale)
Cell-2 = the running total of all sales to which A1 will be added.
and if needed Cell-3 to hold the contents of Cell-2 like our memory holds it while we update the total.
a very simple math problem, but driving me nuts to try to get excel to do it, I have searched and searched but I don't even know the right question to ask.
thank you for your help
Given the restrictions:
No helper columns ("without taking pages of running total lines")
No VBA ("no fancy visual basic script")
"I want to use at most 3 cell's"
I think a remaining option is iterative calculation? I don't know, maybe someone else can think of a better solution. But regarding iterative calculation, here's how I've set my sheet up:
This uses two cells (ignoring the labels in row 1):
In cell A2, I have the formula =A2+B2
Cell B2 is blank (ready to have some number entered)
I then go to File > Options > Formulas > Enable iterative calculation > Change "Maximum Iterations" to 1 > OK
Any notifications regarding circular references should now disappear, and if I put a number in cell B2, cell A2 updates in the manner you described in your post. Hopefully, you can replicate this on your machine too.
The thing to note is the iterative calculation setting you'll change is application-wide, so I believe it affects all other workbooks. Something to keep in mind.
If your setup looks like this in Sheet1:
then a small vba macro in the worksheetmodule from Sheet1 should do the trick:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = "$A$2" Then
Range("B2").Value = Range("B2").Value + Range("A2").Value
End If
End Sub
This subroutine will run after every worksheet change. It will check if the change was in cell A2. If that's the case the value in A2 will be added to the value in B2.
Assuming you're okay with a small amount of VBA code, you could try putting the code below into the worksheet module for the sheet where you're entering the data:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
' Assumes "daily sales" are entered into cell B2 and that "running total" is in cell A2
If Not (Intersect(Target, Me.Range("B2")) Is Nothing) Then
Me.Range("A2").Value2 = Application.Sum(Me.Range("A2").Value2, Target.Value2)
End If
End Sub
(Pressing Alt + F11 concurrently should open the editor, you can then find worksheet modules on the left).
If you then enter a number in cell B2, then cell A2 should update in a cumulative manner. If you enter a number in a cell other than B2 or A2, you should find cell A2 doesn't update (unlike the iterative calculation approach). We use Application.Sum instead of + operator to avoid Type Mismatch error, if either A2 or B2 contain non-numeric data.
Try it, see if it does what you're after.
Edit:
If you have multiple rows, you could try something like below.
Private Sub Worksheet_Change(ByVal Target As Range)
' Assumes "daily sales" are entered into column B and that "running total" is in column A
If Target.Column = 2 Then
Me.Cells(Target.Row, "A").Value2 = Application.Sum(Me.Cells(Target.Row, "A"), Target)
End If
End Sub

Excel sheets dependant on / mirroring each other

Let's say I have three sheets in Excel. Sheet 1 is the Master sheet, sheets 2 and 3 contain different sets of information with the same headers that feed into the table in the master sheet. Is there a way to make it such that I could edit information in sheet 1 and sheet 2 will change AND vice versa so I can edit info in sheet 2 that will update the master sheet?
You could solve it by having Vlookup-formulas in your Master sheet. That way, if you change anything in sheet 2 and 3 the Master will automatically be updated.
If the user changes anything in the Master sheet, you will have to build logic in VBA on that. One way to go is to format the Master sheet so that there is something that helps the VBA know what the formula should be in the edited cell, and also to know from where the data should come. Loosely one could set up the Master sheet like this:
Row 1 is hidden and contains the template formulas
Row 2 is hidden and is completely empty (this will make less problems with filtering)
Row 3 contains headers
Row 4 and down contains the data, using the formulas define in row 1
Add the Change event on the Master sheet, that sees if the changed cell was one with a formula. If so, it will examine the template formula to identify from where the data should come. Then it will update that cell in Sheet 2 or 3 with the new value that is entered in the Master sheet. After this, it will overwrite the value manually entered in the Master sheet with the formula from the template row.
The big job here is to write a parser that understands from which cell the vlookup will get it's value.
One thing that I overlooked is that the CHANGE event is triggered only ONCE if the user pastes several cells in one go. The TARGET will then contain several rows or columns.
So this is some kind of skeleton using the above idea...
Option Explicit
Dim ChangeEventDisabled As Boolean 'Flag for disabling the Change event
Public Sub Disable_ChangeEvent()
ChangeEventDisabled = True
End Sub
Public Sub Enable_ChangeEvent()
ChangeEventDisabled = False
End Sub
Sub Worksheet_Change(ByVal Target As Range)
Dim updatedValue As Variant
Dim SourceCell As Range
'While the MasterSHeet is populated intially, we don't want this event to do anything
If ChangeEventDisabled Then
'There are chenges being done in teh sheet that should not trigger updates of the source-sheets.
Else
'Only run the code if it was a data-cell that was changed
If Target.Row > 3 Then
'We are in the rows containg data
'Did the changed cell contain a Vlookup formula before the user changed the cells value?
If UCase(Cells(1, Target.Column).Formula) Like "=VLOOKUP(*" Then
'A vlookup normally populates this cell.
'To know from where the data normally comes, I will need to put back the formula in the changed cell.
'So, first save the new value that we will write in the source cell
updatedValue = Target.Value
'Insert the formula again in the cell
'As we will now CHANGE a cell in the Masterr sheet, a Change event will trigger. Disable it temporarily
Disable_ChangeEvent
Cells(1, Target.Column).Copy Destination:=Target
Enable_ChangeEvent
'Find out from which cell the data is being fetched by the Vlookup
Set SourceCell = MyMagicParsing(Target)
'Update the source-cell with the new value
SourceCell.Value = updatedValue
End If
End If
End If
End Sub
Function GetSourceCell(Target As Range) As Range
'This function should decipher the formula in the cell Target, and figure out from where
'the data is actually coming. It shoudl return the range which is the source of the data.
'As I dont know how to do that quickly, I just hardcode the cell that is the source.
GetSourceCell = Worksheets("Sheet2").Cells(67, 3)
End Function

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