View and hide columns in excel using vba - excel

I have a worksheet with values in columns B:G. In the same sheet in cell A1 I have made a drop down list using data validation with values like A, B and C.
What I require is when I select cell value A then columns B:C need to be visible and the other columns should be hidden from D:G. In the same way if I select B from the list I need to view columns D:E and B:C and F:G should be hidden.
Could you please help me on this.
Note: I don't have good knowledge in VBA.

Try this:
Open the VBA editor (ALT + F11)
Double click Sheet1
Select Worksheet in the top left drop down and Change in the top right hand drop down
Paste this code
NB- this assumes data validation is in cell A1
Private Sub Worksheet_Change(ByVal Target As Range)
Dim allColumns As Range
Set allColumns = Columns("B:G")
allColumns.Hidden = True
If Not Intersect(Target, Range("A1")) Is Nothing Then
If Target.Value = "A" Then
Columns("B:C").Hidden = False
ElseIf Target.Value = "B" Then
Columns("D:E").Hidden = False
ElseIf Target.Value = "C" Then
//Add more logic here
End If
End If
End Sub

Go to view --> macros.
Hit the dropdown and do "record new macro".
Right click on a column header and do hide column.
Then do unhide column.
Do Macros->stop recording.
Macros-->View macros
Click edit.
you get the following code:
Columns("C:C").Select
Selection.EntireColumn.Hidden = True
Selection.EntireColumn.Hidden = False
Now you know how to hide and show columns. First you select the column then your set Hidden = true or false.
Google: excel macro when cell value changes
Click the first link: http://support.microsoft.com/kb/213612
Take the code from that link and read the comments:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim KeyCells As Range
' The variable KeyCells contains the cells that will
' cause an alert when they are changed.
Set KeyCells = Range("A1:C10")
If Not Application.Intersect(KeyCells, Range(Target.Address)) _
Is Nothing Then
' Display a message when one of the designated cells has been
' changed.
' Place your code here.
MsgBox "Cell " & Target.Address & " has changed."
End If
End Sub
Make sure you read the link very closely. And follow the instructions. I find I sometimes rush and miss important details
Let me know if this is enough or you need more help.

It's been a long time, but it may still be useful to someone.
Userform to hide-unhide worksheet’s columns :
The userform that we created to hide the columns in the workbook and unhide the hidden columns contains also a button to minimize userform. With to the drop-down list in the userform can be navigated between worksheets, the selected worksheet from the combobox is active and the column management(hide-unhide) of this worksheet is provided.
Explanations and sample Excel file here

Related

Multiple Worksheet Change Events: Multiple Dropdowns

I am trying to create a workbook where if I change a dropdown on 1 sheet, it automatically updates that same dropdown on a second sheet. These dropdowns will represent different scenarios and my purpose in creating this is to allow the end-user the ability to change the scenario dropdown from any sheet, rather than just one.
I used this reference to create a VBA for changing 1 dropdown -Original VBA code referenced - and it worked correctly (See example workbook to download). However, now I want to add the other 2 dropdowns so that if any changes are made to them it updates accordingly.
I'm also open to other solutions if you know of something better.
Sorry if this question was elementary -- I am new to VBA.
Using the example from the Original VBA code referenced, if you were looking to apply this rule to 2 sets of drop downs (4 total), instead of 1 set - you would duplicate the snippet from "If Not Intersect(..." to "..End if" and then update the reference to the 2nd set of cells.
EXAMPLE:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim targetSheet As Worksheet
If Not Intersect(Target, Range("A1")) Is Nothing Then ' watch only cell A1
Set targetSheet = ActiveWorkbook.Worksheets("Sheet2") ' define the sheet to copy to
On Error Resume Next
Application.EnableEvents = False
targetSheet.Range("B1") = Target.Value ' copy to cell B1 on the target sheet
Application.EnableEvents = True
End If
If Not Intersect(Target, Range("A2")) Is Nothing Then ' watch only cell A1
Set targetSheet = ActiveWorkbook.Worksheets("Sheet2") ' define the sheet to copy to
On Error Resume Next
Application.EnableEvents = False
targetSheet.Range("B2") = Target.Value ' copy to cell B1 on the target sheet
Application.EnableEvents = True
End If
End Sub
You would then repeat for the other worksheet.

Excel VBA Clear indirect dropdown cell within a column range

