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
Related
I am looking to clear a set of cells (reset them) every time a new option is selected in the dropdown menu. However, based on the choice selected, a different combination of rows will be hidden. There are 12 of these dropdowns and so I need a function I can call that will clear only the cells related to that drop down (when all are unhidden there are 16 rows and 3 columns). I have tried using the formula below, however it does not recognize the hidden rows and ends up deleting the contents of the following dropdown's rows. Thank you in advance for your help.
Sub Clear(Target As Range)
Range(Target.Offset(16,).Address, Target.Offset(,3).Address).Value = " "
End Sub
I have a workbook with three worksheets. On the first worksheet the user is asked to click "relevant" checkboxes that relate to their project. Based on these checkboxes one thing happens but on two worksheets:
On the first worksheet (same as checkboxes)
1. related rows appear when checked and disappear when not checked. This I have managed to figure out using the following code for each checkbox (I have 8 checkboxes):
Private Sub CheckBox1_Click()
[22:25].EntireRow.Hidden = Not CheckBox1
End Sub
On the second worksheet
2. Same as above but on a different sheet, I would like the same result of related rows appearing when checked and disappear when not checked. I am not sure how to refer to another worksheet, and / or if there is a way to insert another line to the above.
Any help is appreciated.
I have created a ComboBox to do a Google-style search of Cities. I have my ComboBox on sheet 1, that is linked to a dynamic range of cities on sheet 2. The ComboBox output cell is what changes the dynamic range.
Here’s the problem I’m having. I start typing “sea” and I get two results in my drop down list-seaport and Seattle. So far so good. Then I use the down arrow to Select Seattle and the program crashes and restarts. I believe it is because when I hit down one time, seaport is selected and this narrows down my range to just the one option and Seattle disappears.
Is there any way around this? Either disabling the use of the keyboard down arrows for selections or preventing the output to the linked cell until a selection is finalized?
This the code for my combobox on Sheet1
Private Sub TempCombo_Change()
TempCombo.ListFillRange = "Cities"
Me.TempCombo.DropDown
End Sub
The named range "Cities" is defined by formula on sheet 2
='City Data'!$L$2:INDEX('City Data'!$L$2:$L$570,MAX('City Data'!$K$2:$K565,1))
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
I've got a code written that categorizes employees along with their qualifications. In order to weed out employees with unwanted qualifications I have applied a filter to each column that titles the category of their qualification.
I've written my VBA code in order that repetitious names and qualifications are made invisible for ease of location. However, I am unable to get the code to run automatically.
Currently the only way I can get the code to run is by setting it to
Private Sub Worksheet_Change(ByVal Target As Range) and then changing the value of an arbitrary cell.
i found what I believe to be the correct solution at:
http://www.ozgrid.com/forum/showthread.php?t=72860
But I cannot make sense of it.
Is there a way to run this code without having to select and deselect a cell after the filter has run?
The key points from my article Trapping a change to a filtered list with VBA
There is more detail and a sample file with the article, the key points are summarised below
A "dummy" WorkSheet is added with a single SUBTOTAL formula in A1 pointing back to the range being filtered on the main sheet.
A Worksheet_Calculate() Event is added to the "dummy" WorkSheet, this Event fires when the SUBTOTAL formula updates when the filter is changed.
The next two setps are needed if it is desired to run the Workbook Calculation as Manual
Add a Workbook_Open Event to set the EnableCalculation property of all sheets other than "Dummy" to False.
Run the Workbook in Calculation mode
The ozgrid code you mentioned tells you that you can put your code in a worksheet_calculate event (in the worksheet module), as long as you have something that will recalculate when you change your autofilter. This something can be a subtotal formula that you can hide in your worksheet, e.g. =subtotal(3,A:A)
Still need to investigate but looks like Chart Calculate event is triggered when Calculation = xlCalculationManual. At least works on my Excel 2007. So the steps are:
create a chart (saying "Chart 1" on Sheet1) which actually uses data from any of your table column
check that it updates its picture when you change the filter
create a new class e.g. clsChartEvents:
Public WithEvents Chart As Chart
Private Sub Chart_Calculate()
Stop
End sub
add this code to some module or class:
Private chartEvents as new ChartEvents 'create a module-scope variable
sub SubscribeToChartEvents
set chartEvents.Chart = Sheet1.ChartObjects("Chart 1").Chart
end sub
execute SubscribeToChartEvents
change a filter and you should appear in Sub Chart_Calculate()