Macro to hide columns containing blanks - excel

I am trying to write a VBA code that would allow me to hide all columns containing whatever text/option has been selected from a drop down list in excel.
For example, all the drop down list options are within columns I:FT. The drop down list contains "Cat", "Dog", "Horse", "Fish", " " <—blanks. If I am selected on the blank " " option in the drop down list then I want to hide all columns in “I:FT” containing blanks.
When it does hide the columns containing blanks, all rows in the column must be blank when the I:FT range is selected. The range of these cells are 416R x 169C.
Additionally, I need it to only account for visible rows. I want it to have the ability to filter on a column (such as part number) and have the macro run and only hide the columns with blanks based off whatever is shown from the filtered part number.
The drop down list is associated cell H2 and the list is on another sheet in the background
I am fairly new to writing VBA code and any help would be MUCH appreciated.
Thank you !

To get you started, see the recently answered question about hiding rows based upon a drop down box. You can use this to adapt it for your own purposes.
Multiple Non-contiguous rows in excel Based on cell value
If the columns may change order from time to time, then hard coding the column numbers would not be sufficient. In that case, you could use a couple of methods to make sure the correct column(s) is hidden, which would work by assigning the columns to named ranges first:
Worksheet.Range("CELL REFERENCE FOR 1st ROW").EntireColumn.Hidden = True
Worksheet.Columns("COLUMN REFERENCE").Hidden = True
Once you have attempted that and are still stuck. Come back here, explain your progress and share what VBA code you have written so far.

Related

In excel, how can I automate a sheet to be filled based on conditions of another sheet?

Is there a way in excel to copy the contents of one column to another sheet based on particular values in another column.
I have data which looks like this:
Sheet1:
Sheet2:
I would like to copy the column A of sheet 1 directly onto column A of sheet 2, but only if the value in column B is Y.
I have tried used the match function but am not sure how the best way to do this would be. Is there a way for me to achieve this?
My desired output is:
As discussed in the comments, #Waldorf99 was looking to have a second worksheet that would automatically show a filtered list from the first sheet. I can think of a few ways to do this (array formulas or pivot tables come to mind). The problem with mixing dynamic columns with static values is that the static values would become desynced from the dynamic ones.
In the original example, rows may have a blank value in the condition column in sheet one, and then may be assigned a Y or N at a later date. If a Y is assigned to a row in the middle of other rows, the filtered sheet would shift the existing rows down to make room. The static values would stay where they were, and would become desynced. To demonstrate:
If the above image is the original state of sheet 1 and 2...
...adding a y next to x.1.c would result in sheet two shifting columns A and B of row 2 down, but leaves columns C and D behind (as they are static, and not tied into the first two columns in any way).
One thing that may work as you are wanting are filters. You would only have one sheet, with all of the data manually entered. Then you can add filters, and change them to hide rows temporarily when needed.
To add and use filters:
Start with your data all on one sheet...
Highlight your data...
On the Home tab, select "Format as Table" and choose any style...
This turns your data into a table. You can filter by clicking the drop down in a given columns header row, then deselecting the values you want to hide.
The results are a table that only shows the rows with a 'Y'.
The other rows aren't removed, just hidden. You can always reset or change your filters, to configure which rows are visible.
Note: when working with tables, they will auto expand to account for new rows, so long as you work in the row directly under the table (e.g. don't leave blank rows). You can also manually resize the table at any time by clicking and dragging from the bottom right corner of the table.
There are tons of resources of Excel tables online, and it's a pretty useful tool in Excel.
Hope that achieves what you were looking for.

Excel VBA Drop Down List with dependent list with multiple selection

I have a spreadsheet where clients have to make Drop Down selections for (1) Account, (2) Account Name and (3) Account Categories throughout excel and are dependent of each other. I actually did a non-VBA Drop Down List dependent of each other. The problem is that the drop down list only works one at a time instead of making multiple selections.
So what I want of course is the drop down list to work for not just one row, but selections of many rows.
The only way this Drop Down List would work non-VBA is by creating as my many Unique List depending on the number of accounts and their dependents.
So far without using Excel VBA I have done the following:
I have Defined Name the 3 Main List ranges: GLacct,GLname, & GLcat:
Created - 3 Unique List:
So this Unique List is populated when the first DDL "55700" is selected, next DDL will populate "Research - Other" and then the next DDL will populate 3 choices correspondent to "Research -Other".
So to create the Unique List in excel I added these formulas:
UniqueGLacct: `INDEX(GLacct,MATCH(0,COUNTIF($F$1:F1,GLacct),0))`
UniqueGLname:
`INDEX(GLname,MATCH(0,COUNTIF($G$1:G1,GLname)+(GLacct<>TM!$A$16),0))`
UniqueGLcat:
`INDEX(GLcat,MATCH(0,COUNTIF($H$1:H1,GLcat)+(GLacct<>TM!$A$16)+(GLname<>TM!$B$16),0))`
Name Manager – Refers to and added the following formulas:
GLacct: Offset(TB!$A$2,0,0,COUNTA(TB!$A$2:$A$1000))
GLname: Offset(TB!$B$2,0,0,COUNTA(TB!$B$2:$B$1000))
GLcat: Offset(TB!$C$2,0,0,COUNTA(TB!$C$2:$C$1000))
uniqueGLacct: OFFSET(TB!$F$2,0,0,COUNT(IF(TB!$F$2:$F$1000=””,””,1)),1)
uniqueGLname: OFFSET(TB!$G$2,0,0,COUNT(IF(TB!$G$2:$G$1000=””,””,1)),1)
uniqueGLcat: OFFSET(TB!$H$2,0,0,COUNT(IF(TB!$H$2:$H$1000=””,””,1)),1)
Added the Data Validation to cells that need the drop down list in Sheet named ™: ( The problem is if I where to make another selection in the next row, I still get the data from the first selection, which means in order to work I have to make as many Unique List for each account.
cell: A16: uniqueGLacct
cell: B16: uniqueGLname
cell: C16: uniqueGLcat
How would I make this Excel Drop Down List dynamic for 3 List dependent for multiple selections using Excel VBA. But if you notice accounts below 55700 can't popular their dependent list because the first selection made is 55700.
The only way this would work without using VBA is by creating Unique List for each account dependent.
I'm not sure you can't do this without a macro.
1) First you could change your reference style to R1C1:
Link to great explanation
2) Then you would create a named range for each unique list (I am assuming they are finite).
3) Then you would add validation List and then Indirect to an name of that range (in the header or an offset cell) to each indirect.
With this, you can just say "This Row, Column offset" or whatever is applicable:
In this case I selected the same row, one column to the left which is the name of the named range that I wanted in the drop down.
The benefit of this is that you have 1 formula across all of your cells. You can change the display back to A1 type display and the formulas will adapt.