I have successfully used the code below to clear the contents of an indirect cell based on a change of a dropdown list in order to prevent mismatched data.
For example my first drop down cell is D2 and has 3 options, when an option is selected, my second cell F2 (based on named ranges) changes to show the choices specific to the selected option.
When a user changes the first option in D2, the code triggers and clears out the contents of F2 so a new selection can be made preventing mis-matched data.
Private Sub Worksheet_Change(ByVal Target As Range)
On Error Resume Next
'Clear Adjustment Reasons if there is a change of Type'
If Target.Address = "$D$2" Then
If Target.Validation.Type = "$F$2" Then
Application.EnableEvents = False
Target.Offset(0, 2).ClearContents
End If
End If
exitHandler:
Application.EnableEvents = True
Exit Sub
End Sub
What i'm looking to do is now extend this so it can work on a sheet where the dropdown lists will be in each cell of a column (ranged from M7:M500), and the indirect dropdown in each cell of (ranged from N7:N500)
So, when the user changes the original option in any of the cells in the M range, then the corresponding N cell in the same row will clear.
Does anyone know any examples i could potentially look at to make this work?

Excel IF any cells is Y then all other cells are N

I need some advise on an excel formula.
I have 6 cells (A1 to A6) - If ANY of them is "Y" then I want all the others to auto populate to "N".
Is there a way to do this?
Try following code
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range
Set rng = Range("A1:A6") 'set rng as A1:A6
If Not Intersect(Target, Target.Worksheet.Range("A1:A6")) Is Nothing Then
Application.EnableEvents = False
If UCase(Target) = "Y" Then 'check if entered value is Y
rng.Value = "N" 'make cells N
Target = "Y"
End If
Application.EnableEvents = True
End If
End Sub
Insert VBA code to Excel Workbook
Open your workbook in Excel.
Press Alt+F11 to open Microsoft Visual Basic Editor.
At the top left corner of the editor window, under Project-VBAProject pane, double click on the sheet name you want code to work for.
Copy above VBA code and paste it to the right pane of the VBA editor.
Finally, change the values in Range A1:A6 and you should get desired result.
To know how to enter this code in workbook see this.

Hide/Unhide cells with empty rows on Worksheet_Change

I have two columns of data that is pulled into a worksheet from data on other sheets elsewhere in the workbook via a formula in each cell...
The first column, Column A, has either a Yes, No or is blank from data that is pulled in via a formula from another sheet.
The second column, Column B, also has data pulled in from elsewhere but every row has data in it.
What I hope to do is hide any rows that does not have anything in column A. Any rows with data in column A should be visible. I'd like this to be updated via the worksheet_change event using VBA when data is entered that appears in column A.
Many thanks if you can help.
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A:A")) Is Nothing Then Exit Sub
Application.EnableEvents = False
dim lrow as Integer
dim i as Integer
lrow = Cells(1, 2).End(xlDown).Row
For i = 1 To lrow
If Cells(i, 1) = 0 Then
Rows(i).Select
Selection.EntireRow.Hidden = True
End If
Next
Application.EnableEvents = True
End Sub
You have to insert this on the code of the sheet. right click the sheet name and press the view code and save it as macro enable.
It gets activated when changes have done to column a.

Create macro upon double click cell display filter in new sheet

I have a question regarding creating macros whereas the scenarios as follows:
Sheet1
Upon clicking any cell in Sheet1, it will automatically filter based on cell A and B.
Sheet2
Automatically display filtered criteria based on double click from Sheet1
For example: when I double click on C1, on Sheet2 will automatically diplay filtered data based on A1 and B1 and same thing goes to if I double clik on C2 on Sheet2 will automatically diplay filtered data based on A1 and B2.
Really need help from the experts here.
This would be the code you need to catch your single-click event:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Debug.Print Target.Address
End Sub
This would be the code you need to catch your double-click event:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
Debug.Print Target.Address
'cancel the double click, prohibiting editng of cell per double-click
Cancel = true
End Sub
I would have helped you with your filtering too, but since you did not paste any code to that, and I don't get how excactly you want what data to be filtered, I'll leave that up to you ;)
Edit:
This code can be used for Worksheet_SelectionChange and will set a filter based on a valid selection inside the used range. If a filter is already in place, it will be deactivated.
On Error Resume Next
If Sheet1.AutoFilterMode Then
'clear existing autofilter
Sheet1.UsedRange.AutoFilter
Else
'setup filter based on selection
Sheet1.UsedRange.AutoFilter field:=Target.Column, _
Operator:=xlFilterValues, _
Criteria1:=Target.Value, _
VisibleDropDown:=True
End If

Resources