Excel conditional formatting using ADDRESS function - excel

In my excel file you can make changes to the content. The cells which are changed should be marked with a different styling (different background color).
I thought i could solve it like
Create 2 worksheets with the same content
Create a conditional formatting condition. When the value of Cell A on Sheet 1 is different from the value of Cell A on Sheet 2, the formatting rule fires and the background color is changed.
This does work, for 1 cell. However, I would like it to apply on the entire sheet. The only way that i can make it work for now is to create a formatting rule for each cell which takes a lot more time (in EPPlus, C#).
I tried an unequal to condition like 'Sheet2!B2'. Can i make this function dynamic? Calling the ADDRESS function does not seem to work in this condition.
I would like to generate the sheet using EPPlus

Have you tried first removing the '$' signs in the conditional formatting formula for A1 and then selecting the entire sheet in 'Applies to' in the Conditional Formatting dialogue - in that order (if that can be done from EPPlus).
But it might be worth considering a completely different approach that does not require a copy of the sheet. I would add a Worksheet.Change event to the workbook module 'ThisWorkbook':
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
Target.Interior.ColorIndex = 4
End Sub

Related

Copy paste values in Excel multiple selections

I have tried to make multiple selections in an excel sheet where there are formulas in each cell that is highlighted as shown below. I have tried copy/paste these cells, so they only show hardcoded values instead of formulas. Unfortunately, Excel doesn't allow this type of action when you have multiple selections. Is there a way to have multiple selections (as shown below) in Excel and copy only the selected cells and then paste the values with only having to use the paste special option once?
I don't think there is any other way than using VBA in this case.
This code worked for me:
Sub myPasteSpecial()
Dim cell As Range
For Each cell In Selection
cell.Value = cell.Value
Next cell
End Sub

Change variable from multiple sheets?

I have an excel workbook with multiple sheets that aggregate costs and revenues of different technological components (set up in different sheets) in a system.
I want to have a main worksheet, where users can change a small selection of important variables from the technology sheets. I also want those important variables to be defined and editable on the technology sheets.
I've been using named ranges to manage variables, but I'm not sure how to link two cells on different sheets to one variable. For example, I want to name a variable "oilprice" that is referenced in different formulas. I want to be able to change the variable "oilprice" from the main worksheet and the electricity technology sheet in my workbook.
Similarly, I want to be able to check a box on both sheets for "Turn on Electricity" and have the checkbox on the other sheet change as well.
I've been looking around on google and stackoverflow but can't find an answer. Thanks!
Named range, option 1: "override" style formula
With named ranges, you are not able to update the value from multiple cells. You could use logic in a formula to look at a "override" cell and pass that value to the actual named range. This works if the number of overrides is small. That style of formula looks like:
=IF(ISBLANK(oilprice2), oilprice1, oilprice2)
Where oilprice1 and oilprice2 are the cells that hold possible values. Note that there is an implied order in these which can get confusing after a while. That is, if oilprice2 has a value, it will not change oilprice1 nor will oilprice1 be considered.
Named range, option 2: scroll bar or spin control
Another option similar to the checkboxes below, is to use a spin control or scrollbar control to update the values. Those work across multiple sheets.
Checkboxes across sheets
For the checkboxes, this is handled by the Cell Link. You can set as many checkboxes as you want to control a single cell's value.
Here is an example with two checkboxes sharing the same Cell Link = $C$2. They both change when one is clicked.
Okay, I figured out how to have two cells to refer to the same value.
I named a range "oilprice" on the "electricity" sheet.
The cell to input oilprice on the "main" sheet has the formula "=oilprice" and is named "oilprice2", showing the value on the "electricity" sheet "oilprice" named range.
Then I made the following vba code which updates the "oilprice" cell on the electricity sheet when you change the "oilprice2" cell on the main sheet and reverts back to the formula showing "oilprice":
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Target.Worksheet.Range("oilprice2")) Is Nothing Then
Application.EnableEvents = False
Worksheets("electricity").Range("oilprice").Value = _
Worksheets("main").Range("oilprice2").Value
Worksheets("main").Range("oilprice2").Value = "=oilprice"
Application.EnableEvents = True
End If
End Sub

