Cost Tracker.xlsmHi all,
I am new to this forum because normally i can find the answers to all my excel questions without having to post the thread myself, however i am stuck here.
I am making an excel sheet for work and i have made 2 worksheets, 1 containing the lists that i would like to have in my dropdown menus, and a worksheet in where i have implemented these drowdown menus.
The sheet i am making contains a large amount of colums that a lot of times do not need to be filled out.
I have created a drowdown menu in a cell in my worksheet, based on the information in the list worksheet via data validation, list, source=REVLIST.
In order to allow my coworkers to add information to this dropdown list the following code is added to my worksheet:
Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Address(False, False) = "$AN$2" Then
Select Case Target.Value
Case "No": Columns("AO:AQ").Hidden = True:
Case "Yes": Columns("AO:AQ").Hidden = False:
On Error Resume Next
On Error Resume Next
Dim ws As Worksheet
Dim str As String
Dim i As Integer
Dim rngDV As Range
Dim rng As Range
Dim lCol As Long
Dim myRsp As Long
Dim strList As String
If Target.Count > 1 Or Target.Value = "" Then Exit Sub
If Target.Row > 1 Then
If Target.Validation.Type <> 3 Then Exit Sub
str = Target.Validation.Formula1
str = Right(str, Len(str) - 1)
Set rng = ThisWorkbook.Names(str).RefersToRange
If rng Is Nothing Then Exit Sub
Set ws = rng.Parent
If Application.WorksheetFunction _
.CountIf(rng, Target.Value) Then
Exit Sub
Else
myRsp = MsgBox("Add this item to the drop down list?", _
vbQuestion + vbYesNo + vbDefaultButton1, _
"New Item -- not in drop down")
If myRsp = vbYes Then
lCol = rng.Column
i = ws.Cells(Rows.Count, lCol).End(xlUp).Row + 1
ws.Cells(i, lCol).Value = Target.Value
strList = ws.Cells(1, lCol).ListObject.Name
With ws.ListObjects(strList).Sort
.SortFields.Clear
.SortFields.Add _
Key:=Cells(2, lCol), _
SortOn:=xlSortOnValues, _
Order:=xlAscending
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With ws.ListObjects(strList)
.Resize .DataBodyRange.CurrentRegion
End With
End Sub
End If
End If
And the following code has been entered in the list
Private Sub Worksheet_Change(ByVal Target As Range)
Dim strList As String
On Error Resume Next
strList = Cells(1, Target.Column).ListObject.Name
If strList <> "" Then
Application.ScreenUpdating = False
With ListObjects(strList).Sort
.SortFields.Clear
.SortFields.Add _
Key:=Cells(2, Target.Column), _
SortOn:=xlSortOnValues, _
Order:=xlAscending
.Header = xlYes
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
With ListObjects(strList)
.Resize .DataBodyRange.CurrentRegion
End With
End If
Application.ScreenUpdating = True
End Sub
Now in my worksheet i would like to add a code that hides colums AO:AQ if cell AN2 = no (the no being selected from the dropdown menu)
Can anybody please help me with the code for this and how to implement it in the current change code that is already in place?
Thanks in advance.
Regards,
Robin
Bookmarks