Clear contents of hidden excel cells based on dropdown menu vba - excel

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

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

Getting Values from Multiple Comboxes in same sub

I have an excel sheet workbook with 2 sheets. One sheet has data imported from an external source and the other sheet then filters this data. I want to be able to select a date range to narrow done the filtered results.
To do this, I have 4 comboboxes, one checkbox, and one button. When the button is clicked I want my sub to first see if the checkbox has been checked. If it has not, then just run filter the data regardless of date. If the checkbox is checked, I then want to look at the four comboboxes. The first two will have the start year and start month and the last two will have the end year and end month. I want to take these values and filter my data using this criteria as well when the checkbox has been checked.
The issue I am having is that I cannot get the value of the combobox. For example, I named one of my comboboxes "Start_Month". In my sub, when I select the sheet with the combobox first, then I use "Start_Month.Value" to check the combobox value, but this gives me runtime error 424 "Object Required".
Does anybody know how to get the value of four different comboboxes in the same sub or is this not possible?

Can the colour of a cell be changed once it has been selected?

I am doing a large amount of data entry but down a list which is already populated i.e. I am changing slightly the entries if they meet certain requirements.
I was wondering if anyone knew of a way to get a cell to change colour once it has been selected?
The work flow I want is:
Select cell at the top --> make alteration if necessary --> press enter to go down to next cell --> the cell changes from red to green.
The idea is that when I take a break or check different values in the same excel sheet I can very easily find where I left off without having write down the row number.
It's much easier to change colour of the cell when you select it (rather than after deselecting). To do this:
Right-click the sheet tab
Click 'view code'
Paste the following into the window that pops up:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Target.Interior.ColorIndex = 33
End Sub
Try it.

Dynamically Populate Listbox - Exclude Empty cells

I am creating a form in excel (not a userform) and I am populating the listbox using cells. However, these cells are sometimes A1:10 and sometimes they are A1:A4. Is there a way to dynamically change what is shown in the listbox?
Right now, when I use A1:10 and there are only 4 cells populated, I get the list of 4 populated cells followed by 6 blank entries. I'd like to get rid of the 6 blanks when there are only 4.
Unfortunately, this is more of a workaround than a solution. However, it may be able to do what you need it to do. I've been hitting the same wall you are with trying to make ranges dynamic.
Without seeing some code to know exactly what you're doing, try something like this.
Private Sub ListBox1()
x = 1
'Add items to listbox until you reach an empty cell.
Do while Cells(x,1) <> ""
ListBox1.AddItem Cells(x,1)
Loop
I'm not very familiar with listboxes outside of userforms but this should do approximately what you want to do.
Edit your original post with your code so we can try and get a better understanding of what you've tried and what you're trying to do.
You can create a named range using a dynamic formula. Either of the below formulas will work.
=A1:INDEX($A$1:$A$10,MATCH("",$A$1:$A$10,0)-1)
=OFFSET($J$7,0,0,MATCH("",$J$7:$J$32,0)-1)
To create the named range, click ctrl+F3 then click new, insert one of the two options above into the "refers to:" section, then label the new range whatever you would like. in the "row source" section of the listbox simply type in the name you selected for your new named range.

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