Linking cells to each other; having cells update linked cells and vice versa

I have two ideas that could lead to more or less the same result.
I am trying to have similar cells or tables update themselves to whatever the most recent entry was in the linked system. For example, cell A1 is linked to cell B2 (in this system that I am trying to create). I would enter something like "1" or "Text" or whatever in A1 and the system would update cell B2 to whatever I entered in cell A1. However the opposite must work in the same manner. If I changed whatever in cell B2 to, say, "5", cell A1 would also display "5". Also, note that I have Excel 2013.
I need this to work with a cell or a table. So, getting to the possible solutions...
A subroutine in VBA that automatically updates all the linked cells or tables.
Some sort of mechanic unknown to me that uses VBA or another Excel aspect to do this, like a function or tool.
In your answer or solution, please be mindful to my inexperience with VBA. Thanks in advance.
You can try a worksheet change function:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address = Range("A1").Address then
ActiveSheet.Range("B1").Value = ActiveSheet.Range("A1").Value
ElseIf Target.Address = Range("B1").Address then
ActiveSheet.Range("A1").Value = ActiveSheet.Range("B1").Value
End If
End Sub
Although this seems like it might create an infinite loop (the update from the change causes another change) it DOES work for me in Excel 2010..
There are other Worksheet functions you can try as well (e.g. Worksheet_SelectionChange)
This macro needs to be placed/entered as a WORKSHEET macro on the sheet where you desire to use it..it will not work in a Module.
To install:
1) Save your workbook as a macro-enabled file.
2) Close Excel, reopen file, and enable macro security
3) Type Alt-F11
4) In the project explorer view on the left, look for your sheet name. Double click it
5) In the code entry area on right (big window) paste the example code above
6) Return to your worksheet and try it.

Hide row in excel by mouseover of particular cell

I am using =Hyperlink in a series of cells in a worksheet to call a public function that changes the value of a particular cell. It works very well. But I want the function to hide a row when I mouse over a particular cell. Can anyone help?
The code is:
Public Function highlightcell(seriesName As Range)
Range("valSelOption") = seriesName.Value
'enter code here to hide Row 1
End Function
You could try
Rows("1:1").RowHeight = 0
As an easy way to hide row 1
Simple Answer
There is no On Mouseover event in Excel.
This thread shows how you can achieve something similar using selection change, but it wont work for what you want.
http://www.ozgrid.com/forum/showthread.php?t=147219
Hiding a Row
Use this code:
ActiveSheet.Rows(2).Hidden=True
This will hide row 2.
Explaining Events in Excel
If you want the worksheet to react to the user (clicking a cell, changing a cell, calculating the sheet, opening the workbook, etc) you want to use Excel Events. This is a good explanation of Excel Events:
http://msdn.microsoft.com/en-us/library/office/hh211482(v=office.14).aspx

How to detect if filter(s) changed on a worksheet?

In excel workbook project, how could we detect if the filters on some worksheet are updated?
Make sure that you have a formula (e.g. COUNT) that includes an entire column of the data. In the case of a Table, turn on the Total row.
When the filter is changed, the Excel calculation event will fire because of the formula and you can pick this up by inserting the following code into the sheet.
Private Sub Worksheet_Calculate()
MsgBox "Calculation"
End Sub
Your sheet will need to be designed to only have data, otherwise code will be needed to determine if the calculation event on the sheet did not originate due to a change in filter.
You will need to add code to pick up the filter values. Focus on the Filter class members like Citeria1, Criteria2, Operator, On, etc.
My case was an Excel database. I created a label indicating "number of filtered items" or "number of meeting instances", so that when you filter using dropdown filters this label will update. I didn't find any "filter change" event. I tried the method described above like the following:
Select a cell in your sheet that you are not willing to use
Set the formula of the cell to "=count(B:B)" or "=counta(C:C)" or any formula that depends on the entire column. Make sure that the cell is not in the same column
Set the format type of this auxiliary cell to "custom" and set the format to ";;;" so the cell contents will be invisible
In VBA, use the "worksheet calculate" event to execute your code
Private Sub Worksheet_Calculate()
' The commands and actions you want to execute when filter changes
End Sub
Now, you are done

Resources