Change variable from multiple sheets? - excel

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

Related

Auto fit row height on other worksheets from a activex combobox

Firstly I cannot program in VBA but I know how useful it is. Secondly my Excel version is 2010 but the organisation has 365.
I have a project that is to be used to help a voluntary organisation in the UK comply with a new food labelling law. I have one in use but all line heights have to be adjusted manually with the risk that if not done some ingredients will not be stated.
The main sheet called View, it is for selecting a meal option in an activeX combobox drop down and via VLOOKUP's displaying a list of ingredients from the Data sheet. On the View sheet are also the buttons to run a number of print macros. Because of the very variable contents of each cell I need to reset row height after each selection, which I do on the View sheet as follows:
Private Sub Worksheet_Change(ByVal Target As Range)
Sheets("View").Select
Range("B3:B19").Select
Selection.Rows.AutoFit
End Sub
What I also need to do is reset row height on set ranges on other linked sheets as well. Linked to the View sheet are other sheets called Ingredients (which specifies what ingredients have to be used), Notice (headed A4 notice of all ingredients used) and Labels (same information as Notice but 6 to a page for takeaway food labels) which can be printed from the macro buttons. I have googled and found a number of suggested solutions, including this one:
AutoFit on rows that are referenced from different sheet using function - VBA
but for whatever reason I could not get any to work. Is there a way that I can get selected ranges of rows on the other sheets to work off of the Combobox change on the View sheet, either by modifying the above code or putting Private Subs into the other sheets? I have already tried using variations of the View code without success.
This is what worked thanks to CDP1802
Private Sub Worksheet_Change(ByVal Target As Range)
'To reset row heights on all printable sheets on selection in Combobox
Sheets("View").Rows("3:19").AutoFit
Sheets("Ingredients").Rows("5:22").AutoFit
Sheets("Notice").Rows("5:19").AutoFit
Sheets("Labels").Rows("1:39").AutoFit
End Sub

Read One Cell and Copy to Another Only Upon Opening Spreadsheet

I need to copy the data in cell c3 to cell i11 when I open my spreadsheet. I do not want i11 to change after this action until I reopen the spreadsheet.
You can set the code in the ThisWorkbook part of your projects. Select Workbook and Open or use the following code.
The working formula is the value you see me do below, but this can be achieved a number of way such as using Cells(3,3).Value. Additionally the way the sheet is referenced can vary. I put both the Activesheet reference and a explicit sheet reference, depending how your sheet is structured you may have to use one over the other, but choose one below and see how your sheet handles it and if adjustments are needed.
Private Sub Workbook_Open()
'Use one of these depending on how your sheet opens
ActiveSheet.Range("I11").Value = ActiveSheet.Range("C3").Value
Worksheet("YourWorksheetHere").Range("I11").Value = Worksheet("YourWorksheetHere").Range("C3").Value
End Sub

Excel conditional formatting using ADDRESS function

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

Excel 2010 - Multiple Types of Locks on the Same Sheet

So I'm trying to create an excel sheet that handles a user input/output task (I know that this is a kludge at best and offensive at worst). So I want the user to be able to edit the input cells (and only the input cells), and select (but not edit) the outputs.
Example: Grid A1:C3. A1:A3 are completely locked - unselectable. B1:B3 are selectable and editable. C1:C3 are selectable and uneditable.
Is this possible? My gut is telling me no, but I thought I'd ask anyway.
It is possible using worksheet protection:
In the Format Cells dialog of the cells B1:B3, uncheck the "Protected" checkbox in the last tab (Protection). Then protect the worksheet (right click the worksheet tab at the bottom->Protect Sheet). Once the sheet is protected, the user can only edit those cells that are unprotected.
Regarding the selection of C1:C3 and non-selection of A1:A3 - you can allow/prevent the user from selecting protect cells in the same sheet protection dialog. However, this is a sheet wide setting, so be default you can only fully prevent selecting all protect cells or allow selection of all cells.
If you only want prevent selection of A1:A3 because you don't want the user to see the formula, just check "Hidden" in the Format cells dialog - this way the user does not see the formula.
If you really need to separate between selectable and unselectable, either split across two worksheets - or use a little VBA macro. To do so, open the VBA editor (Alt+F11) and double click your worksheet in the top-left list. In the code window, enter the following code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Me.ProtectContents And Intersect(Target, Range("B1:C3")) Is Nothing Then Range("B1").Select
End Sub
This way, every time a cell outside your required range is selected, B1 is selected instead

Microsoft Excel: Programmatically create a hidden named range

I am told that the Excel object model permits a Range that is not a part of any sheet, yet contains a set of cells and is denoted by a name in the workbook.
Can anyone explain to me how these fit into the Excel object model and how one would go about creating such a thing programatically (either in VBA or .NET source code).
Thanks.
Your question is a little vague, but I'll give it a shot.
Well, as Dave describes, you can give a specific range of cells on a sheet a "Range Name" which you can then refer to programatically, but that doesn't sound like what you are asking.
It sounds like you are asking "is there an abstract RANGE of cells available to be used by VBA code that doesn't literally exist on any worksheet?" The answer to this is no, even named ranges are simply a convenient reference to a real set of cells on a real worksheet.
You can, however, programatically hide a worksheet so that the user doesn't see it, and still work with cells and ranges on that sheet. Just do:
Sheets("Sheet1").Visible = xlSheetHidden
Sheets("Sheet2").Visible = xlSheetVeryHidden
Sheets("Sheet3").Visible = xlSheetVisible
What's "VeryHidden", you ask?
It means that the user can't go to Format, Sheet, Unhide and make the sheet visible.
So if I'm correctly understanding what you want, just programatically hide one of the sheets, then use Dave's technique to create a named reference to a range on this hidden (or VeryHidden) sheet.
That would be a named range. You can reference a selection of cells, and just type a name where it says 'A1' next to the formula bar. That creates a named range that doesn't change.
Alternatively you can create a named range that is based on a formula, and therefore potentially changes as data in the spreadsheet changes. You do this from the 'Define Name' option (which is in the Formulas Ribbon in Office 2010).
Named ranges can be accessed from VBA, and (I'm pretty sure) from .net.
So you'd access the named range from vba like so:
Range["MyNamedRange"]
Yes, there is a NamedRange control in the Microsoft.Office.Tools.Excel namespace in VSTO. This is a host control which is different from the native Range control in the Microsoft.Office.Interop.Excel.Range namespace.

Resources