Matching data based on IDs

I have two sheets, one is a huge table of item ID's with a corresponding code column next to them. However over time this has not been properly filled in. I have assembled a complete list of item ID's and corresponding codes in another sheet.
I want Excel to fill in the blanks using the ID column as a key.
What's the easiest way to get Excel to look for the ID in the main sheet, and paste in the corresponding code from the other sheet?
Rather than fill in just the blanks - and face the issue of how then to skip over cells already populated - use the same formula to fill in the blanks and to overwrite whatever happens to be present already.

Excel VBA to Hide Complete Rows based upon the values cells within Named Ranges

I've been trying to figure out some code to perform the following via VBA.
I have at the moment one named range ("Quarter2"), which will eventually expand to a number of other named ranges, names TBC at the moment but probably "Quarter3" and onwards etc
Each of the names ranges has the same layout, i.e. the same information appears in the same columns as they'll be shown on the same tab, or the same layout but on tabs within the same workbook etc
I need a macro to hide the entire row based on the value of one of two cells:
In column B, if the value is >= 1 then it's to be hidden, and/or
In Column D, if the value is >= $50000.00 then it's to be hidden also
The number of ranges need to be expandable so that needs to be taken into account also.
I've have a look at various snippets of VBA that I've found but I can't get one to work for this situation.
Any help appreciated.

Excel VBA - Get rid of duplicate values in a particular column

I have a worksheet with many rows of IDs.
I would like to know the best way to write a VBA procedure that will look at the range of values in one column, and replace the entire range with only the unique values which appear in that range. So a column of 1000 IDs might reduce down to a column of 150 unique IDs. I would not like this procedure to affect the data in other columns in the worksheet.
So, say the initial column A was:
*IDs*
ID12
ID12
ID34
ID56
ID78
ID78
ID78
I would like it to replace the column with a new column A:
*IDs*
ID12
ID34
ID56
ID78
Thank you kindly.
Note: I know how to do this manually a few different ways, but I would like to cycle through and do this procedure for every non empty column on a sheet, and the columns are of varying length.
To achieve what you want please do the following:
Select entire column you want to remove dupes of.
Go Ribbon Data > Remove Duplicates.
Set My data has headers (according your input).
You're done. If you want these steps in VBA - turn on macro-recorder before the start (bottom left corner of Excel window).

Resources